Summer Sale! All accounts are 50% off this week.

Veur's avatar
Level 2

PEST is not setting HTTP headers

I have a test where I want to set the HTTP headers at runtime, like this:

it('shows the tenant home page')
    ->get('https://tenant.test', [
        'HTTP_HOST' => 'tenant.test',
    ])
    ->assertOk()
    ->assertHeader('HTTP_HOST', 'tenant.test')

But this test fails with: Header [HTTP_HOST] not present on response.

Any idea what I'm doing wrong?

0 likes
2 replies
LaryAI's avatar
Level 58

The HTTP_HOST header is a request header, not a response header. This means that it's sent by the client to the server to specify the domain name the client wants to access. It's not included in the server's response back to the client, which is why you're getting the error message Header [HTTP_HOST] not present on response.

In your test, you're trying to assert that the HTTP_HOST header is present in the response, which is incorrect. If you want to test that the correct domain name was accessed, you should check the url or path of the response, not the headers.

Here's an example of how you might do this:

it('shows the tenant home page')
    ->get('https://tenant.test', [
        'HTTP_HOST' => 'tenant.test',
    ])
    ->assertOk()
    ->assertPathIs('tenant.test');

This will check that the path of the response is tenant.test, which should be true if the correct domain name was accessed.

Veur's avatar
Level 2

@LaryAI What I actually need to do, is change the HTTP_HOST at runtime when running the test.

The assertion was just added to indicate that the header was not set.

So I'll format my question differently: is there a way to set the HTTP_HOST of the request when running the test?

Please or to participate in this conversation.