mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Fixing RequesHandler::prefers(). It was previously entirely wrong.
It took the ordered list of accept types, and blindly assumed the first in the list was the most preferred. This is an incorrect assumption to make, as all types with the same q value are equal. - Using CakeRequest::parseAccept() to access only the most preferred content types. - Using in_array() to check for the desired type. - Updating tests for RequestHandler.
This commit is contained in:
parent
e2f48b4a95
commit
bb3a1d546b
2 changed files with 15 additions and 12 deletions
|
@ -468,14 +468,17 @@ class RequestHandlerComponent extends Component {
|
||||||
* @see RequestHandlerComponent::setContent()
|
* @see RequestHandlerComponent::setContent()
|
||||||
*/
|
*/
|
||||||
public function prefers($type = null) {
|
public function prefers($type = null) {
|
||||||
$accepts = $this->accepts();
|
$acceptRaw = $this->request->parseAccept();
|
||||||
|
|
||||||
|
if (empty($acceptRaw)) {
|
||||||
|
return $this->ext;
|
||||||
|
}
|
||||||
|
$accepts = array_shift($acceptRaw);
|
||||||
|
$accepts = $this->mapType($accepts);
|
||||||
|
|
||||||
if ($type == null) {
|
if ($type == null) {
|
||||||
if (empty($this->ext)) {
|
if (empty($this->ext) && !empty($accepts)) {
|
||||||
if (is_array($accepts)) {
|
return $accepts[0];
|
||||||
return $accepts[0];
|
|
||||||
}
|
|
||||||
return $accepts;
|
|
||||||
}
|
}
|
||||||
return $this->ext;
|
return $this->ext;
|
||||||
}
|
}
|
||||||
|
@ -484,9 +487,9 @@ class RequestHandlerComponent extends Component {
|
||||||
|
|
||||||
if (count($types) === 1) {
|
if (count($types) === 1) {
|
||||||
if (!empty($this->ext)) {
|
if (!empty($this->ext)) {
|
||||||
return ($types[0] == $this->ext);
|
return in_array($this->ext, $types);
|
||||||
}
|
}
|
||||||
return ($types[0] == $accepts[0]);
|
return in_array($types[0], $accepts);
|
||||||
}
|
}
|
||||||
|
|
||||||
$intersect = array_values(array_intersect($accepts, $types));
|
$intersect = array_values(array_intersect($accepts, $types));
|
||||||
|
|
|
@ -333,8 +333,8 @@ class RequestHandlerComponentTest extends CakeTestCase {
|
||||||
public function testRenderAsWithAttachment() {
|
public function testRenderAsWithAttachment() {
|
||||||
$this->RequestHandler->request = $this->getMock('CakeRequest');
|
$this->RequestHandler->request = $this->getMock('CakeRequest');
|
||||||
$this->RequestHandler->request->expects($this->any())
|
$this->RequestHandler->request->expects($this->any())
|
||||||
->method('accepts')
|
->method('parseAccept')
|
||||||
->will($this->returnValue(array('application/xml')));
|
->will($this->returnValue(array('1.0' => array('application/xml'))));
|
||||||
|
|
||||||
$this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download', 'charset'));
|
$this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download', 'charset'));
|
||||||
$this->RequestHandler->response->expects($this->at(0))
|
$this->RequestHandler->response->expects($this->at(0))
|
||||||
|
@ -387,8 +387,8 @@ class RequestHandlerComponentTest extends CakeTestCase {
|
||||||
$this->RequestHandler->request = $this->getMock('CakeRequest');
|
$this->RequestHandler->request = $this->getMock('CakeRequest');
|
||||||
|
|
||||||
$this->RequestHandler->request->expects($this->once())
|
$this->RequestHandler->request->expects($this->once())
|
||||||
->method('accepts')
|
->method('parseAccept')
|
||||||
->will($this->returnValue(array('application/xml')));
|
->will($this->returnValue(array('1.0' => array('application/xml'))));
|
||||||
|
|
||||||
$this->RequestHandler->response->expects($this->once())->method('download')
|
$this->RequestHandler->response->expects($this->once())->method('download')
|
||||||
->with('myfile.xml');
|
->with('myfile.xml');
|
||||||
|
|
Loading…
Reference in a new issue