it doesn’t work. i have tried different combinations.
read the code and result in the comments below.
import http from 'k6/http';
import { group, check } from 'k6';
const only422Callback = http.expectedStatuses(422);
export default function () {
group('improper_input_simple_request', function () {
const input_payload = JSON.stringify({
'my': 'input', 'json': 'payload'
});
const json_headers = {
'Content-Type': 'application/json',
'Accept': '*/*'
};
const returned_response = JSON.stringify({
'my': 'expected', 'json': 'response'
});
// try 1: your sugesstion. and this generates following error and also doesn't let other requests in other groups below this be sent.
// ```
// ERRO[0001] ReferenceError: only422callback is not defined
// running at file:///<my-path>/<my-file>.js::115:105(47)
// default at go.k6.io/k6/js/modules/k6.(*K6).Group-fm (native)
// at file:///<my-path>/<my-file>.js::100:6(19)
// at native executor=ramping-vus scenario=default source=stacktrace
// ```
// const response = http.post(url_predict, input_payload, {headers: json_headers, responseCallback: only422callback});
// try 2: same result as above, try 1.
// const response = http.post(url_predict, input_payload, {headers: json_headers}, {responseCallback: only422callback});
// try 3: improving on your suggestion, specifying the status code directly.
// generates no error but doesn't work. i see same % in http_request_failed. and requests in other groups run as they shoud be.
// const response = http.post(url_predict, input_payload, {headers: json_headers, responseCallback: http.expectedStatuses(422)});
// try 4: same result as above, try 3.
// const response = http.post(url_predict, input_payload, {headers: json_headers}, {responseCallback: http.expectedStatuses(422)});
// try 5: generates following error:
// ```
// ERRO[0000] SyntaxError: file:///<my-path>/<my-file>.js: Unexpected token, expected , (128:104)
// 126 | // > 124 | const response = http.post(url_predict, input_payload, {headers: json_headers}, responseCallback: only422callback);
// 127 | // ```
// > 128 | const response = http.post(url_predict, input_payload, {headers: json_headers}, responseCallback: only422callback);
// | ^
// ```
// const response = http.post(url_predict, input_payload, {headers: json_headers}, responseCallback: only422callback);
// try 6: results in same error as above, try
// const response = http.post(url_predict, input_payload, {headers: json_headers}, responseCallback: http.expectedStatuses(422));
// try 7: same result as try 1.
// const response = http.post(url_predict, input_payload, {headers: json_headers}, only422callback);
// try 8: same result as try 3.
// const response = http.post(url_predict, input_payload, {headers: json_headers}, http.expectedStatuses(422));
// try 9: same result as try 1.
// const response = http.post(url_predict, input_payload, {headers: json_headers, only422callback});
// try 10: same result as try 5, but syntax error highligting the period between http and expectedStatuses.
// const response = http.post(url_predict, input_payload, {headers: json_headers, http.expectedStatuses(422)});
check (response, {
'status is 422': response.status === 422,
// 'output contains the pre-defined body'
'output check': response.body === returned_response
});
});
};