DEV Community

Santhosh
Santhosh

Posted on

Unable to set request header in laravel tests

I have a test which is trying to login and get details. I need to pass a bearer token in request header for this operation. Please see the below code. I could see that the header seldom has the headers that I set. Can anyone give me a pointer to fix this issue?

I am using Laravel 7.2.2, PHP 7.4,

And I am running php artisan test

Code:

public function a_user_can_get_details()
    {
        $this->create_user();

        $response = $this->json('POST', 
                    '/api/login', 
                    [
                        "email" => "john.doe@example.com",
                        "password" => "john123"
                    ]);
        $response->assertSuccessful();

        $token = Str::replaceLast('"', '', Str::replaceFirst('"', '', $response->getContent()));
        $headers = [
                        'Accept' => 'application/json',
                        'Content-Type' => 'application/json',
                        'Authorization' => 'Bearer ' . $token
                    ];


        $response = $this->withHeaders($headers)
                        ->get('api/user');

        $response->assertSuccessful();
        $this->assertCount(1, User::all());

    }

And here is the error I am getting. Actually, the test must pass. That is the right user name and password:

Response status code [403] is not a successful status code. Failed asserting that false is true.

  at tests/Feature/UserTest.php:141
    137|
    138|         $response = $this->withHeaders($headers)
    139|                         ->get('api/user');
    140|
  > 141|         $response->assertSuccessful();
    142|         $this->assertCount(1, User::all());
    143|
    144|     }
    145|

Written with StackEdit.

Top comments (1)

Collapse
 
santhoshj profile image
Santhosh

Thank you for the response. You were right for one part. I believe the string manipulation made is issue there. Yet, the issue still exists. I got 401 now.

There was 1 failure:

1) Tests\Feature\UserTest::a_user_can_get_details
Response status code [401] is not a successful status code.
Failed asserting that false is true.

/Users/santhoshj/dev/Sites/diet-server/vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:73
/Users/santhoshj/dev/Sites/diet-server/tests/Feature/UserTest.php:140

I changed my header array like this:

$headers = [
                        'Accept' => 'application/json',
                        'Content-Type' => 'application/json',
                        'Authorization' => 'Bearer ' . $response->getContent()
                    ];

I tried die and dump the $response, and here is the header array from the dd:

#headerNames: array:3 [
        "cache-control" => "Cache-Control"
        "date" => "Date"
        "content-type" => "Content-Type"
      ]
      #headers: array:3 [
        "cache-control" => array:1 [
          0 => "no-cache, private"
        ]
        "date" => array:1 [
          0 => "Sat, 28 Mar 2020 03:03:45 GMT"
        ]
        "content-type" => array:1 [
          0 => "application/json"
        ]
      ]