How to have k6 not treat http 422 response as a failed request?

@naveenmarthala

import http from 'k6/http';
import { check } from 'k6';

const only422Callback = http.expectedStatuses(422);

export default function() {
  const json_headers = { "someheader": "value" }
  const response = http.post("https://httpbin.test.k6.io/status/422", "something", { headers: json_headers, responseCallback: only422Callback});
  check(response, {
    'status is 422': response.status === 422,
  });

};

definitely works for me for all kind of versions of k6. The output shows

     ✓ status is 422

     checks.........................: 100.00% ✓ 1        ✗ 0
     data_received..................: 5.7 kB  11 kB/s
     data_sent......................: 585 B   1.1 kB/s
     http_req_blocked...............: avg=416.66ms min=416.66ms med=416.66ms max=416.66ms p(90)=416.66ms p(95)=416.66ms
     http_req_connecting............: avg=117.26ms min=117.26ms med=117.26ms max=117.26ms p(90)=117.26ms p(95)=117.26ms
     http_req_duration..............: avg=120.27ms min=120.27ms med=120.27ms max=120.27ms p(90)=120.27ms p(95)=120.27ms
       { expected_response:true }...: avg=120.27ms min=120.27ms med=120.27ms max=120.27ms p(90)=120.27ms p(95)=120.27ms
     http_req_failed................: 0.00%   ✓ 0        ✗ 1
     http_req_receiving.............: avg=140.52µs min=140.52µs med=140.52µs max=140.52µs p(90)=140.52µs p(95)=140.52µs
     http_req_sending...............: avg=135.28µs min=135.28µs med=135.28µs max=135.28µs p(90)=135.28µs p(95)=135.28µs
     http_req_tls_handshaking.......: avg=266.56ms min=266.56ms med=266.56ms max=266.56ms p(90)=266.56ms p(95)=266.56ms
     http_req_waiting...............: avg=120ms    min=120ms    med=120ms    max=120ms    p(90)=120ms    p(95)=120ms
     http_reqs......................: 1       1.851053/s
     iteration_duration.............: avg=537.48ms min=537.48ms med=537.48ms max=537.48ms p(90)=537.48ms p(95)=537.48ms
     iterations.....................: 1       1.851053/s

which means that:

  1. ✓ status is 422 all checks with that name passed os the status code was 422
  2. http_req_failed................: 0.00% ✓ 0 ✗ 1 0% means that there were 0% failed with 0 failed and 1 not failed. Unfortunately we noticed later that this double negative isn’t exactly great for users understanding of what is happening.

The URL used will return w/e status you give it as the last part of the path and you can test around it to make certain that you understand how this works. From the looks of it either:

  1. you are making some small mistake making the script
  2. you are having a problem with reading the output due to the unfortunate UX issue we have :frowning:

@BobRuub as far as I can see your change shouldn’t do anything although this is javascript so I would expect that depending on what login_params is … a comma might make a difference :person_shrugging: . If you want you can provide and httpbin.test.k6.io based full example so I can dig further. I at least can’t reproduce it breaking without a comma :person_shrugging: