wheeler's avatar

Lumen 5.4 doesn't seem to be sending test headers

AuthServiceProvider

<?php
  $this->app['auth']->viaRequest('api', function ($request) {
            if ($request->hasHeader('Authorization')) {
                return User::where('api_token', $request->bearerToken())->first();
            }
        });

Lumen 5.3; test is green

<?php
    /** @test */
    public function user_can_access_own_endpoint_with_bearer_token()
    {
        $user = factory(App\User::class)->create([
            'email' => '[email protected]',
            'api_token' => 123456,
        ]);

        $this->get('users/' . $user->id, [
            'Authorization' => 'Bearer ' . $user->api_token
        ])->seeJson([
            'email' => '[email protected]',
        ]);
    }

Lumen 5.4; test is red

<?php
    /** @test */
    public function user_can_access_own_endpoint_with_bearer_token()
    {
    // throws unauthenticated exception because Authorization header is not visible to AuthServiceProvider
    }

Note I tried the following and got the same result.

 $this->call('GET', '/users/' . $user->id,[],[],[], ['HTTP_Authorization' => $user->api_token]);

I also did a sanity check in Postman with the Authorization header and it worked as expected in 5.4, therefore I concluded that the 5.4 testing framework is the problem.

I considered it was maybe something to do with laravel/dusk, but Lumen does not mention any requirements to import dusk or change tests, so I assume it has no relevance.

0 likes
7 replies
oliverlundquist's avatar

Got it working with this:

app()->get('/test-that-headers-where-set', function (\Illuminate\Http\Request $request) {
    (new \Illuminate\Support\Debug\Dumper)->dump($request->headers);
});

$server = [
    'Content-Type'   => 'application/vnd.api+json',
    'Accept'         => 'application/vnd.api+json',
    'Authorization'  => 'Bearer 123'
];
$transformedServer = $this->transformHeadersToServerVars($server);

$this->call('GET', '/test-that-headers-where-set', $parameters = [], $cookies = [], $files = [], $server);
$this->call('GET', '/test-that-headers-where-set', $parameters = [], $cookies = [], $files = [], $transformedServer);
dd('stop!');

Output:

Symfony\Component\HttpFoundation\HeaderBag {#270
  #headers: []
  #cacheControl: []
}
Symfony\Component\HttpFoundation\HeaderBag {#239
  #headers: array:9 [
    "host" => array:1 [
      0 => "localhost"
    ]
    "user-agent" => array:1 [
      0 => "Symfony/3.X"
    ]
    "accept" => array:1 [
      0 => "application/vnd.api+json"
    ]
    "accept-language" => array:1 [
      0 => "en-us,en;q=0.5"
    ]
    "accept-charset" => array:1 [
      0 => "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
    ]
    "content-length" => array:1 [
      0 => 2
    ]
    "content-type" => array:1 [
      0 => "application/vnd.api+json"
    ]
    "authorization" => array:1 [
      0 => "Bearer <token>"
    ]
  ]
  #cacheControl: []
}
"stop!"
oliverlundquist's avatar

I did some more testing and it seems like it doesn't work in middleware, I cannot get the same headers from the request instance that I can get inside the controller callback.

oliverlundquist's avatar

Could I get some help with this? why are we not getting request headers inside middleware when called from PHPUnit? works in Postman.

wheeler's avatar

@oliverlundquist nice spotting, I pulled in the latest 5.4 and i'm back to green tests.

Just waiting on a tagged release now.

Please or to participate in this conversation.