【发布时间】:2015-11-04 07:44:26
【问题描述】:
我想测试我的基本身份验证保护页面。 unauthorization 的测试工作正常。但是我在授权登录方面很挣扎,因为我不知道如何在测试中设置标题。
我找不到提示,如何在$this->call() 上设置标题。我能找到的唯一信息是:
$this->call($method, $uri, $parameters, $cookies, $files, $server, $content);
并且缺少标题。
如何在 laravel 上轻松测试基本身份验证。具体:如何设置测试请求的基本认证头?
我目前拥有的:
class ExampleTest extends TestCase {
public function test401UnauthorizedOnMe() {
$response = $this->call('GET', '/api/me');
$this->assertResponseStatus( 401);
}
public function testCorrectLoginOnMe() {
// http://shortrecipes.blogspot.de/2009/12/testing-basic-http-authentication-using.html
//send header with correct user and password i.e.
////YWRtaW46YWRtaW4xMg== is equal to base64_encode( "admin:admin12")
$this->request->setHeader( 'Authorization','Basic YWRtaW46YWRtaW4xMg==');
$response = $this->call('GET', '/api/me');
$this->assertResponseStatus(200);
}
}
我尝试了$this->$request->setHeader();,但这样我只得到一个错误:
1) ExampleTest::testCorrectLoginOnMe
ErrorException: Undefined property: ExampleTest::$request
【问题讨论】:
-
你试过 $this->call('GET', '/api/me',[],[],[], ['Authorization' => 'Basic YWRtaW46YWRtaW4xMg=='] ,[]); ?
-
不起作用。我收到了
Failed asserting that 401 matches expected 200。
标签: php unit-testing laravel-5 phpunit basic-authentication