霍格沃兹测试开发学社
ceshiren.com
是 JavaScript Object Notation 的缩写
是一种轻量级的数据交换格式
是理想的接口数据交换语言
官网:https://www.json.org/json-en.html
JSON
请求体
JSON
字符串HashMap
对象 + Jackson
库import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
public class TestJsonStr {
@Test
void testJsonRequest(){
// 定义请求体数据:json序列化后的字符串
String jsonStr = "{\"Hello2\": \"Hogwarts\"}";
given()
.contentType("application/json") // 设置请求内容类型
.body(jsonStr) // 设置请求体数据
.log().headers() // 打印请求头信息
.log().body() // 打印请求体信息
.when()
.post("https://httpbin.ceshiren.com/post") // 发送请求
.then()
.statusCode(200); // 响应状态码断言
}
}
import java.util.HashMap;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
public class TestJsonObj {
@Test
void testJsonRequest(){
// 定义请求体数据:HashMap 对象
HashMap<String, String> jsonObj = new HashMap<>();
jsonObj.put("Hello", "Hogwarts");
given()
.contentType("application/json") // 设置请求内容类型
.body(jsonObj) // 定制请求体数据
.log().headers() // 打印请求头信息
.log().body() // 打印请求体信息
.when()
.post("https://httpbin.ceshiren.com/post") // 发送请求
.then()
.statusCode(200); // 响应状态码断言
}
}
HTML
< >
标签来描述信息XML
请求体
XML
文件示例接口地址:http://dneonline.com/calculator.asmx
XML
文件import static io.restassured.RestAssured.*;
import org.junit.jupiter.api.Test;
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class TestXML {
@Test
void testSoapApi() throws IOException {
// 定义请求体数据:源自文件对象
File file = new File("src/test/resources/add.xml");
FileInputStream fis = new FileInputStream(file);
String reqBody = IOUtils.toString(fis, "UTF-8");
given()
.contentType("text/xml") // 定制请求内容媒体类型
.body(reqBody) // 定制请求体数据
.log().headers() // 打印请求头信息
.log().body() // 打印请求体信息
.when()
.post("http://dneonline.com//calculator.asmx") // 发送请求
.then()
.statusCode(200); // 响应状态码断言
}
}
Jackson
依赖配置pom.xml
中添加配置信息IOUtils
依赖配置pom.xml
中添加配置信息