zeecher's avatar

[PhpUnit] How to test custom headers

Hi, could anybody tell me how to access request headers during unit testing?

Giving following error:

0 likes
1 reply
zeecher's avatar
zeecher
OP
Best Answer
Level 2

Figured out, here is the working version in case if someone needs.

<?php

use Illuminate\Http\Response;

class RequestTest extends TestCase {

    public $response;

    function __construct()
    {
        $this->response = new Response();
    }

    public function testHeader()
    {

       $this->call('POST', '/test',[],[],[], ['HTTP_Authorization' => 'content'],[]);

        $foo = $this->response->getContent();

        $this->assertEquals('yes',$foo);
    }

}


Route::post('/test', function()
{
    return \Request::header('Authorization') ? 'yes' : 'no';

});

2 likes

Please or to participate in this conversation.