HttpClient測試框架

簡介

官網(wǎng):http://hc.apache.org/httpclient-3.x/

HttpClient是 Apache Jakarta Common 下的子項目,用來提供高效的、最新的、功能豐富的支持 HTTP 協(xié)議的客戶端編程工具包,并且它支持 HTTP 協(xié)議最新的版本和建議。雖然在 JDK 的 java net包中已經(jīng)提供了訪問 HTTP 協(xié)議的基本功能,但是對于大部分應(yīng)用程序來說,JDK 庫本身提供的功能還不夠豐富和靈活。

HttpClient不是瀏覽器,它是一個HTTP通信庫,

HttpClient的主要功能:

實現(xiàn)了所有 HTTP 的方法(GET、POST、PUT、HEAD、DELETE、HEAD、OPTIONS 等)

支持 HTTPS 協(xié)議

支持代理服務(wù)器(Nginx等)等

支持自動(跳轉(zhuǎn))轉(zhuǎn)向

這些httpclient 要依賴moco框架內(nèi)容

HttpClientDemo

public class MyHttpClient {

? ? @Test

? ? public void test1() throws IOException {

? ? ? ? //用來存放我們的結(jié)果

? ? ? ? String result;

? ? ? ? HttpGet get = new HttpGet("http://www.baidu.com");

? ? ? ? //這個用來執(zhí)行g(shù)et方法

? ? ? ? //HttpClient client = new DefaultHttpClient(); 這個過時了

? ? ? HttpClient client = HttpClientBuilder.create().build();

? ? ? ? HttpResponse response = client.execute(get);

? ? ? ? result = EntityUtils.toString(response.getEntity(),"utf-8");

? ? ? ? System.out.println(result);

? ? }

}

HttpClientForGet

public class MyCookiesForGet {

private Stringurl;

? ? private ResourceBundlebundle;

? ? //用來存儲cookie信息的變量

? ? private CookieStorestore ;

? ? @BeforeTest

? ? public void beforeTest(){

//會自動找到resource中application文件

? ? ? ? bundle = ResourceBundle.getBundle("application", Locale.CHINA);

? ? ? ? url =bundle.getString("test.url");

? ? }

@Test

? ? public void testGetCookies()throws IOException {

String result;

? ? ? ? //從配置文件中 拼接測試的url

? ? ? ? String uri =bundle.getString ("getCookies.uri");

? ? ? ? String testUrl =this.url + uri;

? ? ? ? // 測試邏輯代碼書寫

? ? ? ? HttpGet get =new HttpGet(testUrl);

? ? ? ? //HttpClient 沒有辦法獲取cookie信息Default

// HttpClient client = new DefaultHttpClient();

//CookieStore store = new BasicCookieStore();

? ? ? ? this.store =new BasicCookieStore();

? ? ? ? CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(store).build();

? ? ? // CloseableHttpClient client = HttpClientBuilder.create().build();

? ? ? ? HttpResponse response = client.execute(get);

? ? ? ? result = EntityUtils.toString(response.getEntity(),"utf-8");

? ? ? ? System.out.println(result);

? ? ? ? //獲取cookie信息

//CookieStore? store = client.getCookiesStore();

//

? ? ? ? List cookieList =store.getCookies();

? ? ? ? for (Cookie cookie : cookieList){

String name = cookie.getName();

? ? ? ? ? ? String value = cookie.getValue();

? ? ? ? ? ? System.out.println("cookie name = " + name +";cookie value = "+value);

? ? ? ? }

}

@Test(dependsOnMethods = {"testGetCookies"})

public void testGetWithCookies()throws IOException{

String uri =bundle.getString("test.get.with.cookies");

? ? ? ? String testUrl =this.url + uri;

? ? ? ? //聲明一個get的方法

? ? ? ? HttpGet get =new HttpGet(testUrl);

? ? ? ? //聲明一個client對象,用來進(jìn)行方法的執(zhí)行,并設(shè)置cookies信息

? ? ? ? CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(this.store).build();

? ? ? ? //執(zhí)行g(shù)et方法,并獲得結(jié)果

? ? ? ? CloseableHttpResponse response = client.execute(get);

? ? ? ? //獲取響應(yīng)的狀態(tài)碼,判斷返回的結(jié)果是否符合預(yù)期

? ? ? ? int statusCode = response.getStatusLine().getStatusCode();

? ? ? ? System.out.println("statusCode= "+statusCode);

? ? ? ? if(statusCode ==200){

String result = EntityUtils.toString(response.getEntity(),"utf-8");

? ? ? ? ? ? System.out.println(result);

? ? ? ? }else {

System.out.println("訪問getwithcookies失敗");

? ? ? ? }

}

}

HttpClientForPost

public class MyCookiesForPost {

private Stringurl;

? ? private ResourceBundlebundle;

? ? //用來存儲cookie信息的變量

? ? private CookieStorestore ;

? ? @BeforeTest

? ? public void beforeTest(){

//會自動找到resource中application文件

? ? ? ? bundle = ResourceBundle.getBundle("application", Locale.CHINA);

? ? ? ? url =bundle.getString("test.url");

? ? }

//獲取cookies信息

? ? @Test

? ? public void testGetCookies()throws IOException {

String result;

? ? ? ? //從配置文件中 拼接測試的url

? ? ? ? String uri =bundle.getString ("getCookies.uri");

? ? ? ? String testUrl =this.url + uri;

? ? ? ? // 測試邏輯代碼書寫

? ? ? ? HttpGet get =new HttpGet(testUrl);

? ? ? ? //HttpClient 沒有辦法獲取cookie信息Default

// HttpClient client = new DefaultHttpClient();

//CookieStore store = new BasicCookieStore();

? ? ? ? this.store =new BasicCookieStore();

? ? ? ? CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(store).build();

? ? ? ? // CloseableHttpClient client = HttpClientBuilder.create().build();

? ? ? ? HttpResponse response = client.execute(get);

? ? ? ? result = EntityUtils.toString(response.getEntity(),"utf-8");

? ? ? ? System.out.println(result);

? ? ? ? //獲取cookie信息

//CookieStore? store = client.getCookiesStore();

? ? ? ? List cookieList =store.getCookies();

? ? ? ? for (Cookie cookie : cookieList){

String name = cookie.getName();

? ? ? ? ? ? String value = cookie.getValue();

? ? ? ? ? ? System.out.println("cookie name = " + name +";cookie value = "+value);

? ? ? ? }

}

@Test(dependsOnMethods = {"testGetCookies"})

public void testPostMethod()throws IOException {

String uri =bundle.getString("test.post.wtih.cookies");

? ? ? ? //拼接最終的測試地址

? ? ? ? String testUrl =this.url + uri;

? ? ? ? //聲明一個Client對象,用來進(jìn)行方法的執(zhí)行

? ? ? ? CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(this.store).build();

? ? ? ? //聲明一個方法,這個方法就是post方法

? ? ? ? HttpPost post =new HttpPost(testUrl);

? ? ? ? //添加參數(shù);這個也可以優(yōu)化,用例里面很多參數(shù)

? ? ? ? JSONObject param =new JSONObject();

? ? ? ? param.put("name", "jane");

? ? ? ? param.put("age", "18");

? ? ? ? //設(shè)置請求頭信息 設(shè)置header信息;若公用的,可以優(yōu)化到一個header工具類方法中

? ? ? ? post.setHeader("content-type", "application/json");

? ? ? ? // 將參數(shù)信息添加到方法中

? ? ? ? StringEntity entity =new StringEntity(param.toString(), "utf-8");

? ? ? ? post.setEntity(entity);

? ? ? ? //聲明一個對象來進(jìn)行響應(yīng)結(jié)果的存儲

? ? ? ? String result;

? ? ? ? //設(shè)置cookies信息

//client.setCookieStore(this.store);

//執(zhí)行post方法

? ? ? ? HttpResponse response = client.execute(post);

? ? ? ? //獲取響應(yīng)結(jié)果

? ? ? ? result = EntityUtils.toString(response.getEntity(), "utf-8");

? ? ? ? System.out.println(result);

? ? ? ? //處理結(jié)果,判斷返回結(jié)果是否符合預(yù)期

//將返回的響應(yīng)結(jié)果字符串轉(zhuǎn)化為json對象

? ? ? ? JSONObject resultJson =new JSONObject(result);

? ? ? ? //具體判斷返回結(jié)果的值

//獲取到結(jié)果值

? ? ? ? String name = (String)resultJson.get("name");

? ? ? ? String status = (String)resultJson.get("status");

? ? ? ? Assert.assertEquals("success", name);

? ? ? ? Assert.assertEquals("1", status);

? ? }

}

?著作權(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ù)。

相關(guān)閱讀更多精彩內(nèi)容

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