Laravel: test routes with custom request headers

So the question is simple:

How to send custom request header when I test Laravel?

I am trying to do this:

$this->call('POST', '/my/route', ['params' => 'array'], [], ['X-Custom' => 'header']);

But when I call Request::header('X-Custom')in my controller, I did not understand this. Yes, it is available at Request::server('X-Custom'), but that is not what I need.

So I need to get it in Request::header().

PS: Laravel 4

+4
source share
3 answers

You need to form the header correctly, otherwise it will be ignored. Try the following:

this->call('POST', '/my/route', ['params' => 'array'], [], ['HTTP_X-Custom' => 'header']);

HTTP_ will be disabled if you look at your request

+10
source

TL; DR; Native Header Prefix "HTTP_"

- , , :

https://github.com/laravel/framework/issues/1655

0

Laravel >= 5.1 :

call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)

An additional array is required before the array of headers:

this->call('POST', '/my/route', ['params' => 'array'], [], [], ['HTTP_X-Custom' => 'header']);
0
source

Source: https://habr.com/ru/post/1540983/


All Articles