tzookb's avatar

laravel testig request setting header

Im trying to test the jwt authentication,

so I have a test that sets a request with a token in the header, but I simply cant place the token in the header.

this is what I need:

 Authorization: Bearer {yourtokenhere}

this is from this package

https://github.com/tymondesigns/jwt-auth/wiki/Authentication

0 likes
7 replies
MarkRedeman's avatar

@tzookb if you're asking how to set the header when you do a post request in your test, then you can do the following (assuming you're using Laravel 5.1):

$this->post('blogs', ['title' => 'awesome blog post'], ['HTTP_Authorization' => 'Bearer' . $token]);

So in the third argument you can pass your headers. The same goes for the put, patch and delete methods. The get method takes the headers as a second argument as there is no post data. If you want the headers to be accessible by $request->headers(), then you have to prefix it with HTTP_.

If you're not using Laravel 5.1 then you can use the call method which takes the header data as the 6th argument, below you can see the public interface of the method.

/**
 * Call the given URI and return the Response.
 *
 * @param  string  $method
 * @param  string  $uri
 * @param  array   $parameters
 * @param  array   $cookies
 * @param  array   $files
 * @param  array   $server
 * @param  string  $content
 * @return \Illuminate\Http\Response
 */
public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
6 likes
tzookb's avatar

I found the problem the app ran twice...

once when I used the JWTAuth facade, so it was created by an empty request and again when did the put test call.

So even though it looks bad, I do this steps:

  • create the user
  • create token based on the user
  • reset the application
  • create again the user (as this is in tests, he will still be the first user)
  • run the put request test
  • now when the controller that handles the test request gets first the request, and when it tries to use the JWTAuth facade, the object is created with the request that was created by the test.

** hope somebody understood, but it is surely an edge case

   $user = factory('App\User')->create();
    $token = JWTAuth::fromUser($user);

    $this->refreshApplication();

    $user = factory('App\User')->create();

    $server = [
        'HTTP_Authorization' => 'Bearer '.$token
    ];

    $res = $this->put('/api/posts?boom=tzook', [], $server);
    $res->seeStatusCode(200);
4 likes
Mike-e's avatar

Hi @MarkRedeman,

I use lumen 5.1 with a middleware to check the validity of an API key.

When i call :

$this->get('some-uri', ['X-Api-Key' => 'some-key']);

in a unit test, this header was not added to request.

I checked the header received in the middleware with dd($request->header()); and i dot not see my X-Api-Key.

Someone have the same problem ?

MarkRedeman's avatar

@Jaoued it took me some time to figure out, but you will need do prefix HTTP_ before your header key. This is because when a Request object is constructed the contents of $server are inserted in a ServerBag object, which has a getHeaders() method that will return all headers based on some criteria, one of which is that all keys which are prefixed with HTTP_ are returned as headers, as you can see here.

I will change my previous post to reflect that you need to prefix the headers with HTTP_.


@tzookb I'm not sure if you're still have a question or if you've solved the problem, can you elaborate?

VojtaSvoboda's avatar

I've fixed this problem by getting request directly from container:

$this->app['auth']->viaRequest('api', function ($request) {
    $request = $this->app['request'];
    if ($token = $request->headers->get('Api-Token')) {
        return User::where('api_token', $token)->first();
    }
});

and it works! When using $request passed to closure, it didn't works.

Test example:

$this->get('/v1/items', ['Api-Token' => $apiToken]);

Please or to participate in this conversation.