DEV Community

Discussion on: Unable to set request header in laravel tests

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"
        ]
      ]