Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Http/Requests/ResourceQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,18 @@ protected function mediaTypes(): array
return $this->mediaTypes;
}

/**
* Determine if the given content types match.
*
* @param string $actual
* @param string $type
* @return bool
*/
public static function matchesType($actual, $type)
{
return $actual === $type;
}

/**
* Get an exception if the media type is not acceptable.
*
Expand Down
32 changes: 32 additions & 0 deletions tests/lib/Acceptance/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace LaravelJsonApi\Laravel\Tests\Acceptance;

use App\Models\Post;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Route;
use LaravelJsonApi\Core\Responses\DataResponse;
use LaravelJsonApi\Core\Responses\ErrorResponse;
Expand Down Expand Up @@ -95,4 +96,35 @@ public function testErrors(): void

$response->assertExactErrorStatus($error);
}

public function testValidHeader(): void
{
Route::get('/test', fn() => []);

$response = $this
->withoutExceptionHandling()
->jsonApi()
->get('/test');

$response->assertStatusCode(Response::HTTP_OK);
}

public function testInvalidHeader(): void
{
$error = [
'status' => '406',
'title' => 'The requested resource is capable of generating only content not acceptable according to the Accept headers sent in the request.'
];

Route::get('/test', fn() => ErrorResponse::error($error));

$response = $this
->withoutExceptionHandling()
->jsonApi()
->withHeader('Accepts', 'application/json')
->get('/test');

$response->assertExactErrorStatus($error);
$response->assertStatusCode(Response::HTTP_NOT_ACCEPTABLE);
}
}