简体   繁体   中英

Accessing HttpServletRequest and Response from JSR223 sampler within Jmeter

I am using a custom SDK with the following method:

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

in the jmeter, I am using JSR223 sampler to access this method:

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

where request and response are HttpServletRequest and HttpServletResponse

In the step before JSR223, I have an HTTPRequest Sampler where the user logs in and a cookie gets created. 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

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:

Make sure you add HTTP Cookie Manager to your Test Plan elsewise you'll get null on attempt to access it via getCookieManager() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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