如何使用phpunit api请求发送标头?
我正在尝试使用phpunit测试我的web服务,它是用laravel5编写的。我是这个测试框架的新手。应用程序使用JWT进行身份验证,身份验证在请求的头中传递 我的测试看起来是这样的:如何使用phpunit api请求发送标头?,php,testing,http-headers,phpunit,Php,Testing,Http Headers,Phpunit,我正在尝试使用phpunit测试我的web服务,它是用laravel5编写的。我是这个测试框架的新手。应用程序使用JWT进行身份验证,身份验证在请求的头中传递 我的测试看起来是这样的: $response = $this->call('POST', 'authenticate', [ 'username' => 'carparts', 'email' => 'admin@admin.com', 'password' => 'password' ])
$response = $this->call('POST', 'authenticate', [
'username' => 'carparts',
'email' => 'admin@admin.com',
'password' => 'password'
]);
$token = 'Bearer ' . json_decode($response->getContent())->token;
$response = $this->call('POST', 'users', [
"first_name" => "Test",
"last_name" => "Test",
"email" => 'test@test.com',
"password" => "testing",
"role" => "1"
], [], [], [
'Authorization' => $token
]);
dd($response->getContent());
令牌返回得很好,但是当我尝试在下一个请求中使用它来创建新用户时,它失败了,因为无法从请求中解析令牌。当我在中间件中执行
request()->头检查时,即使没有显示头。我做错了什么?如何使用PHPUnit在请求中传递头?$server
参数是一个服务器变量数组,而不是http头。
将您的令牌作为HTTP\u授权传递
:
$response = $this->call('POST', 'users', [
"first_name" => "Test",
"last_name" => "Test",
"email" => 'test@test.com',
"password" => "testing",
"role" => "1"
], [], [], [
'HTTP_AUTHORIZATION' => $token
]);
有一个函数可以将头转换为服务器变量。请尝试传递以下消息:
$this->transformHeadersToServerVars([ 'Authorization' => $token ])
只是胡乱猜测
检查您的.htaccess,它必须包含
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
我正在使用PHP单元进行一些功能测试
$client->request('GET','url', [], [], ['HTTP_Authorization' => $token]);
这对我有用 还是不行。我甚至试图
dd($\u SERVER['HTTP\u AUTHORIZATION'])
在我的路径中查看,但它显示未定义索引。有什么想法吗?我尝试了HTTP_授权,它对我有效。