我已经在Laravel 5.3中构建了一个api,现在我想测试一下。 api正在使用Tymen jwt-auth。
例如,更新帐户的测试如下所示:
protected function headers($user = null) {
$headers = ['Accept' => 'application/json'];
if (!is_null($user)) {
$token = JWTAuth::fromUser($user);
JWTAuth::setToken($token);
$headers['Authorization'] = 'Bearer '.$token;
}
return $headers;
}
/** @test */
public function a_user_updates_his_account() {
factory(User::class)->create([
'name' => 'Test',
'last_name' => 'Test',
'email' => 'mail@gmail.com',
'mobile' => '062348383',
'function' => 'ceo',
'about' => 'About me.....',
'corporation_id' => 1
]);
$user = User::first();
$user->active = 2;
$user->save();
$url = '/api/user/' . $user->slug;
$test = $this->get($url, $this->headers($user));
dd($test);
}
在dd($test);
我收到内部服务器错误:
The token could not be parsed from the request
当我尝试这个时:
dd($this->headers($user));
结果如下:
"Accept" => "application/json"
"Authorization" => "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cL2xvY2FsaG9zdCIsImlhdCI6MTQ4NDY0ODUxMiwiZXhwNDg0NjU5MzEyLCJuYmYiOjE0ODQ2NDg1MTIsImp0aSI6ImNhODBjZGZjZjI1YjdiYjM5ZGYxMTU4N2E0NmM1NTExIn0.IPKfabrad5RPtcyoNqwCHcDzMWPzFbLXdipvrBeVKZY"
那可能是什么错误?
- 编辑 -
如果我把它放在中间件中,它就无法正常工作。但是在控制器构造函数中呢? 的中间件:
<?php
namespace App\Http\Middleware;
use Closure;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Request;
class TestingKeepRequest
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ((\App::environment() == 'testing') && array_key_exists("HTTP_AUTHORIZATION", Request::server())) {
JWTAuth::setRequest(\Route::getCurrentRequest());
}
return $next($request);
}
}