簡體   English   中英

授權標頭未在Codeception API測試中完成

[英]Authorization header not making it through in Codeception API testing

我正在嘗試使用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

根據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: header永久化。 不確定這是錯誤還是功能。 而是手動構建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的內容之后,我發現問題在於我與REST模塊同時使用Laravel4模塊。 我沒有意識到使用Laravel4進行HTTP請求實際上只是在同一個會話中模擬對路由的請求,因此我的JWTAuth對象只能在任何特定測試的第一個REST調用中從IOC容器中解析出來。 這意味着在進行后續調用時,第一次調用的請求(包括標題)被保留,因此沒有看到Authorization標頭(在那時正確地通過Request對象傳遞)。

我真的只使用Laravel4模塊將我的環境設置為'測試'並確保我的過濾器在該環境中運行,所以我現在只需要找出一種不同的方法來設置它而不必修改我的bootstrap /每次我想運行我的測試時start.php。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM