I've always used Postman in the past to test the results on a per-request base, but for this scenario I need it to verify the Xth result in a series of requests.
Basically I need to test if the rate limiting is working correctly so the scenario would be something like:
Request 1 -> Expected response has a 200 status code.
Request 2 -> Expected response has a 200 status code.
Request 3 -> Expected response has a 429 status code.
Delay of 1 min
Request 4 -> Expected response has a 200 status code.
Any way to do that?
I've tried turning it into a js script, as I found in this topic, but something doesn't seem to work yet:
pm.test("Rate limiter test", function () {
OkTest();
setTimeout(OkTest, 10);
setTimeout(TooManyRequestsTest, 10);
setTimeout(OkTest, 5000);
});
pm.test("Rate fail limiter test", function () {
OkTest();
setTimeout(OkTest, 10);
setTimeout(OkTest, 10);
setTimeout(OkTest, 5000);
});
function OkTest() {
pm.sendRequest(pm.request.url, function (err, res) {
tests['status code should be 200']= res.code ===200;
});
}
function TooManyRequestsTest() {
pm.sendRequest(pm.request.url, function (err, res) {
tests['status code should be 429']= res.code ===429;
});
}
In this scenario I'd expect one of the tests to fail as the endpoint is configured to allow a max of 2 requests per 5 seconds. All is green though. Manually invoking the endpoint does show me a 429 response on the 3rd request if I quickly run several requests.