Skip to content

Commit

Permalink
Merge pull request #1893 from akrabat/fix-determine-content-type
Browse files Browse the repository at this point in the history
Ensure determineContentType() returns a valid result
  • Loading branch information
silentworks committed May 25, 2016
2 parents dbd275b + c097e03 commit 0e281eb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Slim/Handlers/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function determineContentType(ServerRequestInterface $request)
$selectedContentTypes = array_intersect(explode(',', $acceptHeader), $this->knownContentTypes);

if (count($selectedContentTypes)) {
return $selectedContentTypes[0];
return current($selectedContentTypes);
}

// handle +json and +xml specially
Expand Down
28 changes: 28 additions & 0 deletions tests/Handlers/AbstractHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,32 @@ public function testHalfValidContentType()

$this->assertEquals('text/html', $return);
}

/**
* Ensure that an acceptable media-type is found in the Accept header even
* if it's not the first in the list.
*/
public function testAcceptableMediaTypeIsNotFirstInList()
{
$request = $this->getMockBuilder('Slim\Http\Request')
->disableOriginalConstructor()
->getMock();

$request->expects($this->any())
->method('getHeaderLine')
->willReturn('text/plain,text/html');

// provide access to the determineContentType() as it's a protected method
$class = new \ReflectionClass(AbstractHandler::class);
$method = $class->getMethod('determineContentType');
$method->setAccessible(true);

// use a mock object here as AbstractHandler cannot be directly instantiated
$abstractHandler = $this->getMockForAbstractClass(AbstractHandler::class);

// call determineContentType()
$return = $method->invoke($abstractHandler, $request);

$this->assertEquals('text/html', $return);
}
}

0 comments on commit 0e281eb

Please sign in to comment.