描述
当我对运行在其上的 symfony 服务器发出正常请求时,http://localhost:8000/api/admin/login_check
它会返回所需的 jwt 令牌。
但是,当我使用功能测试(使用./bin/phpunit
)执行此操作时,出现以下错误:
错误:找不到路径 \"/api/admin/login_check\" 的控制器。路由配置错误。
我还浏览了功能测试文档。
错误重现
不要犹豫,克隆或分叉这个项目进行测试。有 README.md 解释安装步骤。
通过克隆 lexikjwtauthenticationbundle 的创建者之一提供的工作示例,我还能够重现该错误。
日志
测试日志(错误)
运行时出现./bin/phpunit
[2019-01-31 09:37:49] request.INFO: Matched route "api_admin_login_check". {"route":"api_admin_login_check","route_parameters":{"_route":"api_admin_login_check"},"request_uri":"http://localhost/api/admin/login_check","method":"POST"} []
[2019-01-31 09:37:49] security.INFO: Populated the TokenStorage with an anonymous Token. [] []
[2019-01-31 09:37:49] request.WARNING: Unable to look for the controller as the "_controller" parameter is missing. [] []
开发日志(成功)
在执行 curl 或 postman 请求时发生
[2019-01-29 21:16:26] request.INFO: Matched route "api_admin_login_check". {"route":"api_admin_login_check","route_parameters":{"_route":"api_admin_login_check"},"request_uri":"https://localhost:8000/api/admin/login_check","method":"POST"} []
[2019-01-29 21:16:27] doctrine.DEBUG: SELECT t0.id AS id_1, t0.email AS email_2, t0.password AS password_3 FROM admin t0 WHERE t0.email = ? LIMIT 1 ["email@test.com"] []
[2019-01-29 21:16:27] security.INFO: User has been authenticated successfully. {"username":null} []
相关代码:
测试方法:
public function testLogin(){
$client = static::createClient();
$client->request('POST', '/api/admin/login_check', [], [],
[
'Content-Type' => 'application/json',
'Accept' => 'application/json'
],
json_encode([
'email' => 'email@test.com',
'password' => 'qwerty123'
])
);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
路线:
# Admin Routes
api_admin_login_check:
path: /api/admin/login_check
methods: [POST]
安全:
security:
# more configs here
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login_admin:
pattern: ^/api/admin/login
stateless: true
anonymous: true
json_login:
username_path: email
provider: app_admin_provider
check_path: /api/admin/login_check
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
admin_api:
pattern: ^/api/admin
stateless: true
provider: app_admin_provider
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
access_control:
- { path: ^/api/admin/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/admin, roles: ROLE_ADMIN }
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
问题
为什么在功能测试期间有一条404 route not found
for/api/admin/login_check
路线,但在 curl 和 postman 中运行良好?