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 1 commit
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
Next Next commit
Server should always respond with `Content-type: application/vnd.api+…
…json` as per specification.
  • Loading branch information
bbrala committed Oct 26, 2018
commit a631c502fab9793e843c38da8fa8af3086dd6e6b
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ 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`

## 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
6 changes: 4 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']);
}

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

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

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

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

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

Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/HandleResponsesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Tests\Unit;

use Swis\JsonApi\Server\Traits\HandleResponses;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Tests\TestCase;
use Tests\TestClasses\TestModel;

Expand All @@ -29,6 +30,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 +40,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 +50,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 +60,12 @@ 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);
}
}