所以我是一個新手代碼,我想弄清楚如何測試我的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