为什么 codeception 运行ning 中间件在 Laravel 中的路由上而不应该是 运行?

Why is codeception running middleware on routes in Laravel which should not be run?

所以我是一个代码接收新手,我正在尝试弄清楚如何使用它来测试我的 Web 服务。我已经为 authenticate 路由编写了我的第一个简单测试,只是为了确保应用程序像它应该的那样吐出 JWT 令牌。测试看起来像这样:

<?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' => 'admin@admin.com',
    '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 路由甚至不需要令牌,所以我继续查看在我的应用程序中哪里是被解析的令牌。最后我意识到在 Kernal 中有一个名为 ChangeDb 的中间件,它会检查除 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 returns true 显然是因为它查看了令牌,这是我的测试失败的地方。

那么为什么会这样呢?从 Codeception 生成的请求是否与从 Postman 或我的前端 ajax 应用程序生成的请求有某种不同?如何解决这个问题?

所以我假设 Laravel5 因为 REST 部分中的依赖项是问题所在。它没有模拟真实的 HTTP 请求,这就是 middleware 无法分辨请求来自何处的原因。我将其更改为 PhpBrowser,现在可以使用了。 PhpBrowser 显然模拟真实的 HTTP 请求。