Rest Assured (1) -- 請求信息設(shè)置

此篇主要詳細講解關(guān)于請求信息的設(shè)置,主要包括,請求Header,請求參數(shù),請求Cookie和請求Content Type信息。

1.請求參數(shù)

Rest Aassured 有兩種方法來傳遞參數(shù):
a. 如果是 GET 請求方法,使用查詢請求參數(shù)queryParam()方法
b. 如果是 POST 請求方法,那么將會使用 form 表單參數(shù)formParam()方法

注意: 雖然不同的請求用不同的方法傳遞參數(shù),但是它也可以根據(jù)HTTP的請求方法來自動判斷使用哪一種方法,所以都可以使用param(key, value)。 當使用param(key, value)方法時,如果是get請求,這個時候param()會被當做queryParam()對待,如果是post請求,param()會被當做formParam()對待。

1.1 get 請求參數(shù)

如果是get請求的參數(shù),使用queryParam(key,value)或者是param(key, value)。

Get請求 栗子
一個參數(shù) http://jsonplaceholder.typicode.com/posts?userId=1 (可用)
    @Test
    public void testGetParam(){
        given()
                .queryParam("userId",1).  //或者是使用 param("userId",1).
        when()
                .get("http://jsonplaceholder.typicode.com/posts").
        then()
                .statusCode(200)
                .body("id", hasItem(1));
    }
1.2 post 請求參數(shù)

如果是post請求的參數(shù),使用formParam(key,value)或者是param(key, value)

Post請求 栗子
URL http://jsonplaceholder.typicode.com/posts
參數(shù) {
???"title": "Test",
???"body": "ABCDE",
???"userId": 1
}

1.2.1 text格式的參數(shù)
可以使用formParam(key,value)或者將參數(shù)存為字符串變量傳給body(String)。

   // 方法1
   @Test
    public void testPostTextParam1(){
        String test= "{\n" +
                "      \"title\": \"Test\",\n" +
                "      \"body\": \"ABCDE\",\n" +
                "      \"userId\": 6\n" +
                "}";
        given()
                .body(test)
                .contentType("text/html").
        when()
                .post("http://jsonplaceholder.typicode.com/posts").
        then()
                .statusCode(201);
    }

    //方法2
    @Test
    public void testPostTextParam2(){
        given()
                .formParam("title","test")
                .formParam("body","ABCDRESS")
                .formParam("userId", 3)
                .contentType("text/html; charset=UTF-8").
        when()
                .post("http://jsonplaceholder.typicode.com/posts").
        then()
                .statusCode(201);
    }

1.2.2 json格式參數(shù)
需要將參數(shù)存為字符串變量傳給body(String)。

    @Test
    public void testPostJsonParam(){
        String test= "{\n" +
                "      \"title\": \"Test\",\n" +
                "      \"body\": \"ABCDE\",\n" +
                "      \"userId\": 6\n" +
                "}";
        given()
                .body(test)
                .contentType("application/json").
        when()
                .post("http://jsonplaceholder.typicode.com/posts").
        then()
                .statusCode(201);
    }

1.3 多個請求參數(shù)
當有過個請求參數(shù)的時候,可以像上邊寫的,寫多個param()方法,或者存為一個字符串。其實也可以將參數(shù)定義為一個集合,存放在集合中,再傳給body方法。

    @Test
    public void testMoreParams(){
        HashMap<String, String> test1 = new HashMap<String,String>();
        test1.put("title","test");
        test1.put("body","Test123");
        test1.put("userId","7");

        given()
                .contentType(ContentType.JSON)
                .body(test1).
        when()
                .post("http://jsonplaceholder.typicode.com/posts").
        then()
                .statusCode(201);
    }

2.請求路徑參數(shù)

一般的,在請求的URL中也會設(shè)定一些參數(shù),對于這些參數(shù)使用的是pathParam(key, value)方法 。變量名用key表示,value就是給key設(shè)定的值,最后在請求的URL中,用變量名key拼接在請求URL中。下邊測試只是一個例子來展示寫法,不能運行。

    @Test
    public void testPathParam(){
        String body = "{\n" +
                "    \"isNext\": true,\n" +
                "    \"isNow\": true\n" +
                "}";
        given().log().all()
                .body(body)
                .pathParam("archId",50)   
                .contentType("application/json")
        when()
                .post("http://localhost:3001/api/XXX/added_archetype/{archId}"). //括號里的就是pathParam的key
        then()
                .statusCode(200);
    }

3.請求Header

在請求頭部分,可以設(shè)置一個或者多個header,或者使用headers來設(shè)置多個key和value。

    @Test
    public void testHeader() {
        given()
                .header("K1", "V1")
                .header("K2", "V21", "V22")
                .headers("K1", "V1", "K2", "V21").
        when()
                .get("http://jsonplaceholder.typicode.com/posts").
        then()
                .statusCode(201);
    }

4.請求Cookie

方法1: 使用cookie(key, value)方法,每個方法對應(yīng)一個cookie值。

    @Test
    public void testCookies1() {
        String body = "{\n" +
                "    \"isNext\": true,\n" +
                "    \"isNow\": true\n" +
                "}";

        given().log().all()
                .body(body)
                .pathParam("archId", 50)
                .contentType("application/json")
                .cookie("session","test")
                .cookie("_pk_ses.50.1fff",1).
        when()
                .post("http://localhost:3001/api/XXX/added_archetype/{archId}").
        then()
                .statusCode(200);
    }

方法2: 直接將所有的cookies用header(key, value)方法傳遞。

    @Test
    public void testCookies2() {
        String body = "{\n" +
                "    \"isNext\": true,\n" +
                "    \"isNow\": true\n" +
                "}";

        given().log().all()
                .body(body)
                .pathParam("archId", 50)
                .contentType("application/json")
                .header("Cookie", "session=test; _pk_ses.50.1fff=1;").
        when()
                .post("http://localhost:3001/api/XXX/added_archetype/{archId}").
        then()
                .statusCode(200);
    }

方法三:

    @Test
    public void testCookies3() {
        String body = "{\n" +
                "    \"isNext\": true,\n" +
                "    \"isNow\": true\n" +
                "}";

        Cookie cookie1 = new Cookie.Builder("session","test").build();
        Cookie cookie2 = new Cookie.Builder("_pk_ses.50.1fff","1").build();
        Cookies cookies = new Cookies(cookie1, cookie2);

        given()
                .body(body)
                .pathParam("archId", 50)
                .contentType("application/json")
                .cookies(cookies).
        when()
                .post("http://localhost:3001/api/XXX/added_archetype/{archId}").
        then()
                .statusCode(200);
    }

5.請求Content Type

根據(jù)不同API的需求傳遞不同的參數(shù)給contentType(類型)方法。

    @Test
    public void testContentType() {
        given()
                .contentType(ContentType.JSON)
                .contentType("application/json")
        when()
                .get("http://jsonplaceholder.typicode.com/XXX").
        then()
                .statusCode(200);
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容