1
votes

There are two options under HTTP Request- Redirect Automatically and Follow Redirects. I want to implement behavior when Follow Redirects in unchecked

//CASE 1: HttpClient client = HttpClientBuilder.create().build();
//CASE 2: HttpClient client = HttpClientBuilder.create().disableRedirectHandling().build();

HttpGet get = new HttpGet("${request_link}");
HttpResponse response = client.execute(get);
SampleResult.setResponseData(EntityUtils.toByteArray(response.getEntity()));

I am using "disableRedirectHandling" as shown above in CASE 2 to stop auto re-direct. But I am getting a 200 response code instead of the expected 302.

This is response I get for CASE 1: Response code: 200 Response message: OK Response headers:

But I am expecting below reponse: Response code: 302 Response message: Found

Response headers: HTTP/1.1 302 Found Date: Fri, 11 Jun 2021 00:32:45 GMT Content-Length: 0 Connection: keep-alive Cache-Control: no-cache, no-store, max-age=0, must-revalidate Pragma: no-cache Expires: 0 Location: {redirect location link}

Note: disableRedirectHandling() is disabling Redirect Automatically but what I want to implement is Disable Follow Redirects

Basically I want something of this setRedirectsEnabled(false) OR setMaxRedirects(0)

I tried this but no luck, Still getting re-direct

HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(RequestConfig.custom().setRedirectsEnabled(true).setMaxRedirects(0).build()).build();
1
Could you reproduce the issue against this endpoint httpbin.org/#/Redirects/get_redirect_to If yes - show full code for it.daggett

1 Answers

1
votes

Your CASE 2 looks valid, can it be the case the problem is with your request_link variable?

As per JSR223 Sampler documentation:

The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:

Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.

Or Use Script Text and check Cache compiled script if available property. When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.

So try replacing your HttpGet("${request_link}"); with HttpGet(vars.get("request_link")); where vars stands for JMeterVariables class instance (see Top 8 JMeter Java Classes You Should Be Using with Groovy for more information on this and other JMeter API shortcuts)


In general, any specific reason for not using JMeter's HTTP Request sampler for building the request? It has support of cookie, header, cache manager, embedded resources, authentication, better integration with reporting and so on.