03-接口自動化框架REST Assured對Response結果的導出獲取

1、前情回顧

上一篇文章中介紹了rest-assured對返回結果的斷言,最後說明了對於Response結果導出的需求,具體可參考文章:
02-接口自動化框架REST Assured的斷言實現

這裏就將繼續研究rest-assuredresponse結果的導出獲取,現有一個登錄接口auth/oauth/token,接口的部分返回值如下:

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"
        }
        ...
 }

2、語法演示

注:因這篇文章主要目的是演示返回值的導出功能,所以演示代碼中將省略請求參數部分,如需瞭解可參考文章
01-初識REST Assured-爲Java量身定做的接口自動化框架

  • extract().path()
    extract是我們獲取返回值的核心,通過它來指明後面需要獲取的內容,path()中的語法同斷言時的JsonPath一致,例如我們現在要獲取user_id,寫法如下:

    @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_iduser_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"));
     }     
    

    運行結果:
    在這裏插入圖片描述

    注:這裏用到的from是rest-assured的,不要導錯包了:import static io.restassured.path.json.JsonPath.from;

  • 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");
    

3、補充說明

上述這些已幾乎可滿足日常工作所需,如有需要可在官網進一步研究,官網還提供了獲取同名多值的headercookie等方法:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章