Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.
Merged
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
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ All Notable changes to `json-api-server` will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## Unreleased

### Fixed

- Server should always respond with `Content-type: application/vnd.api+json`
- Server should not escape slashes in responses (issue #11)

## 0.3.3

### Fixed

- The `authentication_test.stub` called `PUT` instead of `PATCH` (issue #14)

## 0.3.2

self
### Fixed

- Enable disable middleware for tests and add "Accept: application/vnd.api+json" header when running test calls.
Expand Down
8 changes: 6 additions & 2 deletions src/Services/ResponseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ protected function createResponse($responseModel, $content)
if ($responseModel instanceof RespondError) {
$errors = $this->formatErrors($responseModel, $content);

return response($errors, $responseModel->getStatusCode());
return response()->json($errors, $responseModel->getStatusCode(), ['Content-Type' => 'application/vnd.api+json'], JSON_UNESCAPED_SLASHES);
}

return response($content, $responseModel->getStatusCode());
return response()->json($content, $responseModel->getStatusCode(), ['Content-Type' => 'application/vnd.api+json'], JSON_UNESCAPED_SLASHES);
}

public function respondWithResourceCollection($strResponseModel, $content)
Expand All @@ -33,6 +33,8 @@ public function respondWithResourceCollection($strResponseModel, $content)

return (new BaseApiCollectionResource($content))
->response()
->setEncodingOptions(JSON_UNESCAPED_SLASHES)
->header('Content-Type', 'application/vnd.api+json')
->setStatusCode($responseModel->getStatusCode());
}

Expand All @@ -45,6 +47,8 @@ public function responseWithResource($strResponseModel, $content)

return (new BaseApiResource($content))
->response()
->setEncodingOptions(JSON_UNESCAPED_SLASHES)
->header('Content-Type', 'application/vnd.api+json')
->setStatusCode($responseModel->getStatusCode());
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/HandleResponsesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function test_respond_with_forbidden()
$response = $this->mock->respondWithForbidden('FORBIDDEN');

$this->assertEquals('FORBIDDEN', json_decode($response->getContent())->errors[0]->detail);
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-Type'));
$this->assertEquals(403, $response->getStatusCode());
}

Expand All @@ -38,6 +39,7 @@ public function test_respond_with_bad_request()
$response = $this->mock->respondWithBadRequest('BAD REQUEST');

$this->assertEquals('BAD REQUEST', json_decode($response->getContent())->errors[0]->detail);
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-Type'));
$this->assertEquals(400, $response->getStatusCode());
}

Expand All @@ -47,6 +49,7 @@ public function test_respond_with_not_found()
$response = $this->mock->respondWithNotFound('NOT FOUND');

$this->assertEquals('NOT FOUND', json_decode($response->getContent())->errors[0]->detail);
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-Type'));
$this->assertEquals(404, $response->getStatusCode());
}

Expand All @@ -56,9 +59,11 @@ public function test_respond_with_collection_model()
$model = new TestModel();
$model->body = 'TEST';
$response = $this->mock->respondWithCollection($model);

$responseBody = json_decode($response->getContent())->data;

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-Type'));
$this->assertEquals('TEST', $responseBody->attributes->body);
}
}