2017-10-16 67 views
1

如何在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' 
      ] 
     ]); 
    } 
+0

告訴我們任何你的phpunit代碼 –

回答

0

的解決方案可能是在refreshAppliation();

首先設置一個令牌,然後刷新測試中的應用程序,然後重試。

$token = 'some-token-taken-from-somewhere'; 

$this->refreshApplication(); 

$request = $this->get('some/route/action', [ 
    'HTTP_Authorization' => 'Bearer ' . $token 
]);