nGrinder TestRunner http post json

时间:2024-09-02 19:35:08

s

nGrinder学习笔记 — post请求

https://blog.****.net/meyoung01/article/details/50435881

import HTTPClient.HTTPResponse
import HTTPClient.NVPair
import ch.qos.logback.classic.Level
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.plugin.http.HTTPRequest
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Test
import org.junit.runner.RunWith
import org.slf4j.LoggerFactory import static net.grinder.script.Grinder.grinder
import static org.hamcrest.Matchers.is
import static org.junit.Assert.assertThat /**
* Created by lindows
*/
@RunWith(GrinderRunner)
class LoginDemo {
public static GTest test
public static HTTPRequest request @BeforeProcess
public static void beforeProcess() {
HTTPPluginControl.getConnectionDefaults().timeout = 6000
test = new GTest(1, "192.168.70.206")
request = new HTTPRequest()
test.record(request);
grinder.logger.info("before process.");
} @BeforeThread
public void beforeThread() {
// 只打印错误的log
LoggerFactory.getLogger("worker").setLevel(Level.ERROR) grinder.statistics.delayReports = true;
grinder.logger.info("before thread.");
} private NVPair[] headers() {
return [
new NVPair("Content-type", "application/json;charset=UTF-8")
];
} @Test
public void test1() {
// json字符串
def json = '{"tenant_code":"XXX","user_name":"XX","password":"X","skip_duplicate_entries":true,"type":"0"}';
// 发起请求
HTTPResponse result = request.POST("http://192.XXX.XX.XXX:XXXX/XXX/XX/login", json.getBytes(), headers());
grinder.logger.info(result.getText());
grinder.logger.info(result.getHeader("Content-type")); 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));
}
}
}

end