Last Updated: February 25, 2016
·
5.368K
· 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']);

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside