上一篇文章中介绍了 rest-assured 对返回结果的断言,最后说明了对于 Response 结果导出的需求。可查看往期文章进行查看。
HTTP/1.1 200 OKServer: nginx/1.12.2Date: Mon, 13 Jan 2020 02:15:11 GMTContent-Type: application/json;charset=UTF-8Transfer-Encoding: chunkedExpires: 0Cache-Control: no-cache, no-store, max-age=0, must-revalidateX-XSS-Protection: 1; mode=blockPragma: no-cacheX-Frame-Options: DENYX-Content-Type-Options: nosniffProxy-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" } ... }
@Testvoid 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 字符串的形式保存下来,再一一根据需要获取,具体写法如下:
@Testvoid 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 对象:
@Testvoid 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
@Testvoid 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 的值:
@Testvoid 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()
@Testvoid 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()
@Testvoid 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 pairsMap<String, String> allCookies = response.getCookies();// Get a single cookie value:String cookieValue = response.getCookie("cookieName");
复制代码
上述这些已几乎可满足日常工作所需,如有需要可在官网进一步研究,官网还提供了获取同名多值的 header 和 cookie 等方法
更多学习资料戳下方!!!
https://qrcode.ceba.ceshiren.com/link?name=article&project_id=qrcode&from=infoQ×tamp=1662366626&author=xueqi
评论