nGrinder中快速编写groovy脚本04-发送POST请求

POST请求分类:
java

一、根据是否修改代码,分为两种方式:web

一种是在UI界面添加后自动生成脚本,一种是直接在脚本中添加json

二、根据请求参数的不一样,主要能够分为两种:数组

param为key value格式cookie

body为json格式app

1、经过UI方式发送POST请求–key/value参数ide

经过 UI 设置:脚本 -> 新建脚本 -> 显示高级配置this

当选择了请求方法为POST后,在高级配置中默认会在headers中显示Content-Type为x-www-form-urlencoded,同时,添加key/value格式的params:url


webp

生成代码以下(因为篇幅限制,去掉import部分):spa

@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方式添加参数的效果同样,都是把参数添加在@BeforeProcess,不一样的只是在@Test中使用的是request.POST方法。

2、经过UI方式发送POST请求–json

经过 UI 设置:脚本 -> 新建脚本 -> 显示高级配置

当选择了请求方法为POST后,在高级配置中的headers中选择Content-Type为application/json,同时,添加json字符串:


webp

生成代码以下(因为篇幅限制,去掉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))

}

}

从以上代码能够看出,这种方式是在静态变量中定义了body的内容,在@BeforeProcess中添加json请求头,并在@Test中的request.POST方法中加入了body参数。

关键代码以下:

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())

3、直接在脚本中为POST请求添加json格式的body

使用UI方式添加的json字符串默认是在建立静态变量body的同时添加的;

直接修改脚本的话,就比较灵活,能够在类的任意位置添加,而后在POST请求中调用

(可是要注意变量做用域的问题)

// 定义json字符串

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

//在@Test的POST方法中使用json字符串,同时添加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参数时须要把字符串转成字节数组。

相关文章
相关标签/搜索