我正在编写一个简单的测试来断言一个api端点响应是否返回200状态。
我的api通过sanctum
中间件获得了更多的身份验证。
我的测试功能是
public function testExample() { // $response = $this->withHeader('Authorization' , 'Bearer 1|IJleXxjHgr8RaiP3Q9atJRtVCZHZKQQqBXW8NPdn') // ->getJson('/api/breeds/cat/high'); $response = $this->getJson('/api/breeds/cat/high', ['Authorization' => 'Bearer longtoken']); $response->assertStatus(200); }
路线/api.php;
> Route::middleware('auth:sanctum')->group(function () {
> Route::get('/breeds/{animal_type}/{name}', ListBreeds::class)->name('breed.list'); });
此测试返回给我403 /未授权的响应。如果我在邮递员中使用相同的标记和标头,则该路由可以正常工作。
我做错什么了?
提前谢谢。
发布于 2022-04-15 10:52:16
应该是这样的:
$response = $this->withHeaders([
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . 'abc',
])->getJson('/api/breeds/cat/high');
https://stackoverflow.com/questions/64996847
复制相似问题