Skip to content

Commit

Permalink
Merge pull request #504 from dakota/fix-json-api-ext
Browse files Browse the repository at this point in the history
Cake 3.4 returns false for _ext if not there
  • Loading branch information
ADmad authored Mar 26, 2017
2 parents 924d619 + 13664a5 commit b726256
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Listener/ApiListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public function setupDetectors()

foreach ($detectors as $name => $config) {
$request->addDetector($name, function (Request $request) use ($config) {
if ($request->param('_ext') === $config['ext']) {
if ($config['ext'] !== false && $request->param('_ext') === $config['ext']) {
return true;
}

Expand Down
16 changes: 14 additions & 2 deletions tests/TestCase/Listener/ApiListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,8 @@ public function testSetupDetectorsIntigration()
{
$detectors = [
'json' => ['ext' => 'json', 'accepts' => 'application/json'],
'xml' => ['ext' => 'xml', 'accepts' => 'text/xml']
'xml' => ['ext' => 'xml', 'accepts' => 'text/xml'],
'jsonapi' => ['ext' => false, 'accepts' => 'application/vnd.api+json']
];

$listener = $this
Expand Down Expand Up @@ -1025,7 +1026,9 @@ public function testSetupDetectorsIntigration()
foreach ($detectors as $name => $configuration) {
$request->params['_ext'] = $configuration['ext'];
$request->clearDetectorCache();
$this->assertTrue($request->is($name));
if ($configuration['ext'] !== false) {
$this->assertTrue($request->is($name));
}
}

$request->params['_ext'] = null;
Expand Down Expand Up @@ -1061,6 +1064,15 @@ public function testSetupDetectorsIntigration()
$request->is('api'),
"A request with no extensions should not be considered an api request"
);

//Ensure that no set extension will not result in a true
unset($request->params['_ext']);
$request->clearDetectorCache();
$request->expects($this->any())
->method('accepts')
->will($this->returnValue(false));

$this->assertFalse($request->is('jsonapi'), "A request with no extensions should not be considered an jsonapi request");
}

/**
Expand Down

0 comments on commit b726256

Please sign in to comment.