溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-08-06 22:15:12 來源:ITPUB博客 閱讀:150 作者:testingbang 欄目:編程語言

POST請(qǐng)求分類:
1、根據(jù)是否修改代碼,分為兩種方式:
一種是在UI界面添加后自動(dòng)生成腳本,一種是直接在腳本中添加
2、根據(jù)請(qǐng)求參數(shù)的不同,主要可以分為兩種:
param為key value格式
body為json格式
一、通過UI方式發(fā)送POST請(qǐng)求–key/value參數(shù)
通過 UI 設(shè)置:腳本 -> 新建腳本 -> 顯示高級(jí)配置
當(dāng)選擇了請(qǐng)求方法為POST后,在高級(jí)配置中默認(rèn)會(huì)在headers中顯示Content-Type為x-www-form-urlencoded,同時(shí),添加key/value格式的params:
nGrinder中快速編寫groovy腳本04-發(fā)送POST請(qǐng)求
生成代碼如下(由于篇幅限制,去掉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方法。

二、通過UI方式發(fā)送POST請(qǐng)求–json
通過 UI 設(shè)置:腳本 -> 新建腳本 -> 顯示高級(jí)配置
當(dāng)選擇了請(qǐng)求方法為POST后,在高級(jí)配置中的headers中選擇Content-Type為application/json,同時(shí),添加json字符串:
nGrinder中快速編寫groovy腳本04-發(fā)送POST請(qǐng)求
生成代碼如下(由于篇幅限制,去掉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)用
(但是要注意變量作用域的問題)
// 定義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ù)組。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI