当我对在http://localhost:8000/api/admin/login_check
上运行的symfony服务器发出 normal 请求时,它将返回所需的jwt令牌。
但是,当我进行功能测试(使用./bin/phpunit
)时,出现以下错误:
错误:找不到路径\“ / api / admin / login_check \”的控制器。路由配置错误。
我也经历了functional test docs。
请毫不犹豫地克隆或分叉该项目进行测试。有README.md解释安装步骤。
通过克隆lexikjwtauthenticationbundle的创建者之一提供的工作示例,我也能够reproduce the bug。
在运行./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. [] []
发生卷曲或邮递员请求时出现
[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
提供/api/admin/login_check
的路由,但在curl和postman上工作正常?
答案 0 :(得分:2)
确保/login_check
位于防火墙后面
/login_check
路径必须匹配防火墙模式。
更改此
访问控制:
至
访问控制:
我尝试了:P
答案 1 :(得分:2)
测试请求中content-type
标题的名称错误。
它的名称应为CONTENT_TYPE
。
$client->request(
'POST',
'/login_check',
['_username' => 'lexik', '_password' => 'dummy'],
[],
['CONTENT_TYPE' => 'application/json']
);
检查在这里: vendor / symfony / security / Http / Firewall / UsernamePasswordJsonAuthenticationListener.php :: handle
if (false === strpos($request->getRequestFormat(), 'json')
&& false === strpos($request->getContentType(), 'json')
) {
return;
}
至少可以帮助您“复制错误”代码。