gjsnjq
Last Updated: February 25, 2016
·
3.551K
· jpcamara

Sending HTTP Headers to Controllers when Testing in Laravel

When testing a controller in Laravel, you can supply server parameters as part of the controller invocation. But if you're trying to send HTTP Headers, you have to make sure they're formatted properly or they'll be ignored.

Internally Laravel uses alot of Symfony components, and in this particular case the Symfony\Component\HttpFoundation\ServerBag wraps the $_SERVER variable, and it filters out any headers that don't start with "HTTP_".

//#call($method, $uri, $parameters, $files, $server, $content, $changeHistory)

//Custom-Header will be ignored, and \Request::header() will not contain it
$this->call('GET', 'my/url', [], [], ['Custom-Header' => 'content']);

//Custom-Header will now be available. The "HTTP_" is stripped off of the name
$this->call('GET', 'my/url', [], [], ['HTTP_Custom-Header' => 'content']);