follow redirect

1,966 views
Skip to first unread message

sam

unread,
Jun 4, 2012, 1:45:47 AM6/4/12
to REST assured
Hello!

I am using version 1.6.2. My test is unable to follow redirect.
[<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>1.6.2</version>
</dependency>]


RestAssured.followRedirects = false; gives compilation error

I also tried
RequestSpecification spec = new
RequestSpecBuilder().setConfig(newConfig().redirect(redirectConfig().followRedirects(true).and().maxRedirects(20))).build();

given().spec(spec)

and..

given().
redirects().max(12).and().redirects().follow(true).


but doesn't seam to redirect the request (gives 302 but doesn't make
request to the redirected url)

I can make out from this discussion (https://groups.google.com/group/
rest-assured/browse_thread/thread/7a5ad8a2449e775a/d4f223659fdf9307?
lnk=gst&q=followRedirects#d4f223659fdf9307) that it has been fixed.

Am I using some version with doesn't support follow redirect? I will
appreciate any help on this.

I will appreciate any help on this.

Many Thanks,
Sam

Johan Haleby

unread,
Jun 4, 2012, 2:14:29 AM6/4/12
to rest-a...@googlegroups.com
"RestAssured.followRedirects = false" is invalid. Where did you read that you could do this?

There are multiple test cases that demonstrates the usage of redirects in the "RedirectITest" in the source code. Have a look at them and see what you may be doing wrong.

Regards,
/Johan

sambodhi sagi

unread,
Jun 4, 2012, 5:06:11 AM6/4/12
to rest-a...@googlegroups.com
Hi Johan,

Thanks for your reply.

I checked format from this Test class only. And I tried almost all the
formats mentioned in the discussion. My test currently looks like
this:

given().
redirects().max(100).and().redirects().follow(true).redirects().allowCircular(true).redirects().rejectRelative(false).
when().
post("/oauth/authorize?scope=read&response_type=code&redirect_uri=http%3A%2F%2Flocalhost:8080%2Foauth2-client-demo%2Fredirect&client_id=client_789&client_secret=secret_789&user_oauth_approval=true").cookies();

Also tried
RestAssured.config =
config().redirect(redirectConfig().followRedirects(true).and().maxRedirects(0));

And at server side, I redirect to a different
URL(http://localhost:8080/oauth2-client-demo/app/sso/SSOService) which
redirects back to this servlet
(http://localhost:8080/oauth/authorize). I was hoping that this
configuration would allow me to get final response from this servlet.
Can you please point out if am wrong anywhere?

Rest assurred logs attached.

Thanks,
Sam
ra.txt

Johan Haleby

unread,
Jun 4, 2012, 7:06:57 AM6/4/12
to rest-a...@googlegroups.com
The test cases works at my place and this is what I get back in Wireshark as the response to "/redirect" when running "RedirectITest#followsRedirectsByDefault".

Inline image 1

and Rest Assured does indeed redirect to the URL specified in the Location header. So you may want to check that the response you receive looks kind of like this one (with a location header and status code 302).  Also are you using Maven? If not you may also check that you have the correct version of HTTP Client in class path and that it's used.

Regards,
/Johan
image.png

sambodhi sagi

unread,
Jun 4, 2012, 5:57:47 PM6/4/12
to rest-a...@googlegroups.com
Hi Johan,

I am using maven, httpclient 4.1.3 with rest-assured 1.6.2.

In response, I get status code 301 and in location
"Location: http://localhost:8080/oauth2-client-demo/app/sso/SSOService[\r][\n]

But the request never reaches 'http://localhost:8080/oauth2-client-demo/app/sso/SSOService' . Looks like it never redirects.

Thanks,
Sam
image.png

Johan Haleby

unread,
Jun 5, 2012, 2:15:45 AM6/5/12
to rest-a...@googlegroups.com
Ok so perhaps HTTP Client doesn't automatically redirect for 301 responses for POST? I found this in RFC 2616:

If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

I would suggest that you google a bit about this (and please tell us your findings) and if you can't find anything you could write a "redirect filter" that extracts the location header and forwards the request to that URL.

Regards,
/Johan
image.png

Johan Haleby

unread,
Feb 28, 2013, 2:06:55 AM2/28/13
to rest-a...@googlegroups.com
Well RA cannot follow redirects unless the status code is "302" and a location header is set.

/Johan

On Wed, Feb 27, 2013 at 8:03 PM, Tyler Ebel <webelem...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

filip.g...@intertec.io

unread,
Feb 1, 2018, 8:14:39 AM2/1/18
to REST assured
Hi Johan,

I have a configuration setup like this:

.redirect(redirectConfig().followRedirects(true).and().maxRedirects(10))

So when I am making a request the request has one or two redirects, I am trying to find a way to get the response from all redirects?

Is that possible?

Johan Haleby

unread,
Feb 1, 2018, 8:16:27 AM2/1/18
to rest-a...@googlegroups.com
No this is not something that is built in. You could build a custom filter that follows redirects manually (disable the build in redirects first) which also captures each intermediary response in a data structure.

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

filip.g...@intertec.io

unread,
Feb 1, 2018, 10:49:20 AM2/1/18
to REST assured
Ok, 

.redirect(redirectConfig().followRedirects(true).and().maxRedirects(1))

With the code above I will get the response from the first or the second request/redirect?
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.

Johan Haleby

unread,
Feb 1, 2018, 2:04:04 PM2/1/18
to rest-a...@googlegroups.com
No, you have to disable redirects (.redirects().follow(false)) and then create a filter that looks at the location header and stores the response somewhere for each "redirect response".

To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.

filip.g...@intertec.io

unread,
Feb 2, 2018, 5:24:59 AM2/2/18
to REST assured
Hi Johan,

I am not quite sure how to do that but I tried the code below, I can see everything that I need in the console output but the test fails with this exception: org.apache.http.client.ClientProtocolException. Also when I remove 
.redirect(redirectConfig().maxRedirects(1)) the test doesn't fail but it goes all to the end with the redirects, that's something I don't want, I just need the response from the first redirect. Any advice?



protected RequestSpecification specRedirect() {
RestAssuredConfig restAssuredConfig = RestAssuredConfig.config()
.redirect(redirectConfig().maxRedirects(1))
.objectMapperConfig(new ObjectMapperConfig().jackson2ObjectMapperFactory(
(aClass, s) -> new ObjectMapper().configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
));
RestAssured.useRelaxedHTTPSValidation();
restAssuredConfig = restAssuredConfig.httpClient(httpClientConfig().setParam(PARAMETER_NAME, TIMEOUT_VALUE));
RequestSpecification spec = new TestSpecificationImpl(
new RequestSpecBuilder()
.setConfig(restAssuredConfig)
.setBaseUri(this.baseURI)
.setBasePath(this.basePath)
.addFilters(this.filters)
.addHeaders(this.headers)
.build(),
new ResponseSpecBuilder()
.build()
).getRequestSpecification();

if (cookies != null && useCookies) {
spec.cookies(cookies);
}
return spec;
}

Johan Haleby

unread,
Feb 2, 2018, 5:30:42 AM2/2/18
to rest-a...@googlegroups.com
Setting maxRedirects(1) will only allow one redirect, setting it follow(false) will not follow the redirect at all which is probably what you want. So instead letting Apache HTTP Client following the redirect automatically YOU will have to do it manually. I.e. extracting the URL from the Location and make a new HTTP request to that location. I would do that from a filter that can implement this process.

To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.

filip.g...@intertec.io

unread,
Feb 2, 2018, 8:51:52 AM2/2/18
to REST assured
No, 

That is the problem actually, when I set it to .redirect(redirectConfig().maxRedirects(1) I want it allow to follow one redirect only, but it doesn't do that, I get an exception like this:

Caused by: org.apache.http.client.RedirectException: Maximum redirects (1) exceeded.

Johan Haleby

unread,
Feb 2, 2018, 2:03:33 PM2/2/18
to rest-a...@googlegroups.com
maxRedirects controls the behavior of Apache HttpClient and it throws an exception if more than 1 attempt is required to complete the request flow. Might be a bit counter intuitive but that's how it works. To achieve what you're after you need to follow the redirects yourself as described above.

To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages