14

How can I add the authorization header in phpunit? I am testing a json api that requires an api_token. laravel docs provide a actingAs method. But this does not work in my case because the api token is not directly related to the users table.

EDIT:

public function test_returns_response_with_valid_request()
    {
        $response = $this->json('post', '/api/lookup', [
            'email' => '[email protected]'
        ]);
        $response->assertStatus(200);
        $response->assertJsonStructure([
            'info' => [
                'name'
            ]
        ]);
    }
0

4 Answers 4

33

According to documentation.

You may also specify which guard should be used to authenticate the given user by passing the guard name as the second argument to the actingAs method:

$this->actingAs($user, 'api');
Sign up to request clarification or add additional context in comments.

Comments

10

You can use withHeader method and pass in your token, and this works on my local (Laravel 6)

public function test_returns_response_with_valid_request()
{
    // define your $token here
    $response = $this->withHeader('Authorization', 'Bearer ' . $token)
        ->json('post', '/api/lookup', [
            'email' => '[email protected]'
        ]);

    $response->assertStatus(200);
    $response->assertJsonStructure([
        'info' => [
            'name'
        ]
    ]);
}

Or you can use actingAs with look at docs here with api guard

public function test_returns_response_with_valid_request()
{
    $user = factory(User::class)->create();
    $response = $this->actingAs($user, 'api')
        ->json('post', '/api/lookup', [
            'email' => '[email protected]'
        ]);

    $response->assertStatus(200);
    $response->assertJsonStructure([
        'info' => [
            'name'
        ]
    ]);
}

Comments

2

According to documentation

public function test_returns_response_with_valid_request()
{
     $user = factory(User::class)->create();

     $response = $this->actingAs($user)
         ->post('/api/lookup',[
             'email' => '[email protected]'
         ]);

     $response->assertStatus(200);
     $response->assertJsonStructure([
             'info' => [
                 'name'
             ]
         ]);
}

Comments

1

According to this documentations https://laravel.com/docs/5.8/api-authentication#passing-tokens-in-requests & https://laravel.com/docs/8.x/http-tests

There are several ways of passing the API token to your application. You may choose any of these approaches based on the needs of your application.

  • (Query String) you may specify the token as an api_token query string value
$response = $this->json('POST', '/api/sth?api_token='.$token, $test_data)->assertSussessful();
  • (Request Payload) you may include the token in the request's form parameters as an api_token
     $response = $this->json('GET', '/api/sth', [
            'headers' => [
                'Accept' => 'application/json',
            ],
            'form_params' => [
                'api_token' => $token,
            ],
        ]);
  • (Bearer token) Bearer token in the Authorization header of the request:
    $response = $this->json('GET', '/api/sth', [
                'headers' => [
                    'Authorization' => 'Bearer '. $token,
                    'Accept' => 'application/json'
                ]
            ]);
  • (Bearer token) you can use withHeaders method and Bearer token in the Authorization header of the request:
    $response = $this->withHeaders([
            'Authorization' => 'Bearer '. $token,
            'Accept' => 'application/json'
        ])->post('/api/sth', $test_data);

2 Comments

Please provide an explanation of your answer so that the next user knows why this solution worked for you. Also, sum up your answer in case the link stops working in the future.
I wanted to pass a token in request, Consumers can communicate with api after getting a token. In my test code, when I'm calling to the get/post/... api with authentication token, there was a problem. If someone meets like this problem that can't pass a token to request, please read and follow the above doc;

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.