如何使用 http_client 测试 lexik_jwt_authentication 受保护的 API?
How to test a lexik_jwt_authentication protected API using http_client?
我正在写一些功能测试,看起来客户端找不到路由的控制器,这是我测试的代码:
$response = static::createClient()->request(
'POST',
'/api/login_check',
[
'body' => [
'username' => 'username',
'password' => 'secret123!#'
]
]
);
当我 运行 测试时我得到这个错误:
2020-01-13T11:53:54+00:00 [error] Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "Unable to find the controller for path "/api/login_check". The route is wrongly configured." at /var/www/html/vendor/symfony/http-kernel/HttpKernel.php line 130
路由也按照route:debug
配置
+--------------+---------------------------------------------------------+
| Property | Value
|
+--------------+---------------------------------------------------------+
| Route Name | api_login_check
| Path | /api/login_check
| Path Regex | #^/api/login_check$#sD
| Host | ANY
| Host Regex |
| Scheme | ANY
| Method | POST
| Requirements | NO CUSTOM
| Class | Symfony\Component\Routing\Route
| Defaults | NONE
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+
这与我之前使用邮递员添加 .htaccess
文件时遇到的错误完全相同。
您正在使用 Http 客户端发送错误的参数。
您没有发送编码为 JSON 的身份验证有效负载,也没有发送适当的 content-type
headers。这就是路由没有正确匹配的原因。
而不是设置 body
,使用 json
键和 Http 客户端 will do the work for you。
When uploading JSON payloads, use the json
option instead of body
. The given content will be JSON-encoded automatically and the request will add the Content-Type: application/json
automatically too
$response = static::createClient()->request(
'POST',
'/api/login_check',
[
'json' => [
'username' => 'username',
'password' => 'secret123!#'
]
]
);
我正在写一些功能测试,看起来客户端找不到路由的控制器,这是我测试的代码:
$response = static::createClient()->request(
'POST',
'/api/login_check',
[
'body' => [
'username' => 'username',
'password' => 'secret123!#'
]
]
);
当我 运行 测试时我得到这个错误:
2020-01-13T11:53:54+00:00 [error] Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "Unable to find the controller for path "/api/login_check". The route is wrongly configured." at /var/www/html/vendor/symfony/http-kernel/HttpKernel.php line 130
路由也按照route:debug
配置+--------------+---------------------------------------------------------+
| Property | Value
|
+--------------+---------------------------------------------------------+
| Route Name | api_login_check
| Path | /api/login_check
| Path Regex | #^/api/login_check$#sD
| Host | ANY
| Host Regex |
| Scheme | ANY
| Method | POST
| Requirements | NO CUSTOM
| Class | Symfony\Component\Routing\Route
| Defaults | NONE
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+
这与我之前使用邮递员添加 .htaccess
文件时遇到的错误完全相同。
您正在使用 Http 客户端发送错误的参数。
您没有发送编码为 JSON 的身份验证有效负载,也没有发送适当的 content-type
headers。这就是路由没有正确匹配的原因。
而不是设置 body
,使用 json
键和 Http 客户端 will do the work for you。
When uploading JSON payloads, use the
json
option instead ofbody
. The given content will be JSON-encoded automatically and the request will add theContent-Type: application/json
automatically too
$response = static::createClient()->request(
'POST',
'/api/login_check',
[
'json' => [
'username' => 'username',
'password' => 'secret123!#'
]
]
);