使用api令牌认证的laravel phpunit测试
问题描述:
如何在phpunit中添加授权标头?我正在测试需要api_token的json api。 laravel docs提供了一个actAs方法。但这在我的情况下不起作用,因为api标记与用户表不直接相关。使用api令牌认证的laravel phpunit测试
编辑:
public function test_returns_response_with_valid_request()
{
$response = $this->json('post', '/api/lookup', [
'email' => '[email protected]'
]);
$response->assertStatus(200);
$response->assertJsonStructure([
'info' => [
'name'
]
]);
}
答
的解决方案可能是在refreshAppliation()
;
首先设置一个令牌,然后刷新测试中的应用程序,然后重试。
$token = 'some-token-taken-from-somewhere';
$this->refreshApplication();
$request = $this->get('some/route/action', [
'HTTP_Authorization' => 'Bearer ' . $token
]);
告诉我们任何你的phpunit代码 –