I am trying to test my Laravel 4 REST API using Codeception, but when I try to send through my authorization header (using the $ I-> amBearerAuthenticated () function of the REST module), it does not bring up a possible request.
From what I see, the Symfony2 BrowserKit module modifies any headers added to the HTTP_XXX_XXX format, so the sent header seems to be HTTP_AUTHORIZATION - when I output the received headers in my application, however, neither authorization nor HTTP_AUTHORIZATION is present.
If this helps, here is my Codeception test:
public function loginAndHitProtectedPage(ApiTester $I) { $I->wantTo('login and successfully get to a protected page'); $I->sendPOST('/auth/login', ['username' => 'user1', 'password' => 'pass']); $I->seeResponseIsJson(); $token = $I->grabDataFromJsonResponse('token'); $I->amBearerAuthenticated($token); $I->sendGET('/runs'); $I->seeResponseCodeIs(200); $I->seeResponseIsJson(); $I->dontSeeResponseContains('error'); }
Headers sent according to BrowserKit (output $this->client->getInternalRequest()->getServer() in the REST module):
HTTP_HOST : localhost HTTP_USER_AGENT : Symfony2 BrowserKit HTTP_AUTHORIZATION : Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3RcL2F1dGhcL2xvZ2luIiwic3ViIjoxLCJpYXQiOjE0MjE3ODY0NDEsImV4cCI6MTQyMTg3Mjg0MX0.XxxxZMe8gwF9GS8CdKsh5coNQer1c6G6prK05QJEmDQ HTTP_REFERER : http://localhost/auth/login HTTPS : false
Headers obtained according to PHP:
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Accept-Language: en-us,en;q=0.5 Content-Type: application/x-www-form-urlencoded Host: localhost Referer: http://localhost/auth/login User-Agent: Symfony2 BrowserKit
The token is received correctly, but my API (correctly) returns 401 in the GET request, because it does not receive the token.
Any help would be greatly appreciated!
php authorization laravel codeception bearer-token
Ben slinger
source share