mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Fixing respondAs() so it can be called multiple times. Test cases added for respondAs. Fixes #842
This commit is contained in:
parent
e18e0cc64e
commit
991d035806
2 changed files with 45 additions and 7 deletions
|
@ -690,7 +690,6 @@ class RequestHandlerComponent extends Object {
|
||||||
* like 'application/x-shockwave'.
|
* like 'application/x-shockwave'.
|
||||||
* @param array $options If $type is a friendly type name that is associated with
|
* @param array $options If $type is a friendly type name that is associated with
|
||||||
* more than one type of content, $index is used to select which content-type to use.
|
* more than one type of content, $index is used to select which content-type to use.
|
||||||
*
|
|
||||||
* @return boolean Returns false if the friendly type name given in $type does
|
* @return boolean Returns false if the friendly type name given in $type does
|
||||||
* not exist in the type map, or if the Content-type header has
|
* not exist in the type map, or if the Content-type header has
|
||||||
* already been set by this method.
|
* already been set by this method.
|
||||||
|
@ -699,9 +698,6 @@ class RequestHandlerComponent extends Object {
|
||||||
*/
|
*/
|
||||||
function respondAs($type, $options = array()) {
|
function respondAs($type, $options = array()) {
|
||||||
$this->__initializeTypes();
|
$this->__initializeTypes();
|
||||||
if ($this->__responseTypeSet != null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!array_key_exists($type, $this->__requestContent) && strpos($type, '/') === false) {
|
if (!array_key_exists($type, $this->__requestContent) && strpos($type, '/') === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -738,10 +734,10 @@ class RequestHandlerComponent extends Object {
|
||||||
$header .= '; charset=' . $options['charset'];
|
$header .= '; charset=' . $options['charset'];
|
||||||
}
|
}
|
||||||
if (!empty($options['attachment'])) {
|
if (!empty($options['attachment'])) {
|
||||||
header("Content-Disposition: attachment; filename=\"{$options['attachment']}\"");
|
$this->_header("Content-Disposition: attachment; filename=\"{$options['attachment']}\"");
|
||||||
}
|
}
|
||||||
if (Configure::read() < 2 && !defined('CAKEPHP_SHELL')) {
|
if (Configure::read() < 2 && !defined('CAKEPHP_SHELL')) {
|
||||||
@header($header);
|
$this->_header($header);
|
||||||
}
|
}
|
||||||
$this->__responseTypeSet = $cType;
|
$this->__responseTypeSet = $cType;
|
||||||
return true;
|
return true;
|
||||||
|
@ -749,6 +745,17 @@ class RequestHandlerComponent extends Object {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper for header() so calls can be easily tested.
|
||||||
|
*
|
||||||
|
* @param string $header The header to be sent.
|
||||||
|
* @return void
|
||||||
|
* @access protected
|
||||||
|
*/
|
||||||
|
function _header($header) {
|
||||||
|
header($header);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current response type (Content-type header), or null if none has been set
|
* Returns the current response type (Content-type header), or null if none has been set
|
||||||
*
|
*
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
App::import('Controller', 'Controller', false);
|
App::import('Controller', 'Controller', false);
|
||||||
App::import('Component', array('RequestHandler'));
|
App::import('Component', array('RequestHandler'));
|
||||||
|
|
||||||
Mock::generatePartial('RequestHandlerComponent', 'NoStopRequestHandler', array('_stop'));
|
Mock::generatePartial('RequestHandlerComponent', 'NoStopRequestHandler', array('_stop', '_header'));
|
||||||
Mock::generatePartial('Controller', 'RequestHandlerMockController', array('header'));
|
Mock::generatePartial('Controller', 'RequestHandlerMockController', array('header'));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -308,6 +308,37 @@ class RequestHandlerComponentTest extends CakeTestCase {
|
||||||
$this->assertEqual($this->Controller->viewPath, 'request_handler_test' . DS . 'js');
|
$this->assertEqual($this->Controller->viewPath, 'request_handler_test' . DS . 'js');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test that respondAs works as expected.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testRespondAs() {
|
||||||
|
$RequestHandler = new NoStopRequestHandler();
|
||||||
|
$RequestHandler->expectAt(0, '_header', array('Content-Type: application/json'));
|
||||||
|
$RequestHandler->expectAt(1, '_header', array('Content-Type: text/xml'));
|
||||||
|
|
||||||
|
$result = $RequestHandler->respondAs('json');
|
||||||
|
$this->assertTrue($result);
|
||||||
|
|
||||||
|
$result = $RequestHandler->respondAs('text/xml');
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test that attachment headers work with respondAs
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testRespondAsWithAttachment() {
|
||||||
|
$RequestHandler = new NoStopRequestHandler();
|
||||||
|
$RequestHandler->expectAt(0, '_header', array('Content-Disposition: attachment; filename="myfile.xml"'));
|
||||||
|
$RequestHandler->expectAt(1, '_header', array('Content-Type: text/xml'));
|
||||||
|
|
||||||
|
$result = $RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
|
||||||
|
$this->assertTrue($result);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test that calling renderAs() more than once continues to work.
|
* test that calling renderAs() more than once continues to work.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue