Let’s say we have to test a sequence of redirects.
First, we create a client
ResteasyClient httpClient = new ResteasyClientBuilder().build();
Then we call the start endpoint (for example POST) and expect to get 302 response code back
Form loginForm = new Form()
.param("username", "tester")
.param("password", "password");
Entity<Form> entity = Entity.form(loginForm);
Response response = httpClient
.target("https://my-super-site.com")
.request(APPLICATION_FORM_URLENCODED)
.post(entity);
assertEquals("Expected Redirect", FOUND.getStatusCode(), response.getStatus());
URI redirectURI = response.getLocation();
Follow redirect with RESTEasy
If we need to verify the whole chain of redirects following one by one then we can create the method followRedirectAndExpectRedirect(URI redirectURI)
.
private URI followRedirectAndExpectRedirect(URI redirectUri) {
log.info("Follow Redirect to {}", redirectUri);
Response response = httpClient.target(redirectUri).request().get();
assertEquals("Expected Redirect", FOUND.getStatusCode(), response.getStatus());
URI nextUri = response.getLocation();
log.info("Redirect to {}", nextUri);
return nextUri;
}
Note: We have to use same client for redirections to keep all the cookies
The complete example for following redirects one by one
@Test
public void redirectTest() {
URI redirectURI = loginAndExpecteRedirect("tester", "password", "https://my-super-site.com");
assertEquals(expectedUrl1, redirectURI.toString());
redirectURI = followRedirectAndExpectRedirect(redirectURI);
assertEquals(expectedUrl2, redirectURI.toString());
redirectURI = followRedirectAndExpectRedirect(redirectURI);
assertEquals(expectedUrl3, redirectURI.toString());
...
redirectURI = followRedirectAndExpectRedirect(redirectURI);
assertEquals(expectedUrlN, redirectURI.toString());
}
private URI loginAndExpectRedirect(String username, String password, String url) {
Form loginForm = new Form()
.param("username", username)
.param("password", password);
Entity<Form> entity = Entity.form(loginForm);
log.info("Call POST to {}", url);
Response response = httpClient.target(url)
.request(APPLICATION_FORM_URLENCODED)
.post(entity);
assertEquals("Expected Redirect", FOUND.getStatusCode(), response.getStatus());
URI nextUri = response.getLocation();
log.info("Redirect to {}", nextUri);
return nextUri;
}
private URI followRedirectAndExpectRedirect(URI redirectUri) {
log.info("Follow Redirect to {}", redirectUri);
Response response = httpClient.target(redirectUri).request().get();
assertEquals("Expected Redirect", FOUND.getStatusCode(), response.getStatus());
URI nextUri = response.getLocation();
log.info("Redirect to {}", nextUri);
return nextUri;
}
Follow redirect with RESTEasy using RedirectionException
If you prefer the “exception style”
private URI followRedirectAndExpectRedirect(URI redirectUri) {
URI nextUri = null;
try {
log.info("Follow Redirect to {}", redirectUri);
httpClient.target(redirectUri).request().get(String.class);
fail("Expected Redirect");
} catch (RedirectionException redirect) {
nextUri = redirect.getLocation();
log.info("Redirect to {}", nextUri);
}
return nextUri;
}
private URI loginAndExpectRedirect(String username, String password, String url) {
Form loginForm = new Form()
.param("username", username)
.param("password", password);
URI redirectURI = null;
Entity<Form> entity = Entity.form(loginForm);
try {
log.info("Call POST to {}", url);
httpClient.target(url)
.request(APPLICATION_FORM_URLENCODED)
.post(entity, String.class);
fail("Expected Redirect");
} catch (RedirectionException redirect) {
redirectURI = redirect.getLocation();
log.info("Redirect to {}", redirectURI);
}
return redirectURI;
}
Note: To use the “exception style” you need to unmarshal the response - that triggers RedirectionException
for redirects