post请求json格式的范例脚本如下:
import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.plugin.http.HTTPRequest
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.script.GTest
import net.grinder.script.Grinder
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import java.util.Date
import java.util.List
import java.util.ArrayList
import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import org.slf4j.LoggerFactory
import ch.qos.logback.classic.Level
import java.util.Random
/**
* A simple example using the HTTP plugin that shows the retrieval of a
* single page via HTTP.
*
* This script is automatically generated by ngrinder.
*
* @author admin
*/
@RunWith(GrinderRunner)
class TestRunner {
public static GTest test
public static HTTPRequest request
public static NVPair[] headers = []
public static String body
public static Cookie[] cookies = []
public static List<String> LineList //存放参数文件记录
public static def rowNumber //参数行
public static List<String> memberidList //读取会员
public static String memberid //定义会员
public static String transid //定义交易号
@BeforeProcess
public static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000
test = new GTest(1, "xxxx.intra.sit.bes.gw")
request = new HTTPRequest()
// Set header datas
List<NVPair> headerList = new ArrayList<NVPair>()
headerList.add(new NVPair("Content-Type", "application/json"))
headerList.add(new NVPair("charset", "UTF-8"))
headerList.add(new NVPair("token", "NTAyOD"))
headerList.add(new NVPair("tenantId", "2001"))
headerList.add(new NVPair("orgCode", "9999"))
headers = headerList.toArray()
/*读取csv文件内容*/
LineList = new File("./resources/storecode.txt").readLines("GB2312")
memberidList = new File("./resources/member_id2.txt").readLines("GB2312")
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()) }
/**
* new Random().nextInt(lineList.size()) lineList.size()获取csv文件数据行数
* 为了避免获取到空的数据,建议将 lineList.size()替换为正整数,即csv文件中数据的行数.
*
* 如:buyerRowNumber = new Random().nextInt(10)
*/
memberid = memberidList.get(new Random().nextInt(memberidList.size())).toString()
rowNumber = new Random().nextInt(LineList.size())
/*获取csv文件中某一行的第一列数据*/
String OrgCode = LineList.get(rowNumber).toString().split(",")[0]
String StoreCode = LineList.get(rowNumber).toString().split(",")[1]
String FormatCode = LineList.get(rowNumber).toString().split(",")[2]
def transid = (int)Math.random()*2000000 //交易号不允许重复,随机获取
//设置请求的入参--json格式
body = "{\"amount\":\"10000\",\"chanl\":\"wd\",\"consumpTime\":\"2020-08-26 23:59:59\",\"formatCode\":\"${FormatCode}\",\"memberId\":\"${memberid}\",\"orgCode\":\"${OrgCode}\",\"storeCode\":\"${StoreCode}\",\"transactionNo\":\"${transid}\"}"
grinder.logger.info("before thread. init headers and cookies");
}
@Test
public void test(){
HTTPResponse result = request.POST("http://vxxxx/voyager/point/waiter/c/saveTrade", body.getBytes())
//println body
if (result.statusCode == 301 || result.statusCode == 302) {
grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode);
} else {
assertThat(result.statusCode, is(200));
}
}
}