之前只用过testNG+httpClient做过接口测试,相较于httpClient用各种封装,RestAssured语言更简洁且规范一些。
git:https://github.com/rest-assured/rest-assured/blob/master/xml-path/src/test/java/io/restassured/path/xml/XmlPathTest.java
参考:
xml和json 的断言:
https://github.com/rest-assured/rest-assured/wiki/Usage#example-1---json
https://github.com/rest-assured/rest-assured/blob/master/xml-path/src/test/java/io/restassured/path/xml/XmlPathTest.java
RestAssured基本语法格式尊崇 given()-when()-then()的风格。
@Test public void testMp2(){ given() .queryParam("wd", "mp2") .when() .get("http://www.baidu.com") .then() .log().all() .statusCode(200); }
支持json返回的各种花式解析
json如下:
{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }, "expensive": 10 }
断言方法如下:
@Test
public void testJson2(){
given()
.when().get("http://172.26.240.167:8000/testerhome.json")
.then()
.log().all()
.statusCode(200)
// .body("lotto.lottoId",equalTo(5))
// .body("lotto.winners.winnerId",hasItems(23,54))
.body("store.book.category",hasItems("reference","fiction"))
.body("store.book.category",hasItems("reference"))
.body("store.book[0].author",equalTo("Nigel Rees"))
.body("store.book.find{book->book.price==8.95f}.author",equalTo("Nigel Rees"))
.body("store.book.find{book->book.price==8.95f}.price",equalTo(8.95f))
.body("store.book.findAll{book->book.price>=5&&book.price<=15}.price",hasItems(8.95f))
.body("expensive",equalTo(10))
// 不支持json的find
// .body("**.find{it.winnerId}",equalTo(23))
;
// .body(hasXPath("//*[]"));
}
支持花式解析xml
xml如下:
Chocolate 10 Coffee 20 Paper 5 Pens 15.5 Kathryn's Birthday 200
断言如下:
@Test public void testXml(){ given() .when().get("http://172.26.240.167:8000/testerhome.xml") .then() .log().all() .statusCode(200) .body("shopping.category.item[0].price",equalTo("10")) .body("shopping.category.item[0].name",equalTo("Chocolate")) .body("shopping.category.item.size()",equalTo(5)) .body("shopping.category.findAll {it.@type=='groceries'}.size()",equalTo(1)) .body("shopping.category.item.findAll {it.price==20}.name",equalTo("Coffee")) .body("**.findAll{it.price==20}.name",equalTo("Coffee")) ; }
proxy设置
RestAssured.proxy("127.0.0.1",8888); RestAssured.useRelaxedHTTPSValidation();//证书信任 //请求中有中文 RestAssured.config=RestAssured.config() .encoderConfig(EncoderConfig.encoderConfig().defaultContentCharset("UTF-8"));
同时给所有请求设置header,利用filter()
RestAssured.filters((req,res,ctx)->{ req.header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36") .header("Content-Type","application/x-www-form-urlencoded") .header("Cookie","u=yindongzi"); Response resOrigin = ctx.next(req,res); return resOrigin; });
post 请求中发送json
@Test public void testPostJson(){ HashMapmap =new HashMap<>(); map.put("Firstname","jack"); map.put("LastName","john"); map.put("array",new String[]{"111","222","333"}); given() .contentType(ContentType.JSON) .body(map) .when().post("http://www.baidu.com") .then().statusCode(200); }
导出response,extract()方法,
Response response= given() .when().post("https://www.baidu.com") .then() .extract().response(); //cookie = response.getHeaders().getValue("Set-Cookie").split(";")[0]; //String cookie2 = response.body().jsonPath().getString("data");