所以我是一个新手代码,我想弄清楚如何测试我的web服务使用相同的。我已经为authenticate
路由写了我的第一个简单测试,以确保应用程序正在像应该那样吐出JWT令牌。测试看起来像这样:为什么在不应该运行Laravel的路由上运行中间件?
<?php
$I = new ApiTester($scenario);
$I->wantTo('authenticate a user');
$I->haveHttpHeader('Content-Type', 'application/x-www-form-urlencoded');
$I->sendPOST('/api/authenticate', [
'username' => 'archive',
'email' => '[email protected]',
'password' => 'password'
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
在我的防守中,我尝试了Postman的相同POST,它的工作完美。
api.suite.yml
class_name: ApiTester
modules:
enabled:
- REST:
url: http://localhost:8000/api/
depends: Laravel5
config:
Laravel5:
environment_file: .env.testing
,我面临着在这个测试的问题是,它并没有看到200 OK
作为响应代码,但看到500
代替。后来我意识到,我可以去通过相同的输出在_output
目录,我看到了这个错误:
The token could not be parsed from the request
现在我惊讶的是,authenticate
路线应该甚至不需要凭证,让我去看看哪里在我的应用程序中是正在解析的令牌。最后我意识到有一个名为ChangeDb
的中间件位于Kernal
中,它会检查除了authenticate
路由之外的每个请求中的令牌。它看起来像这样:
public function handle($request, Closure $next)
{
if($request->path() != 'api/authenticate'){
$username = \JWTAuth::parseToken()->getPayload()->get('username');
if(! $username){
\Config::set('database.connections.tenant.database', 'archive');
\DB::reconnect();
\DB::setDatabaseName('archive');
}else{
\Config::set('database.connections.tenant.database', $username);
}
}
return $next($request);
}
但是,当我试图注释掉它的籽粒,测试运行良好,并给了我绿色。所以不知何故,当我从Postman和其他ajax请求尝试它时,这个中间件运行良好,但是当Codeception尝试请求时,if statement
返回true
显然是因为它查看了令牌,这是我的测试失败的地方。
那么为什么会发生这种情况呢?从Codeception生成的请求与Postman或我的ajax应用程序在前端生成的请求有什么不同?如何解决这个问题?
add'var_dump($ request-> path());死亡;'开始处理方法并重新运行此测试。你会得到什么输出? – Naktibalda