Jwt-auth: PHPUnit Authorization Header not received

Created on 22 Sep 2016  路  4Comments  路  Source: tymondesigns/jwt-auth

Been having problems with my API tests. In the test I use an existing seeded User and generate a token and attach this to the Authorization header as "Authorization: Bearer {token}".

On the other end I have a base controller with the following code; which works fine on the app itself.

public function __construct(JWTAuth $auth)
{
    $this->authUser = $auth->parseToken()->authenticate();
    $this->authAccount = $this->authUser->account;
}

When performing a unit test I get the following error.

The token could not be parsed from the request

After some debugging I realised that the HTTP_AUTHORIZATION header is being set/sent but does not show up in the request headers on the other end. I was wondering if FastCGI had a part to play in this but checked the "public/.htaccess" and it is set up correctly. Also proved by the app working fine from a browser.

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Here is the code to one of my example tests that is failing.

Test

public function test_that_all_question_sets_can_be_fetched()
{
    $this->getAuthUser();

    $questionSet = \App\Models\QuestionSet::all();
    $questionSetAsArray = $questionSet->random()->toArray();

    $response = $this->authUserGet('/api/question-set');

    $response->seeJsonContains($questionSetAsArray)
        ->seeApiSuccess();
}

authUserGet function

public function authUserGet($uri, $headers = [])
{
    $this->setHeaders($headers);

    return $this->get($uri, $this->headers);
}

setHeaders function

public function setHeaders($headers)
{
    if (!isset($headers['Authorization'])) {
        $headers['Authorization'] = 'Bearer ' . $this->getAuthUserToken();
    }

    $this->headers = $headers;
}
stale

Most helpful comment

You can use $this->refreshApplication() to solve it.

When you make more than one request in a single test something is not being flushed internally.

$this->getAuthUser(); 

$this->refreshApplication(); 

$response = $this->authUserGet('/api/question-set');

All 4 comments

Can confirm, the same happens to me (using lumen 5.3)

Thanks @nXu. Still haven't resolved it myself and given up.

You can use $this->refreshApplication() to solve it.

When you make more than one request in a single test something is not being flushed internally.

$this->getAuthUser(); 

$this->refreshApplication(); 

$response = $this->authUserGet('/api/question-set');

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

do the job using fcgi

ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9007/var/www/larapi/public/$1

Cf : http://stackoverflow.com/questions/17018586/apache-2-4-php-fpm-and-authorization-headers

Was this page helpful?
0 / 5 - 0 ratings

Related issues

johncloud200 picture johncloud200  路  3Comments

gandra picture gandra  路  3Comments

harveyslash picture harveyslash  路  3Comments

marciomansur picture marciomansur  路  3Comments

gamelife1314 picture gamelife1314  路  3Comments