美文网首页
Rest Assured (1) -- 请求信息设置

Rest Assured (1) -- 请求信息设置

作者: 爱sa笑的菇凉 | 来源:发表于2020-09-21 17:52 被阅读0次

    此篇主要详细讲解关于请求信息的设置,主要包括,请求Header,请求参数,请求Cookie和请求Content Type信息。

    1.请求参数

    Rest Aassured 有两种方法来传递参数:
    a. 如果是 GET 请求方法,使用查询请求参数queryParam()方法
    b. 如果是 POST 请求方法,那么将会使用 form 表单参数formParam()方法

    注意: 虽然不同的请求用不同的方法传递参数,但是它也可以根据HTTP的请求方法来自动判断使用哪一种方法,所以都可以使用param(key, value)。 当使用param(key, value)方法时,如果是get请求,这个时候param()会被当做queryParam()对待,如果是post请求,param()会被当做formParam()对待。

    1.1 get 请求参数

    如果是get请求的参数,使用queryParam(key,value)或者是param(key, value)

    Get请求 栗子
    一个参数 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 请求参数

    如果是post请求的参数,使用formParam(key,value)或者是param(key, value)

    Post请求 栗子
    URL http://jsonplaceholder.typicode.com/posts
    参数 {
       "title": "Test",
       "body": "ABCDE",
       "userId": 1
    }

    1.2.1 text格式的参数
    可以使用formParam(key,value)或者将参数存为字符串变量传给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格式参数
    需要将参数存为字符串变量传给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 多个请求参数
    当有过个请求参数的时候,可以像上边写的,写多个param()方法,或者存为一个字符串。其实也可以将参数定义为一个集合,存放在集合中,再传给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.请求路径参数

    一般的,在请求的URL中也会设定一些参数,对于这些参数使用的是pathParam(key, value)方法 。变量名用key表示,value就是给key设定的值,最后在请求的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

    在请求头部分,可以设置一个或者多个header,或者使用headers来设置多个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)方法,每个方法对应一个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

    根据不同API的需求传递不同的参数给contentType(类型)方法。

        @Test
        public void testContentType() {
            given()
                    .contentType(ContentType.JSON)
                    .contentType("application/json")
            when()
                    .get("http://jsonplaceholder.typicode.com/XXX").
            then()
                    .statusCode(200);
        }
    

    相关文章

      网友评论

          本文标题:Rest Assured (1) -- 请求信息设置

          本文链接:https://www.haomeiwen.com/subject/zrvjyktx.html