3

I have a problem in testing Laravel 5.5. I need to send a Bearer Token in TEST HEADER, but doesn't work

public function testAuthCheckinvalidToken()
    {
        $response = $this->withHeaders([
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer ' . $this->token,
        ])->json('GET', 'auth/check');
    ...
    }

When I dd($response), only the default HEADERS is set:

#headers: array:5 [
            "cache-control" => array:1 [
              0 => "no-cache, private"
            ]
            "date" => array:1 [
              0 => "Tue, 21 Nov 2017 18:48:27 GMT"
            ]
            "content-type" => array:1 [
              0 => "application/json"
            ]
            "x-ratelimit-limit" => array:1 [
              0 => 60
            ]
            "x-ratelimit-remaining" => array:1 [
              0 => 59
            ]
          ]

My HEADERS doesn't appear. I think that I am right

2 Answers 2

9

The headers you set here are for request obviously, for Response you are getting headers from your Laravel application so obviously you won't see the headers you set for your request.

If you want to see headers you set here, you should run dd($request); in your app and not in tests.

EDIT

To confirm that headers are passed to application the whole testing code:

tests/Feafure/ExampleTest.php

<?php

namespace Tests\Feature;

use Tests\TestCase;

class ExampleTest extends TestCase
{

    public function testBasicTest()
    {
        $response = $this->withHeaders([
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer ' . 'abc',
        ])->json('GET', 'auth/check');
    }
}

routes/web.php

Route::get('auth/check', function() {
   dd(request()->headers); 
});

so when I now run test:

./vendor/bin/phpunit

result is:

Symfony\Component\HttpFoundation\HeaderBag {#49   #headers: array:8 [
    "host" => array:1 [
      0 => "localhost"
    ]
    "user-agent" => array:1 [
      0 => "Symfony/3.X"
    ]
    "accept" => array:1 [
      0 => "application/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-type" => array:1 [
      0 => "application/json"
    ]
    "authorization" => array:1 [
      0 => "Bearer abc"
    ]
    "content-length" => array:1 [
      0 => 2
    ]   ]   #cacheControl: [] }

so as you see headers from test are passed to application

Sign up to request clarification or add additional context in comments.

4 Comments

Actually my headers have not been sent, so, my request fails
@FernandoHeriqueRubimPioli Look at my edit, everything is working as expected
adding to the answer I would like to say that for POST requests you can send in data using ->json('POST', 'url', $dataAsArray); you can use json_decode('jsonString',true) to convert Json to Array
if someone needs the way to get each atributtes: $request->headers->get('user-agent')
0

Please check you Auth guard Maybe your auth quard cache request and than use info from previous request

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.