nGrinder中快速編寫(xiě)groovy腳本04-發(fā)送POST請(qǐng)求

點(diǎn)擊鏈接加入QQ群229390571(免費(fèi)公開(kāi)課、視頻應(yīng)有盡有):https://jq.qq.com/?_wv=1027&k=5rbudQa

POST請(qǐng)求分類:

1、根據(jù)是否修改代碼,分為兩種方式:

一種是在UI界面添加后自動(dòng)生成腳本,一種是直接在腳本中添加

2、根據(jù)請(qǐng)求參數(shù)的不同,主要可以分為兩種:

param為key value格式

body為json格式

一、通過(guò)UI方式發(fā)送POST請(qǐng)求–key/value參數(shù)

通過(guò) UI 設(shè)置:腳本 -> 新建腳本 -> 顯示高級(jí)配置

當(dāng)選擇了請(qǐng)求方法為POST后,在高級(jí)配置中默認(rèn)會(huì)在headers中顯示Content-Type為x-www-form-urlencoded,同時(shí),添加key/value格式的params:


生成代碼如下(由于篇幅限制,去掉import部分):

@RunWith(GrinderRunner)

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

class TestRunner {

public static GTest test

public static HTTPRequest request

public static NVPair[] headers = []

public static NVPair[] params = []

public static Cookie[] cookies = []

@BeforeProcess

public static void beforeProcess() {

HTTPPluginControl.getConnectionDefaults().timeout = 6000

test = new GTest(1, "www.baidu.com")

request = new HTTPRequest()

// Set header datas

List<NVPair> headerList = new ArrayList<NVPair>()

headerList.add(new NVPair("Content-Type", "application/x-www-form-urlencoded"))

headerList.add(new NVPair("Connection", "keep-alive"))

headers = headerList.toArray()

// Set param datas

List<NVPair> paramList = new ArrayList<NVPair>()

paramList.add(new NVPair("name", "jing"))

paramList.add(new NVPair("age", "18"))

paramList.add(new NVPair("desc", "beauty"))

params = paramList.toArray()

// Set cookie datas

List<Cookie> cookieList = new ArrayList<Cookie>()

cookieList.add(new Cookie("token", "xxxxxxxx", "www.baidu.com", "", new Date(32503647599000L), false))

cookies = cookieList.toArray()

grinder.logger.info("before process.");

}

@BeforeThread

public void beforeThread() {

test.record(this, "test")

grinder.statistics.delayReports=true;

grinder.logger.info("before thread.");

}

@Before

public void before() {

request.setHeaders(headers)

cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }

grinder.logger.info("before thread. init headers and cookies");

}

@Test

public void test(){

HTTPResponse result = request.POST("https://www.baidu.com", params)

assertThat(result.statusCode, is(200))

assertThat(result.getText(), containsString("jing"))

}

}

從以上代碼可以看出,這種方式跟之前使用GET方式添加參數(shù)的效果一樣,都是把參數(shù)添加在@BeforeProcess,不同的只是在@Test中使用的是request.POST方法。

二、通過(guò)UI方式發(fā)送POST請(qǐng)求–json

通過(guò) UI 設(shè)置:腳本 -> 新建腳本 -> 顯示高級(jí)配置

當(dāng)選擇了請(qǐng)求方法為POST后,在高級(jí)配置中的headers中選擇Content-Type為application/json,同時(shí),添加json字符串:


生成代碼如下(由于篇幅限制,去掉import部分):

@RunWith(GrinderRunner)

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

class TestRunner {

public static GTest test

public static HTTPRequest request

public static NVPair[] headers = []

public static String body = "{\"name\":\"jing\",\"comment\":\"hello\"}"

public static Cookie[] cookies = []

@BeforeProcess

public static void beforeProcess() {

HTTPPluginControl.getConnectionDefaults().timeout = 6000

test = new GTest(1, "www.baidu.com")

request = new HTTPRequest()

// Set header datas

List<NVPair> headerList = new ArrayList<NVPair>()

headerList.add(new NVPair("Content-Type", "application/json"))

headers = headerList.toArray()

grinder.logger.info("before process.");

}

@BeforeThread

public void beforeThread() {

test.record(this, "test")

grinder.statistics.delayReports=true;

grinder.logger.info("before thread.");

}

@Before

public void before() {

request.setHeaders(headers)

cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }

grinder.logger.info("before thread. init headers and cookies");

}

@Test

public void test(){

HTTPResponse result = request.POST("https://www.baidu.com", body.getBytes())

assertThat(result.statusCode, is(200))

}

}

從以上代碼可以看出,這種方式是在靜態(tài)變量中定義了body的內(nèi)容,在@BeforeProcess中添加json請(qǐng)求頭,并在@Test中的request.POST方法中加入了body參數(shù)。

關(guān)鍵代碼如下:

public static String body = "{\"name\":\"jing\",\"comment\":\"hello\"}"

headerList.add(new NVPair("Content-Type", "application/json"))

HTTPResponse result = request.POST("https://www.baidu.com", body.getBytes())

三、直接在腳本中為POST請(qǐng)求添加json格式的body

使用UI方式添加的json字符串默認(rèn)是在創(chuàng)建靜態(tài)變量body的同時(shí)添加的;

直接修改腳本的話,就比較靈活,可以在類的任意位置添加,然后在POST請(qǐng)求中調(diào)用

(但是要注意變量作用域的問(wèn)題)

// 定義json字符串

String jsonStr = '{"name": "jing"}';

//在@Test的POST方法中使用json字符串,同時(shí)添加header

request.POST("https://www.baidu.com", jsonStr.getBytes(), [

? ? new NVPair("Content-Type", "application/json"

] as NVPair[])

其中,需要注意的是:

POST(java.lang.String uri, byte[] data)

此方法中接收body參數(shù)時(shí)需要把字符串轉(zhuǎn)成字節(jié)數(shù)組。


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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