also handle lowercase headers

This commit is contained in:
Bernhard Posselt 2014-06-11 01:20:09 +02:00
parent 0252d39bb6
commit 93169eca1e
2 changed files with 27 additions and 18 deletions

View File

@ -70,28 +70,28 @@ abstract class Controller {
}
/**
* Parses an HTTP accept header and returns the supported responder type
* @param string $acceptHeader
* @return string the responder type
*/
public function getResponderByHTTPHeader($acceptHeader) {
$headers = explode(',', $acceptHeader);
/**
* Parses an HTTP accept header and returns the supported responder type
* @param string $acceptHeader
* @return string the responder type
*/
public function getResponderByHTTPHeader($acceptHeader) {
$headers = explode(',', $acceptHeader);
// return the first matching responder
foreach ($headers as $header) {
$header = trim($header);
// return the first matching responder
foreach ($headers as $header) {
$header = strtolower(trim($header));
$responder = str_replace('application/', '', $header);
$responder = str_replace('application/', '', $header);
if (array_key_exists($responder, $this->responders)) {
return $responder;
}
}
if (array_key_exists($responder, $this->responders)) {
return $responder;
}
}
// no matching header defaults to json
return 'json';
}
// no matching header defaults to json
return 'json';
}
/**

View File

@ -185,4 +185,13 @@ class ControllerTest extends \PHPUnit_Framework_TestCase {
}
public function testResponderAcceptHeaderParsedUpperCase() {
$responder = $this->controller->getResponderByHTTPHeader(
'*/*, apPlication/ToM, application/json'
);
$this->assertEquals('tom', $responder);
}
}