Skip to content

Commit

Permalink
Fixing respondAs() so it can be called multiple times. Test cases add…
Browse files Browse the repository at this point in the history
…ed for respondAs. Fixes #842
  • Loading branch information
markstory committed Jul 1, 2010
1 parent e18e0cc commit 991d035
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
19 changes: 13 additions & 6 deletions cake/libs/controller/components/request_handler.php
Expand Up @@ -690,7 +690,6 @@ function renderAs(&$controller, $type) {
* like 'application/x-shockwave'.
* @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.
*
* @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
* already been set by this method.
Expand All @@ -699,9 +698,6 @@ function renderAs(&$controller, $type) {
*/
function respondAs($type, $options = array()) {
$this->__initializeTypes();
if ($this->__responseTypeSet != null) {
return false;
}
if (!array_key_exists($type, $this->__requestContent) && strpos($type, '/') === false) {
return false;
}
Expand Down Expand Up @@ -738,17 +734,28 @@ function respondAs($type, $options = array()) {
$header .= '; charset=' . $options['charset'];
}
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')) {
@header($header);
$this->_header($header);
}
$this->__responseTypeSet = $cType;
return true;
}
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
*
Expand Down
Expand Up @@ -20,7 +20,7 @@
App::import('Controller', 'Controller', false);
App::import('Component', array('RequestHandler'));

Mock::generatePartial('RequestHandlerComponent', 'NoStopRequestHandler', array('_stop'));
Mock::generatePartial('RequestHandlerComponent', 'NoStopRequestHandler', array('_stop', '_header'));
Mock::generatePartial('Controller', 'RequestHandlerMockController', array('header'));

/**
Expand Down Expand Up @@ -308,6 +308,37 @@ function testRenderAs() {
$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.
*
Expand Down

0 comments on commit 991d035

Please sign in to comment.