简体   繁体   English

在Jmeter中从JSR223采样器访问HttpServletRequest和响应

[英]Accessing HttpServletRequest and Response from JSR223 sampler within Jmeter

I am using a custom SDK with the following method: 我正在通过以下方法使用自定义SDK:

public Optional<User> retrieveUser(HttpServletRequest request, HttpServletResponse response)

in the jmeter, I am using JSR223 sampler to access this method: 在jmeter中,我正在使用JSR223采样器访问此方法:

all the imports here
.
Optional<User> userProfile = new SdkClass().retrieveUser(request,response);

where request and response are HttpServletRequest and HttpServletResponse 其中请求和响应是HttpServletRequest和HttpServletResponse

In the step before JSR223, I have an HTTPRequest Sampler where the user logs in and a cookie gets created. 在JSR223之前的步骤中,我有一个HTTPRequest Sampler,用户在此处登录并创建cookie。 My question is that how do I pass the request and response to 我的问题是我如何将请求和响应传递给

new SdkClass().retrieveUser(request,response); ?

The request should have the cookie that was created in the previous step 该请求应具有在上一步中创建的cookie

You can use the following code to get previous request and response details 您可以使用以下代码获取先前的请求和响应详细信息

import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;

HTTPSamplerProxy previousSampler = ctx.getPreviousSampler();
CookieManager cookieManager = previousSampler.getCookieManager();
HTTPSampleResult previousResult = (HTTPSampleResult)ctx.getPreviousResult();

See JavaDoc on the aforementioned classes: 请参阅上述类的JavaDoc:

Make sure you add HTTP Cookie Manager to your Test Plan elsewise you'll get null on attempt to access it via getCookieManager() method. 确保将HTTP Cookie Manager添加到测试计划中,否则尝试通过getCookieManager()方法访问它时将获得null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM