我试图使用代码欺骗功能测试我的应用程序的RESTful API。我使用HttpBasicAuth与自定义身份验证功能,验证用户名和密码。
我试图测试的endpoint是一个简单的GET /users.functional.suite.yml是这样的:
class_name: FunctionalTester
modules:
enabled:
- Filesystem
- Yii2
- REST:
depends: PhpBrowser
- tests\codeception\_support\FixtureHelper
config:
Yii2:
configFile: 'codeception/config/functional.php'
REST:
url: 'http://localhost:8888/v1/'
我的用户名是APICEPT。php是这样开始的:
<?php
$I = new FunctionalTester($scenario);
$I->wantTo('test the user REST API');
$I->amHttpAuthenticated('goodname', 'goodpassword');
$I->sendGET('users/1');
当我运行套件时,我得到401(未经授权的HTTP异常)。在--debug中,请求标头为空。但是,当我在Postman中运行GET(提供用户名和密码)时,GET会返回我期望的JSON响应。所以我知道应用编程接口有效。
看来amHttpAuthenticated存在问题,我认为它应该提供授权头,对吗?
关于如何解决这个问题,有什么建议吗?
你好乔
如果其他人有这个问题,我的依赖性就错了:
class_name: FunctionalTester
modules:
enabled:
- Filesystem
- Yii2
- REST:
depends: Yii2
...
当我最初在添加REST后进行构建时,我收到错误消息:“此模块依赖于Codeception\Lib\InnerBrowser”,然后使用
depends: PhpBrowser
希望这对别人有帮助。