http 帶cookie值的Post請求(關聯測試)


本文主要包含一下無點。

1、如何get請求如何獲取cookie信息。   getCookieStore()

2、如何發送帶cookie信息的post 請求。  setCookieStore(); HttpPost();

3、testng依耐測試,帶cookie信息的post請求需要依耐於獲取cookie信息的get請求。    @Test(dependsOnMethods = {"getTestCookie"})

4、如何使用moco框架構建帶返回cookie信息的get請求和一個帶cookie和json參數的post請求且響應結果也是json格式。

5、如何獲取請求返回為json格式的參數。

6、如何對響應結果進行斷言。

 

java代碼

 1 package com.course.httpclient.cookies;
 2 
 3 import org.apache.http.HttpResponse;
 4 import org.apache.http.client.CookieStore;
 5 import org.apache.http.client.HttpClient;
 6 import org.apache.http.client.methods.HttpGet;
 7 import org.apache.http.client.methods.HttpPost;
 8 import org.apache.http.cookie.Cookie;
 9 import org.apache.http.entity.StringEntity;
10 import org.apache.http.impl.client.DefaultHttpClient;
11 import org.apache.http.util.EntityUtils;
12 import org.json.JSONObject;
13 import org.testng.Assert;
14 import org.testng.annotations.BeforeTest;
15 import org.testng.annotations.Test;
16 import java.io.IOException;
17 import java.util.List;
18 import java.util.Locale;
19 import java.util.ResourceBundle;
20 
21 public class MyCookieForPost {
22     private String url;
23     private ResourceBundle bundle;//用於讀取配置文件
24     private CookieStore store;//用於存儲cookies信息
25 
26     @BeforeTest
27     public void beforeTest() {
28 
29         bundle = ResourceBundle.getBundle("application", Locale.CHINA);
30         //上行代碼用於讀取配置文件,baseName和類在同一目錄的resource文件中
31         url = bundle.getString("test.url");
32         //上行代碼是獲取配置文件中的域名
33     }
34 
35     @Test
36     public void getTestCookie() throws IOException {
37 
38         String result;
39         String uri = bundle.getString("getCookies.uri");
40         //以上代碼是獲取配置文件中的getCookies.uri對應的路徑
41         String testurl = this.url + uri;
42         HttpGet get = new HttpGet(testurl);
43         System.out.println("這是testurl的地址" + testurl);
44 //        HttpClient client = new DefaultHttpClient(); HttpClient無法獲取cookie信息
45         DefaultHttpClient client = new DefaultHttpClient();
46         //創建HttpClient對象,用於執行get請求
47         HttpResponse response = client.execute(get);
48         System.out.println("這是response的值" + response);
49         result = EntityUtils.toString(response.getEntity(), "utf-8");
50         System.out.println(result);
51         //以下代碼是獲取cookie信息
52         this.store = client.getCookieStore();
53         List<Cookie> cookkielist = store.getCookies();
54         for (Cookie cookie : cookkielist) {
55             String name = cookie.getName();
56             String value = cookie.getValue();
57             System.out.println("cookie name=" + name + "  cookie value=" + value);
58         }
59 
60 
61     }
62 
63     @Test(dependsOnMethods = {"getTestCookie"})
64     public void postTestMethods() throws IOException {
65 
66         String uri = bundle.getString("testPostWithCookies.uri");
67         //以上代碼是獲取配置文件中的getCookies.uri對應的路徑
68         String testurl = this.url + uri;
69         DefaultHttpClient client = new DefaultHttpClient();//聲明一個client對象用來進行方法的執行
70         HttpPost post = new HttpPost(testurl);//什么一個post方法
71         JSONObject param = new JSONObject();//添加參數 接口要求post參數的格式是json格式
72         param.put("name", "zhangshan");
73         param.put("age", "18");
74         post.setHeader("Accept-Encoding", "gzip, deflate"); //設置請求信息,設置header
75         post.setHeader("Content-Type", "application/json"); //設置請求信息,設置header
76         StringEntity entity = new StringEntity(param.toString(), "utf-8");//將參數信息添加到方法中
77         post.setEntity(entity);//將post方法和參數綁定在一起
78         String result; //聲明一個對象,進行響應結果的存儲
79         client.setCookieStore(this.store);//設置cookies信息
80         HttpResponse response = client.execute(post);//執行post方法
81         result = EntityUtils.toString(response.getEntity());//獲取響應結果
82         JSONObject resultJson = new JSONObject(result); //將返回的響應結果字符串轉換成json對象
83 
84         String success = resultJson.getString("zhangshan");//獲取響應的參數值
85         int status = resultJson.getInt("status");
86         //    String status = (String) resultJson.get("status");//獲取響應的參數值
87         Assert.assertEquals("成功", success); //處理結果,判斷期望結果和實際結果
88 
89         Assert.assertEquals(1, status);
90 
91     }
92 
93 }

接口信息配置文件application.properties如下

1 test.url=http://127.0.0.1:8888
2 getCookies.uri=/getCookies
3 testPostWithCookies.uri=/post/with/cookies

moco模擬接口信息如下

[
  {
    "description": "這是一個會返回cookies信息的get請求",
    "request": {
      "uri": "/getCookies",
      "method": "get"
    },
    "response": {
      "headers": {
        "Content-Type": "text/html;charset=gbk"
      },
      "cookies": {
        "login": "true"
      },
      "text": "恭喜你獲得cookies信息成功"
    }
  },

  {
    "description": "這是一個帶cookies的post請求",
    "request": {
      "uri": "/post/with/cookies",
      "method": "post",
      "cookies": {
        "login": "true"
      },
      "json": {
        "name": "zhangshan",
        "age": "18"
      }
    },
    "response": {
      "headers": {
        "Content-Type": "text/html;charset=gbk"
      },
      "status": 200,
      "json": {
        "zhangshan": "成功",
        "status": 1
      }
    }
  }
]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM