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

标签 laravel phpunit

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

编辑:

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

最佳答案

根据 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');

关于使用 api token 身份验证的 laravel phpunit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46769959/

相关文章:

php - 找不到laravel类 'Gloudemans\Shoppingcart\ShoppingcartServiceProvider'

php - Laravel & mock : Unit testing the update method without hitting the database

PHP:如何调整 PHPUnit 以使用不同的 PHP 解释器?

php - 测试 Doctrine EntityManager 写入失败

php - Docker - 启动 phpunit testsuite 最简单的方法是什么

php - 拉维尔 "Invalid datetime format :1366"

php - Laravel 5 仅从请求中获取 GET 或 POST 参数

Laravel Passport Key 路径 oauth-public.key 不存在或不可读

php - 调用返回后,PHP 是否垃圾收集函数范围的对象?

phpunit - Symfony 2带PhpUnit 3.6 : changing target hostname for test purposes