logo

Verify the order of HTTP requests in your tests

And catch duplicate requests at the same time

Joel Clermont
Joel Clermont
2026-04-21

Imagine your code makes two HTTP requests where the order matters. Maybe you fetch a token from one endpoint and then use it to call another.

Here's a test that looks reasonable:

Http::fake();

// run the code

Http::assertSent(fn ($request) => str_contains($request->url(), '/token'));
Http::assertSent(fn ($request) => str_contains($request->url(), '/data'));

You might make the assumption that this verifies the token call happens before the data call, but it doesn't.

Each assertSent independently scans the entire request log, asking only whether at least one matching request happened anywhere. Order is never considered. Both assertions would pass even if your code accidentally called the data endpoint first.

If the order truly matters, there is a better assertion to use:

Http::assertSentInOrder([
    fn ($request) => str_contains($request->url(), '/token'),
    fn ($request) => str_contains($request->url(), '/data'),
]);

This walks the recorded requests in order, asserting the first matches the first callback, the second matches the second, and so on. If your code changes the call order, the test fails with a clear failure message.

A reader pointed out in reply to my previous tip on avoiding accidental duplicate requests that this assertion has another side benefit too. It also enforces a strict count check before walking the callbacks, so you don't need to also call assertSentCount. This one method handles both assertions.

The main lesson is that if you're verifying a series of HTTP requests in your tests, it's important to use assertSentInOrder instead of independent assertSent calls.

Here to help,

Joel

P.S. Subtle testing gotchas like this one are exactly what our testing workshop helps you spot in your own code. Take a look.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice.