我试图使用Codeception测试我的Laravel 4 REST API,但是当我尝试通过我的Authorization标头发送时(使用REST模块的$I-> amBearerAuthenticated()函数),它没有通过最终请求.
从我所看到的,Symfony2 BrowserKit模块修改添加到HTTP_XXX_XXX格式的任何标头,因此发送的标头似乎是HTTP_AUTHORIZATION – 当我在我的应用程序中输出收到的标头时,但是,授权和HTTP_AUTHORIZATION都不存在.
如果有帮助,这是我的Codeception测试:
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'); }
根据BrowserKit发送的标头(在REST模块中输出$this-> client-> getInternalRequest() – > getServer()):
HTTP_HOST : localhost HTTP_USER_AGENT : Symfony2 BrowserKit HTTP_AUTHORIZATION : Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3RcL2F1dGhcL2xvZ2luIiwic3ViIjoxLCJpYXQiOjE0MjE3ODY0NDEsImV4cCI6MTQyMTg3Mjg0MX0.XxxxZMe8gwF9GS8CdKsh5coNQer1c6G6prK05QJEmDQ HTTP_REFERER : http://localhost/auth/login HTTPS : false
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
令牌被正确接收,但我的API(正确)在GET请求上返回401,因为它没有收到令牌.
任何帮助将不胜感激!
我正在使用Laravel 5(L4没有大差异)和REST模块.这就是我现在正在做的方式:
protected $token; public function _before(ApiTester $I) { $user = TestDummy(...); $I->sendPOST('/v1/auth/login',[ 'email' => $user->email,'password' => $user->password ]); $this->token = $I->grabDataFromResponseByJsonPath('$.token'); }
然后在我的测试中:
// Do stuff ... $I->amBearerAuthenticated($this->token[0]); // Do more stuff ...