使用 api 令牌身份验证的 laravel phpunit 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46769959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:48:30  来源:igfitidea点击:

laravel phpunit test with api token authentication

laravelphpunit

提问by Chris

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.

如何在 phpunit 中添加授权标头?我正在测试一个需要 api_token 的 json api。Laravel 文档提供了一个 actAs 方法。但这在我的情况下不起作用,因为 api 令牌与用户表没有直接关系。

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'
            ]
        ]);
    }

回答by Lerzenit

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 actingAsmethod:

$this->actingAs($user, 'api');

您还可以通过将保护名称作为第二个参数传递给方法来指定应该使用哪个保护来验证给定用户actingAs

$this->actingAs($user, 'api');

回答by Tommy Lohil

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

您可以使用 withHeader 方法并传入您的令牌,这适用于我的本地(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 herewith api guard

或者你可以使用actingAs查看文档herewith 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'
        ]
    ]);
}

回答by VBetsun

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'
             ]
         ]);
}