Background: I am writing a feature test to test HMAC Authentication implementation. In my test, I set a header Authorization: Bearer {token} and send a simple GET request as such:
$headers = ['Authorization' => 'Bearer '.$token];
$response = $this->get($uri, $headers);
In my actual Auth guard, I inject the request object into the constructor and retrieve the header:
class HMACGuard implements Guard
{
public function __construct(
UserProvider $provider,
Request $request
) {
$this->request = $request;
$this->provider = $provider;
}
public function user()
{
// ...
$token = $this->request->bearerToken();
// ...
}
}
This works with normal requests (made via browser or Postman etc.). However, this does not work with the unit tests. But, this does work:
public function user()
{
// ...
$token = request()->bearerToken();
// ...
}
Why does the injected version differ and not contain the header information as the one retrieved freshly from the container does? The returned objects obviously are different with different IDs. How can I make sure to inject the correct Request instance that would contain all the information for either type of request? Is there a way to make sure I access the correct Request object without using global helpers/Facades like request() and Request?
Aucun commentaire:
Enregistrer un commentaire