Skip to content

Commit

Permalink
Merge pull request #515 from bravo-kernel/jsonapi-201
Browse files Browse the repository at this point in the history
Return 201 for successfully created JSON API records
  • Loading branch information
jippi authored Apr 6, 2017
2 parents 061cf3a + 7cde63b commit 41aba34
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 10 deletions.
10 changes: 7 additions & 3 deletions src/Listener/JsonApiListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ public function beforeHandle(Event $event)
}

/**
* afterSave() event used to respond with `Location` header pointing to
* the newly created resources. Only applied to `add` actions as described
* at http://jsonapi.org/format/#crud-creating-responses-201.
* afterSave() event used to respond with Status Code 201 for newly
* created resources. Only applied to `add` actions as described at
* http://jsonapi.org/format/#crud-creating-responses-201.
*
* @param \Cake\Event\Event $event Event
* @return false|null
Expand All @@ -127,6 +127,10 @@ public function afterSave($event)
return false;
}

if ($event->subject()->created) {
$this->_controller()->response->statusCode(201);
}

$this->_insertBelongsToDataIntoEventFindResult($event);

$this->render($event->subject);
Expand Down
34 changes: 34 additions & 0 deletions tests/App/Controller/CountriesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace Crud\Test\App\Controller;

use Cake\Controller\Controller;
use Crud\Controller\ControllerTrait;

/**
* Test controller for JsonApiListener integration tests.
*/
class CountriesController extends Controller
{

use ControllerTrait;

public $paginate = [
'limit' => 3
];

public $components = [
'RequestHandler',
'Crud.Crud' => [
'actions' => [
'Crud.Index',
'Crud.View',
'Crud.Add',
'Crud.Edit',
'Crud.Delete',
],
'listeners' => [
'Crud.JsonApi'
]
]
];
}
4 changes: 2 additions & 2 deletions tests/Fixture/CountriesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CountriesFixture extends TestFixture
];

public $records = [
['id' => 1, 'code' => 'NL', 'name' => 'The Netherlands', 'currency_id' => 1],
['id' => 2, 'code' => 'BE', 'name' => 'Belgium', 'currency_id' => 1],
['code' => 'NL', 'name' => 'The Netherlands', 'currency_id' => 1],
['code' => 'BE', 'name' => 'Belgium', 'currency_id' => 1],
];
}
6 changes: 3 additions & 3 deletions tests/Fixture/CulturesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class CulturesFixture extends TestFixture
];

public $records = [
['id' => 1, 'code' => 'nl-NL', 'name' => 'Dutch', 'country_id' => 1],
['id' => 2, 'code' => 'nl-BE', 'name' => 'Dutch (Belgium)', 'country_id' => 2],
['id' => 3, 'code' => 'fr-BE', 'name' => 'French (Belgium)', 'country_id' => 2],
['code' => 'nl-NL', 'name' => 'Dutch', 'country_id' => 1],
['code' => 'nl-BE', 'name' => 'Dutch (Belgium)', 'country_id' => 2],
['code' => 'fr-BE', 'name' => 'French (Belgium)', 'country_id' => 2],
];
}
4 changes: 2 additions & 2 deletions tests/Fixture/CurrenciesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CurrenciesFixture extends TestFixture
];

public $records = [
['id' => 1, 'code' => 'EUR', 'name' => 'Euro'],
['id' => 2, 'code' => 'USD', 'name' => 'US Dollar'],
['code' => 'EUR', 'name' => 'Euro'],
['code' => 'USD', 'name' => 'US Dollar'],
];
}
92 changes: 92 additions & 0 deletions tests/TestCase/Controller/JsonApiIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
namespace Crud\TestCase\Controller;

use Cake\Routing\Router;
use Crud\TestSuite\IntegrationTestCase;

class JsonApiIntegrationTest extends IntegrationTestCase
{

public $fixtures = [
'plugin.crud.countries',
'plugin.crud.currencies'
];

/**
* Set up required RESTful resource routes.
*/
public function setUp()
{
Router::scope('/', function ($routes) {
$routes->resources('Countries', [
'inflect' => 'dasherize'
]);
});
}

/**
* Test most basic `index` action
*
* @return void
*/
public function testGet()
{
$this->configRequest([
'headers' => ['Accept' => 'application/vnd.api+json']
]);

$this->get('/countries');

$this->assertResponseOk();
$this->_assertJsonApiResponse();
$this->assertResponseCode(200);
$this->assertResponseNotEmpty();
}

/**
* Make sure HTTP Status Code 201 is returned after successfully
* creating a new record using POST method.
*
* @link http://jsonapi.org/format/#crud-creating-responses-201
* @link https://github.com/FriendsOfCake/crud/issues/496
* @return void
*/
public function testSuccessfulPostReturnsStatusCode201()
{
$postData = [
'data' => [
'type' => 'countries',
'attributes' => [
'code' => 'NZ',
'name' => 'New Zealand',
'currency_id' => 1
]
]
];

$this->configRequest([
'headers' => [
'Accept' => 'application/vnd.api+json',
'Content-Type' => 'application/vnd.api+json'
],
'input' => json_encode($postData)
]);

$this->post('/countries');

$this->assertResponseOk();
$this->_assertJsonApiResponse();
$this->assertResponseCode(201);
$this->assertResponseNotEmpty();
}

/**
* Helper method to prevent repetition since all JSON API responses
* must pass this test.
*/
protected function _assertJsonApiResponse()
{
$this->assertHeader('Content-Type', 'application/vnd.api+json');
$this->assertContentType('application/vnd.api+json');
}
}

0 comments on commit 41aba34

Please sign in to comment.