http-redirectrequestk6

Running k6 script for load tests returns an error


I'm first time user of k6 and I've already manage to get an error when running the script:

"Request Failed [33merror[0m="Get https:///: stopped after 0 redirects"

Script k6.js:

import http from "k6/http";
import { sleep } from "k6";

export let options = {
 stages: [
    { duration: "30s", target: 20 },
    { duration: "1m30s", target: 10  },
    { duration: "20s", target: 0 },
  ]
};

export default function() {
  let res = http.get("https://<our_page_URL>/");
  check(res, {
    "status code MUST be 200": (res) => res.status == 200,
  }) || fail("status code was *not* 200");
  sleep(1);
}

Why do I get this error and what is the solution?


Solution

  • You have to set up the maxRedirects option; maxRedirects is the maximum number of HTTP redirects that k6 will follow before giving up on a request and erroring out with ("Get $PATH stopped after $MAX_REDIRECT_VALUE redirects")

    You can pass the option as CLI argument or script options. More about this option at https://docs.k6.io/docs/options

    export let options = {
        // Max redirects to follow (default is 10)
        maxRedirects: 10
    };
    

    The default is 10, so there is likely a bug skipping the default value being assigned.

    This is a redirect example to test how it works.

    import http from "k6/http";
    import {check} from "k6";
    
    export let options = {
        // Max redirects to follow (default is 10)
        maxRedirects: 5
    };
    
    export default function() {
        // If redirecting more than options.maxRedirects times, the last response will be returned
        let res = http.get("https://httpbin.org/redirect/6");
        check(res, {
            "is status 302": (r) => r.status === 302
        });
    
        // The number of redirects to follow can be controlled on a per-request level as well
        res = http.get("https://httpbin.org/redirect/1", {redirects: 1});
        console.log(res.status);
        check(res, {
            "is status 200": (r) => r.status === 200,
            "url is correct": (r) => r.url === "https://httpbin.org/get"
        });
    }