共计 3414 个字符,预计需要花费 9 分钟才能阅读完成。
上一篇文章中介绍了 rest-assured 对返回后果的断言,最初阐明了对于 Response 后果导出的需要。可查看往期文章进行查看。
HTTP/1.1 200 OK | |
Server: nginx/1.12.2 | |
Date: Mon, 13 Jan 2020 02:15:11 GMT | |
Content-Type: application/json;charset=UTF-8 | |
Transfer-Encoding: chunked | |
Expires: 0 | |
Cache-Control: no-cache, no-store, max-age=0, must-revalidate | |
X-XSS-Protection: 1; mode=block | |
Pragma: no-cache | |
X-Frame-Options: DENY | |
X-Content-Type-Options: nosniff | |
Proxy-Connection: keep-alive | |
{ | |
"code": 1, | |
"msg": null, | |
"data": { | |
"tenant_id": 6, | |
"userType": "1", | |
"dept_id": 0, | |
"user_id": 6, | |
"username": "xxx", | |
"jti": "afeb93f8-e4e4-4c15-955b-90cee130c4c7", | |
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.exxxzciLCJjbGllbnRfaWQiOiJzeXN0ZW0iLCJ1c2VybmFtZSI6InFpbnpoZW4ifQ.6NQmjJp_9XSveOaATNLjtTktWe6_WjHY0o9NbBUdDx8", | |
"expires_in": 9999999, | |
"token_type": "bearer" | |
} | |
... | |
} |
@Test | |
void login(){ | |
.. . | |
when() | |
.log().all().post("http://47.xxx.xxx.133/auth/oauth/token"). | |
then() | |
.log().all().statusCode(200).body("code",equalTo(1)) | |
.extract().path("data.user_id"); | |
System.out.println("返回 id 的值是:"+id); | |
} |
运行后果:
extract().asString()
有时候咱们可能须要获取 ResponseBody 中的多个值,例如咱们当初想要获取返回体 body 中的 dept_id 和 user_id,咱们就能够利用 extract().asString()先将响应后果以 json 字符串的模式保留下来,再一一依据须要获取,具体写法如下:
@Test | |
void login(){ | |
.. . | |
when() | |
.log().all().post("http://47.xxx.xxx.133/auth/oauth/token"). | |
then() | |
.log().all().statusCode(200).body("code",equalTo(1)) | |
.extract().asString(); | |
System.out.println("返回 body 的值是:"+json); | |
System.out.println("获取 user_id 的值是:"+ from(json).get("data.user_id")); | |
System.out.println("获取 dept_id 的值是:"+ from(json).get("data.dept_id")); | |
} |
运行后果:
extract().response()
下面都是对响应体的后果进行导出,然而理论工作中咱们的需要远不止于此,咱们可能还须要响应头等信息,例如一些接口的 Token、就可能会在响应信息的 Header 中返回;
这个时候就能够利用 extract().response()来讲所有的 response 信息都保留成一个 Response 对象:
@Test | |
void login(){ | |
.. . | |
when() | |
.log().all().post("http://47.xxx.xxx.133/auth/oauth/token"). | |
then() | |
.log().all().statusCode(200).body("code",equalTo(1)) | |
.extract().response(); | |
System.out.println("返回 response 是:"+response); | |
} |
运行后果:
而后在利用各种 Response.get 办法来获取。
1)获取所有的 Headers
@Test | |
void login(){ | |
.. . | |
when() | |
.log().all().post("http://47.xxx.xxx.133/auth/oauth/token"). | |
then() | |
.log().all().statusCode(200).body("code",equalTo(1)) | |
.extract().response(); | |
System.out.println("返回 headers 是:\n"+response.getHeaders()); | |
} |
运行后果:
2)获取某一个 header 值
相似 key,value 的构造,应用 getHeader(“headerName”)即可,例如咱们这里要获取 Content-type 的值:
@Test | |
void login(){ | |
.. . | |
when() | |
.log().all().post("http://47.xxx.xxx.133/auth/oauth/token"). | |
then() | |
.log().all().statusCode(200).body("code",equalTo(1)) | |
.extract().response(); | |
System.out.println("返回 Content-Type 是:\n"+response.getHeader("Content-Type")); | |
} |
运行后果:
3)获取 status line——getStatusLine()
@Test | |
void login(){ | |
.. . | |
when() | |
.log().all().post("http://47.xxx.xxx.133/auth/oauth/token"). | |
then() | |
.log().all().statusCode(200).body("code",equalTo(1)) | |
.extract().response(); | |
System.out.println("返回 StatusLine 是:\n"+response.getStatusLine()); | |
} |
运行后果:
4)获取 status code——getStatusCode()
@Test | |
void login(){ | |
.. . | |
when() | |
.log().all().post("http://47.xxx.xxx.133/auth/oauth/token"). | |
then() | |
.log().all().statusCode(200).body("code",equalTo(1)) | |
.extract().response(); | |
System.out.println("返回 StatusCode 是:\n"+response.getStatusCode()); | |
} |
运行后果:
5)获取 cookies——getCookies()、getCookie(“cookieName”)
rest-assured 还为咱们提供了不便的获取 cookie 的办法;因本例中无 cookies 返回,所以仅展现代码语法,有须要的可自行测试或参考官网文档
// Get all cookies as simple name-value pairs | |
Map<String, String> allCookies = response.getCookies(); | |
// Get a single cookie value: | |
String cookieValue = response.getCookie("cookieName"); |
上述这些已简直可满足日常工作所需,如有须要可在官网进一步钻研,官网还提供了获取同名多值的 header 和 cookie 等办法:
相干参考链接:
RESTAssured 官网文档:
https://github.com/rest-assur…
对于想零碎进阶晋升测试开发技能的同学,举荐霍格沃兹测试学院出品的《测试开发从入门到高级实战》零碎进阶班课程。