Hoping you could help me with what I sure is a glaring oversight on my part:
Since the beginning, I have been using a separate function to perform a check on the response objest and do some logging for every step in my script. However, I have been refactoring the error handling, and now I’m having some trouble getting my function call to complete.
For example, I force a check to fail:
export default function ssoLogin() {
var tenant = `${__ENV.TENANT}`;
var stepNum;
var vuserID = `${__VU}`;
var iterNum = `${__ITER}`;
var response = http.get(`http://` + tenant, {
tags: { name: stepNum + ' - http://' + tenant },
});
checkStatus(response, vuserID, iterNum, 303);
and then pass the response object over to this function:
I can’t explain the weird output if it is happening during check (can’t see anything wrong with it though - are you sure the line numbers match up?).
I’d be more inclined to suspect the culprit being the fail statement, specifically the use of response.body in the message being logged. That could potentially be a very long string with lots of newlines in it, which could explain why you’d get weird output when printing it.
Incidentally, I was just investigating what a generic checkStatus function might look like, and this is where I got to with that:
import http from 'k6/http';
import { check, fail } from 'k6';
export default function () {
let res = http.get("https://httpbin.test.k6.io/html");
checkStatus(res, 200, true, true);
// this will fail:
res = http.get("https://httpbin.test.k6.io/status/500");
checkStatus(res, 200, true, true);
}
function checkStatus(res, expectedStatus, failOnError, printOnError) {
const obj = {};
obj[`HTTP status ${expectedStatus}`] = (r) => r.status === expectedStatus;
const checkResult = check(res, obj);
if (!checkResult) {
if (printOnError) {
console.log("Response: " + res.body);
}
if (failOnError) {
fail(`Received unexpected status code ${res.status} for URL: ${res.url}, expected ${expectedStatus}`)
}
}
}
The only downside is that the response is logged before the associated fail call (it needs to be logged before the call to fail as the latter restarts the iteration).