0

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.

FoxHound
  • 404
  • 5
  • 19

1 Answers1

0

I think you can create and run collection requests to reach your goal. You may set a delay that will be applied to each request, which will make your test suite to take longer to execute but will probably comply with your delay requirement.

First, you go to your collection and click 'Run':

enter image description here

Then, you choose the which requests are gonna run, their order and the delay

enter image description here

If you insist on running a delay only at a specific request, you may try to do it with Newman npm package, which will enable you to run not only requests but entire folders, with the --folder option. So, you could try to create a folder for your un-delayed requests, and another for your delayed request.

João Luca
  • 19
  • 1
  • 8