php – 测试基本身份验证

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 测试基本身份验证脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想测试我的基本身份验证页面.未授权测试工作正常.但我在授权登录方面很费劲,因为我不知道何在测试中设置标头.

我找不到提示,如何在$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 );
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – 测试基本身份验证全部内容,希望文章能够帮你解决php – 测试基本身份验证所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。