Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

echo_'s avatar
Level 7

Http test request lifecycle

In the HTTP Test documentation (https://laravel.com/docs/10.x/http-tests#making-requests), it's said : << These methods do not actually issue a "real" HTTP request to your application. Instead, the entire network request is simulated internally. >>

So if no "real http" request is issued during testing, does the http request issued follow the same request lifecycle as a normal http request as mentionned here (https://laravel.com/docs/10.x/lifecycle) ?

For example When I want to test en api routes which has some custom middlewares, are the middleware executed during the fake http request issued during the test ?

0 likes
6 replies
LaryAI's avatar
Level 58

When you perform HTTP tests in Laravel using methods like $this->get(), $this->post(), etc., the framework simulates the HTTP request lifecycle without actually sending a real HTTP request over the network. However, this simulation does go through the same lifecycle as a real HTTP request within the application.

This means that the request will pass through the same kernel, service providers, middleware, controllers, and responses as it would in a real HTTP request. So, when you're testing your API routes that have custom middleware, those middleware will indeed be executed during the test.

Here's an example of how you might test an API route with middleware:

public function testApiRouteWithMiddleware()
{
    // Simulate a GET request to your API endpoint
    $response = $this->get('/api/your-endpoint');

    // Assert that the middleware was executed by checking the response
    // For example, if your middleware adds a specific header or alters the response in a testable way
    $response->assertStatus(200); // or any other assertion that is relevant to your test

    // You can also check if the response contains the expected data
    $response->assertJson([
        // Your expected JSON structure here
    ]);
}

In this test, the middleware associated with the /api/your-endpoint route will be executed as part of the request lifecycle. If the middleware performs authentication, rate limiting, or any other function, it will affect the outcome of the test just as it would in a real request.

Remember to set up your test environment correctly, including any necessary configuration or authentication, to ensure that your middleware behaves as expected during testing.

1 like
gych's avatar
gych
Best Answer
Level 29

Yes the middleware's will be executed as with the normal http requests. The http request will use the same API route which contains the middleware check.

You can easily test this by making a specific test for this use case.

For example: I have created a test that checks if an API call cannot be executed with the wrong API key. And then another test that check that the API call with the right API key works.

echo_'s avatar
Level 7

@gych Thanks a lot. When you say "You can easily test this by making a specific test for this use case.", does it mean that I can test the middleware separatly ?

gych's avatar

@echo_ I mean that you can replicate a situation where the middleware should definitely fail

In my case this is testing the api route once with the correct APi key and once with an incorrect API key. Then assert the status to check if it passed or failed, if it fails with an incorrect API key I know that the middleware to validate this works.

1 like
gych's avatar

@echo_ No problem :) Don't foget to mark your thread as solved by selecting the best answer.

Please or to participate in this conversation.