授权头在codeception API测试没有把它通过 [英] Authorization header not making it through in Codeception API testing

查看:565
本文介绍了授权头在codeception API测试没有把它通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用codeception来测试我的Laravel 4 REST API,但是当我试图通过我的授权头发送(使用REST模块的$ I-> amBearerAuthenticated()函数),它不使它通过对最后的请求。

I am attempting 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 is not making it through to the eventual request.

从我所看到的,Symfony2的BrowserKit模块修改添加到HTTP_XXX_XXX格式的头,所以发送的头似乎是HTTP_AUTHORIZATION - 当我输出在我的应用程序所接收到的报头,然而,既不授权也不HTTP_AUTHORIZATION是present。

From what I can see, the Symfony2 BrowserKit module modifies any headers added into the HTTP_XXX_XXX format, so the header being sent seems to be HTTP_AUTHORIZATION - when I output the received headers in my application, however, neither Authorization nor HTTP_AUTHORIZATION are present.

如果有帮助,这是我的codeception测试:

If it 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');
}

标题根据BrowserKit(的 $这个 - &GT输出;客户 - > getInternalRequest() - GT; getServer() REST的模块中)发送的:

Headers sent according to BrowserKit (output of $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

根据收到的PHP

标题:

Headers received 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

该令牌被正确接收,但我的API(正确地)对GET请求返回401,因为它没有收到该令牌。

The token is received correctly, but my API (correctly) returns a 401 on the GET request because it's not receiving the token.

任何帮助将大大AP preciated!

Any help would be greatly appreciated!

推荐答案

一些挖掘到Laravel和codeception的胆量,我发现,问题的事实,我用的是Laravel4模块在同一时间作为REST模块。我没有意识到,使用Laravel4 HTTP请求实际上只是模拟请求到同一个会话中的路线,因此我JWTAuth对象只被解决掉了IOC容器对任何特定测试的第一个REST调用。这意味着,使后续呼叫时,从第一呼叫请求(包括标头)中的被保留,因此授权报头(其在该点正通过传递与正确请求对象)没有被看到。

After some digging into the guts of Laravel and Codeception, I discovered that the problem was with the fact that I was using the Laravel4 module at the same time as the REST module. I hadn't realised that using Laravel4 for HTTP requests actually just simulates the request to the route within the same session, and thus my JWTAuth object was only being resolved out of the IOC container on the first REST call in any particular test. This meant that when making subsequent calls, the request (including headers) from the first call was being retained and thus the Authorization header (which at that point was being passed through with the Request object correctly) was not being seen.

我真的只能用Laravel4模块来我的环境设置为测试,并确保我的过滤器是在那种环境下运行的,所以我现在只需要找出一种不同的方式来设置,而无需修改我的引导/ start.php每次我想运行我的测试。

I was really only using the Laravel4 module to set my environment to 'testing' and to ensure that my filters were running in that environment, so I will now just have to figure out a different way to set that without having to modify my bootstrap/start.php everytime I want to run my tests.

这篇关于授权头在codeception API测试没有把它通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆