About this lesson
REST Assured redirects: inspect a 302 with follow(false), then let it through
Learn REST AssuredAdvancedAdvanced Topics
How it works
A redirect is a 3xx status plus a Location header naming where the client should go next. REST Assured, through Apache HttpClient, follows redirects on a GET by default. You request /redirect/302 and get back a 200 with the users array; the 302 itself is never visible to your code. That is convenient for most tests and useless when the redirect is the thing under test.
redirects().follow(false) on the request specification turns following off for that request. The response is then the redirect: status 302, an empty body, and a Location header. On testkru that header holds an absolute URL ending in /api/users, not a bare relative path. getHeader() returns its value, or null if the header is missing, which makes a presence check a simple comparison with null.
The testkru endpoint accepts 301, 302, 303 and 307 in the path and answers 400 for any other code, according to the API source. Every variant points at the user list. As an assertion, then() with statusCode(302) and header() using the endsWith() matcher passes on the unfollowed request.
About the assignment
Look at the raw redirect first, then make the same call with default behaviour and confirm it lands on the user list.
What you will do
- Build a request with
given(), callredirects().follow(false), send a GET for/redirect/302, and print the status code afterRedirect status:. - Print, after the
Has Location header:label, whethergetHeader()forLocationreturned something other thannull, as a boolean. - Send a plain GET for the same path without touching redirect settings and print its status code after
Final status:. - Read the user names from that final body with
jsonPath().getList(), check that the list contains a known user such as John Doe, and print the boolean afterGot users:.
Common mistakes
Expecting to see 302 by default
Without
follow(false)the client has already followed the redirect, andgetStatusCode()reports the final 200. The redirect leaves no trace in the response.Comparing Location to a relative path
The header carries the full URL with scheme and host. An exact match against a path fails; compare with
endsWith()or check only that it is present.Printing the header instead of a boolean
Printing the result of
getHeader()shows the URL. The task wants true or false, so print the outcome of the null comparison.
In a real test suite
Redirect tests protect login flows, HTTP to HTTPS upgrades, moved resources and short links. A JUnit 5 test usually asserts the exact status, because 301 and 302 mean different things to caches and search engines, and checks that Location points where it should, often with a separate test that the target itself responds.
Related lessons
- Custom Response HeadersReads response headers with
getHeader(), used here forLocation. - Extract from Books with TagsTurns a JSON array field into a Java
ListwithgetList(). - Validate 200 OKAsserts a status code with
statusCode(), the core check for a redirect.
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// Part 1: Disable redirect following
// TODO: given().redirects().follow(false).get("/redirect/302")
// Print status and check if Location header exists
// Part 2: Follow redirect (default behavior)
// TODO: given().get("/redirect/302")
// Print final status and verify response has user names
}
}