发布于2021-06-08 08:55 阅读(1389) 评论(0) 点赞(27) 收藏(0)
在实际压测工作中 Post 请求分为两种最常见传参情况:
第一种是通过 key-value
方式传参数
Content-Type:application/x-www-form-urlencoded
第二种是通过 Json 方式传参数
Content-Type:application/json
前置条件是大家源码部署成功的,这样方面咱们直接在源码的脚本位置添加咱们调试的脚本,下面咱们使用两种方式做例子分别介绍。
在模拟请求的服务端的 SpringBoot 工程的 Controller 层添加如下代码:
@PostMapping("/findinfo")
@ResponseBody
public List<UserTable> findUserPost(UserTable userInfo) {
List<UserTable> UserInfo = userService.findinfo(userInfo);
return UserInfo;
}
如果上面代码看不明白请参考:
参考代码:
import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
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.Before
import org.junit.Test
import org.junit.runner.RunWith
import static net.grinder.script.Grinder.grinder
import static org.hamcrest.Matchers.is
// import static net.grinder.util.GrinderUtils.
* // You can use this if you're using nGrinder after 3.2.3
import static org.junit.Assert.assertThat
/**
* @Title: PostDemo
* @Description: post请求
* @author 7DGroup
* @date 2019/10/24 / 12:22
*/
@RunWith(GrinderRunner)
class PostDemo {
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, "localhost:8888")
request = new HTTPRequest()
// Set header datas
List<NVPair> headerList = new ArrayList<NVPair>()
//POST key/value格式的params
headerList.add(new NVPair("Content-Type", "application/x-www-form-urlencoded"))
headers = headerList.toArray()
// Set param datas
List<NVPair> paramList = new ArrayList<NVPair>()
paramList.add(new NVPair("username", "600128"))
params = paramList.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.GET("http://localhost:8888/findinfo", params)
def text = result.getText()
grinder.logger.info(text)
assertThat(result.statusCode, is(200))
}
}
注意别忘记修改启动参数:
-javaagent:D:\maven\repository\net\sf\grinder\grinder-dcr-agent\3.9.1\grinder-dcr-agent-3.9.1.jar
点击运行:
运行结果:
在测试前,先模拟可以发送 Json 请求的服务端,在 Controler 层中增加一个方法并且使用可以解析 Json 方法的注解为:@RequestBody
具体代码为:
**
* json请求
* @param userInfo
* @return
*/
@PostMapping("/findinfoJson")
@ResponseBody
public List<UserTable> findUserPostJson(@RequestBody UserTable userInfo) {
List<UserTable> UserInfo = userService.findinfo(userInfo);
return UserInfo;
}
通过上面代码调整即可接受 Json 格式的请求,以下通过源码编写脚本;
import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair
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.Before
import org.junit.Test
import org.junit.runner.RunWith
import static net.grinder.script.Grinder.grinder
import static org.hamcrest.Matchers.is
// import static net.grinder.util.GrinderUtils.*
// You can use this if you're using nGrinder after 3.2.3
import static org.junit.Assert.assertThat
/**
* @Title: PostDemo
* @Description: json请求
* @author 7DGroup
* @date 2019/10/24 / 12:22
*/
@RunWith(GrinderRunner)
class PostDemo {
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, "localhost:8888")
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() {
String url = "http://localhost:8888/findinfoJson"
String parambody = "{\"username\":\"600128\"}"
HTTPResponse result = request.POST(url, parambody.getBytes())
//显示结果
def text = result.getText()
grinder.logger.info(text)
assertThat(result.statusCode, is(200))
}
}
运行结果:
通过点击方法的 post 请求可以看出源码中支持那些方式传参:
HTTPResponse result = request.POST(url, parambody.getBytes())
注意 key-value
与 Json 在传参的时候注意 Content-Type 的值就行。
扩展知识:
原文链接:https://blog.csdn.net/zuozewei/article/details/117576128
作者:快起来搬砖啦
链接:http://www.javaheidong.com/blog/article/219008/5448761035514f4cc508/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!