Symfony integration tests custom header is missing

I am writing an integration test in Symfony with a request that includes custom headers. However, the request fails because the custom header is apparently missing.
What happened was I forgot to add an HTTP prefix to the custom header.

This is a common pitfall when writing integration tests in Symfony and using custom HTTP headers: the necessity to add an HTTP_ prefix to the header in the test.
If you do not add the prefix to the custom header, it will silently not be added to the request, and you have to debug why the request fails.

So this will fail:

static::$client->request('GET', '/ping', [], [], ['X-Custom-Header' => 'custom']);Code language: PHP (php)

And this will work:

static::$client->request('GET', '/ping', [], [], ['HTTP_X-Custom-Header' => 'custom']);Code language: PHP (php)

According to the documentation, you would actually also need to transform it to uppercase and replace the dash with an underscore but it will work with just the HTTP prefix.


The prefix is however not necessary for not custom but standard headers like ‘Content-Type’.

This is not an intuitive requirement, and I wish it were not necessary.
At the very least, a warning would be nice when the test client encounters a non-prefixed, non-standard header.

A related StackOverflow Post: