Skip to content
CodeKruPro

Handle Redirects

58/62

Handle Redirects

REST Assured follows redirects by default. The /redirect/302 endpoint redirects to /users.

You can disable redirect following to inspect the redirect itself:

given().redirects().follow(false).get(path);  // returns the 3xx response itself

Task:

  1. Disable redirects, verify the 302 status and Location header
  2. Follow redirects (default), verify you get the users list

Expected output:

Redirect status: 302
Has Location header: true
Final status: 200
Got users: true

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

  1. Build a request with given(), call redirects().follow(false), send a GET for /redirect/302, and print the status code after Redirect status:.
  2. Print, after the Has Location header: label, whether getHeader() for Location returned something other than null, as a boolean.
  3. Send a plain GET for the same path without touching redirect settings and print its status code after Final status:.
  4. 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 after Got users:.

Common mistakes

  • Expecting to see 302 by default

    Without follow(false) the client has already followed the redirect, and getStatusCode() 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.

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.

Main.java
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
    }
}