Skip to content

Commit

Permalink
Ensure determineContentType() returns a valid result
Browse files Browse the repository at this point in the history
If the acceptable media type in the Accept header is not first in the
list, we need to ensure that we return it correctly.

Fixes #1892
  • Loading branch information
akrabat committed May 25, 2016
1 parent dbd275b commit c097e03
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 c097e03

Please sign in to comment.