授权标头在Codeception API测试中未通过


Authorization header not making it through in Codeception API testing

我正在尝试使用Codeception测试我的Laravel 4 REST API,但是当我尝试通过我的授权标头发送时(使用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');
}

根据浏览器工具包发送的标头(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

根据 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

令牌已正确接收,但我的 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 ...

我相信有更好的方法可以做到这一点,但在我找到更好的方法之前,这是有效的。

有一个解决方法。使用 $I->amHttpAuthenticated("test", "test") 可使Authorization:标头永久化。不确定这是错误还是功能。而是手动构建 Authorization: Basic 标头,以便在设置Authorization: Bearer标头之前将其删除。

$I = new ApiTester($scenario);
$I->wantTo("fetch token and use it as bearer");
/* This does not work. */
/* $I->amHttpAuthenticated("test", "test"); */
/* Instead use this. */
$I->setHeader("Authorization", "Basic dGVzdDp0ZXN0");
$I->haveHttpHeader("Content-Type", "application/json");    
$I->sendPOST("token", ["read", "write"]);
$I->seeResponseCodeIs(201);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(["status" => "ok"]);
$token = $I->grabDataFromJsonResponse("token");
/* Delete the basic auth header before adding bearer. */
$I->deleteHeader("Authorization");
$I->amBearerAuthenticated($token);

在深入研究了Laravel和Codeception之后,我发现问题在于我同时使用Laravel4模块和REST模块。我没有意识到将 Laravel4 用于 HTTP 请求实际上只是模拟对同一会话中的路由的请求,因此我的 JWTAuth 对象仅在任何特定测试中的第一个 REST 调用中解析出 IOC 容器。这意味着在进行后续调用时,来自第一次调用的请求(包括标头)被保留,因此看不到授权标头(此时与请求对象一起正确传递)。

我实际上只使用 Laravel4 模块将我的环境设置为"测试"并确保我的过滤器在该环境中运行,所以我现在只需要找出一种不同的方法来设置它,而无需修改我的引导/启动.php每次我想运行我的测试时