HttpClient測(cè)試框架

簡(jiǎn)介

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

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

HttpClient不是瀏覽器韧衣,它是一個(gè)HTTP通信庫,

HttpClient的主要功能:

實(shí)現(xiàn)了所有 HTTP 的方法(GET购桑、POST畅铭、PUT、HEAD勃蜘、DELETE硕噩、HEAD、OPTIONS 等)

支持 HTTPS 協(xié)議

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

支持自動(dòng)(跳轉(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");

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

? ? ? ? //HttpClient client = new DefaultHttpClient(); 這個(gè)過時(shí)了

? ? ? 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;

? ? //用來存儲(chǔ)cookie信息的變量

? ? private CookieStorestore ;

? ? @BeforeTest

? ? public void beforeTest(){

//會(huì)自動(dòng)找到resource中application文件

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

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

? ? }

@Test

? ? public void testGetCookies()throws IOException {

String result;

? ? ? ? //從配置文件中 拼接測(cè)試的url

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

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

? ? ? ? // 測(cè)試邏輯代碼書寫

? ? ? ? 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;

? ? ? ? //聲明一個(gè)get的方法

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

? ? ? ? //聲明一個(gè)client對(duì)象缭贡,用來進(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;

? ? //用來存儲(chǔ)cookie信息的變量

? ? private CookieStorestore ;

? ? @BeforeTest

? ? public void beforeTest(){

//會(huì)自動(dòng)找到resource中application文件

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

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

? ? }

//獲取cookies信息

? ? @Test

? ? public void testGetCookies()throws IOException {

String result;

? ? ? ? //從配置文件中 拼接測(cè)試的url

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

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

? ? ? ? // 測(cè)試邏輯代碼書寫

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

? ? ? ? //拼接最終的測(cè)試地址

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

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

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

? ? ? ? //聲明一個(gè)方法,這個(gè)方法就是post方法

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

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

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

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

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

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

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

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

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

? ? ? ? post.setEntity(entity);

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

? ? ? ? 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對(duì)象

? ? ? ? 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)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末攒巍,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子荒勇,更是在濱河造成了極大的恐慌柒莉,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,865評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件沽翔,死亡現(xiàn)場(chǎng)離奇詭異兢孝,居然都是意外死亡窿凤,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,296評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門跨蟹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來雳殊,“玉大人,你說我怎么就攤上這事窗轩『煌海” “怎么了?”我有些...
    開封第一講書人閱讀 169,631評(píng)論 0 364
  • 文/不壞的土叔 我叫張陵痢艺,是天一觀的道長(zhǎng)仓洼。 經(jīng)常有香客問我,道長(zhǎng)堤舒,這世上最難降的妖魔是什么色建? 我笑而不...
    開封第一講書人閱讀 60,199評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮植酥,結(jié)果婚禮上镀岛,老公的妹妹穿的比我還像新娘弦牡。我一直安慰自己友驮,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,196評(píng)論 6 398
  • 文/花漫 我一把揭開白布驾锰。 她就那樣靜靜地躺著卸留,像睡著了一般。 火紅的嫁衣襯著肌膚如雪椭豫。 梳的紋絲不亂的頭發(fā)上耻瑟,一...
    開封第一講書人閱讀 52,793評(píng)論 1 314
  • 那天,我揣著相機(jī)與錄音赏酥,去河邊找鬼喳整。 笑死,一個(gè)胖子當(dāng)著我的面吹牛裸扶,可吹牛的內(nèi)容都是我干的框都。 我是一名探鬼主播,決...
    沈念sama閱讀 41,221評(píng)論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼呵晨,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼魏保!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起摸屠,我...
    開封第一講書人閱讀 40,174評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤谓罗,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后季二,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體檩咱,經(jīng)...
    沈念sama閱讀 46,699評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,770評(píng)論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了刻蚯。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蜂筹。...
    茶點(diǎn)故事閱讀 40,918評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖芦倒,靈堂內(nèi)的尸體忽然破棺而出艺挪,到底是詐尸還是另有隱情,我是刑警寧澤兵扬,帶...
    沈念sama閱讀 36,573評(píng)論 5 351
  • 正文 年R本政府宣布麻裳,位于F島的核電站,受9級(jí)特大地震影響器钟,放射性物質(zhì)發(fā)生泄漏津坑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,255評(píng)論 3 336
  • 文/蒙蒙 一傲霸、第九天 我趴在偏房一處隱蔽的房頂上張望疆瑰。 院中可真熱鬧,春花似錦昙啄、人聲如沸穆役。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,749評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽耿币。三九已至,卻和暖如春韧拒,著一層夾襖步出監(jiān)牢的瞬間淹接,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,862評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工叛溢, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留塑悼,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,364評(píng)論 3 379
  • 正文 我出身青樓楷掉,卻偏偏與公主長(zhǎng)得像厢蒜,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子靖诗,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,926評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容