我想测试我的基本身份验证页面.未授权测试工作正常.但我在授权登录方面很费劲,因为我不知道如何在测试中设置标头.
我找不到提示,如何在$this-> call()上设置标题.我能找到的唯一信息是:
$this->call($method,$uri,$parameters,$cookies,$files,$server,$content);
并且缺少标题.
如何在laravel上轻松测试基本身份验证.具体:如何为测试请求设置基本auth标头?
我目前拥有的:
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
找到了
HTTP authentication with PHP的解决方案.这可以在$this-> call()的$server参数中使用.
这是我的工作职能:
public function testCorrectLoginonMe() {
// call( $method,$parameters = [],$cookies = [],$files = [],$server = [],$content = null)
$this->call('GET','/api/me',[],['PHP_AUTH_USER' => 'admin','PHP_AUTH_PW' => 'admin12']);
$this->assertResponseStatus( 200 );
}