Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Testing routes with custom request headers

So the question is simple:

How to send custom request header, when I testing with Laravel?

I'm trying to make like so:

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

But when I call Request::header('X-Custom') in my controller, I didn't get it. Yes, it's available in Request::server('X-Custom'), but it's not what I need.

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

PS: Laravel 4

like image 470
MrSil Avatar asked May 17 '14 18:05

MrSil


2 Answers

You need to properly form the header, or it will be ignored. Try this:

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

The HTTP_ will be stripped when you look at your Request

like image 143
Laurence Avatar answered Oct 20 '22 07:10

Laurence


TLDR; Prefix your custom headers with 'HTTP_'

If anybody is interested in why this worked, read this issue:

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

like image 45
Visti K Avatar answered Oct 20 '22 07:10

Visti K