Skip to content
Permalink
Browse files

Refactoring RequestHandler methods to use the response object

  • Loading branch information...
lorenzo committed Aug 2, 2010
1 parent 4fd032b commit 5fb320f31de31f3b21e37d4e34bdbd9d297dc871
@@ -142,7 +142,7 @@ public function startup(&$controller) {
$controller->request->params['isAjax'] = $this->request->is('ajax');
$isRecognized = (
!in_array($this->ext, array('html', 'htm')) &&
in_array($this->ext, array_keys($this->_contentTypeMap))
$this->response->getMimeType($this->ext)
);
if (!empty($this->ext) && $isRecognized) {
@@ -183,12 +183,12 @@ public function beforeRedirect(&$controller, $url, $status = null) {
$url = Router::url($url + array('base' => false));
}
if (!empty($status)) {
$statusCode = $controller->httpCodes($status);
$statusCode = $this->response->httpCodes($status);
$code = key($statusCode);
$msg = $statusCode[$code];
$controller->header("HTTP/1.1 {$code} {$msg}");
$this->response->statusCode($code);
}
echo $this->requestAction($url, array('return', 'bare' => false));
$this->response->body($this->requestAction($url, array('return', 'bare' => false)));
$this->response->send();
$this->_stop();
}
@@ -502,7 +502,7 @@ function renderAs(&$controller, $type) {
$this->__renderType = $type;
$controller->layoutPath = $type;
if (isset($this->_contentTypeMap[$type])) {
if ($this->response->getMimeType($type)) {
$this->respondAs($type, $options);
}
@@ -534,24 +534,18 @@ function renderAs(&$controller, $type) {
* @see RequestHandlerComponent::setContent()
*/
function respondAs($type, $options = array()) {
if (!array_key_exists($type, $this->_contentTypeMap) && strpos($type, '/') === false) {
return false;
}
$defaults = array('index' => 0, 'charset' => null, 'attachment' => false);
$options = array_merge($defaults, $options);
if (strpos($type, '/') === false && isset($this->_contentTypeMap[$type])) {
$cType = null;
if (is_array($this->_contentTypeMap[$type]) && isset($this->_contentTypeMap[$type][$options['index']])) {
$cType = $this->_contentTypeMap[$type][$options['index']];
} elseif (is_array($this->_contentTypeMap[$type]) && isset($this->_contentTypeMap[$type][0])) {
$cType = $this->_contentTypeMap[$type][0];
} elseif (isset($this->_contentTypeMap[$type])) {
$cType = $this->_contentTypeMap[$type];
} else {
$defaults = array('index' => null, 'charset' => null, 'attachment' => false);
$options = $options + $defaults;
$cType = null;
if (strpos($type, '/') === false) {
$cType = $this->response->getMimeType($type);
if ($cType === false) {
return false;
}
if (is_array($cType) && isset($cType[$options['index']])) {
$cType = $cType[$options['index']];
}
if (is_array($cType)) {
if ($this->prefers($cType)) {
$cType = $this->prefers($cType);
@@ -564,10 +558,10 @@ function respondAs($type, $options = array()) {
}
if ($cType != null) {
$header = 'Content-type: ' . $cType;
$this->response->type($cType);
if (!empty($options['charset'])) {
$header .= '; charset=' . $options['charset'];
$this->response->charset($options['charset']);
}
if (!empty($options['attachment'])) {
$this->_header("Content-Disposition: attachment; filename=\"{$options['attachment']}\"");
@@ -625,12 +619,12 @@ public function mapAlias($alias) {
if (is_array($alias)) {
return array_map(array($this, 'mapAlias'), $alias);
}
if (isset($this->_contentTypeMap[$alias])) {
$types = $this->_contentTypeMap[$alias];
if (is_array($types)) {
return $types[0];
$type = $this->response->getMimeType($alias);
if ($type) {
if (is_array($type)) {
return $type[0];
}
return $types;
return $type;
}
return null;
}
@@ -86,6 +86,8 @@ public function testType() {
$response->type(array('keynote' => 'application/keynote'));
$this->assertEquals($response->type('keynote'), 'application/keynote');
$this->assertFalse($response->type('wackytype'));
}
/**
@@ -317,22 +317,17 @@ function testRenderAs() {
* @return void
*/
function testRespondAs() {
$debug = Configure::read('debug');
Configure::write('debug', 0);
$this->RequestHandler->response = $this->getMock('CakeResponse', array('type'));
$this->RequestHandler->response->expects($this->at(0))->method('type')
->with('application/json');
$this->RequestHandler->response->expects($this->at(1))->method('type')
->with('text/xml');
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_header'));
$RequestHandler->expects($this->at(0))->method('_header')
->with('Content-type: application/json');
$RequestHandler->expects($this->at(1))->method('_header')
->with('Content-type: text/xml');
$result = $RequestHandler->respondAs('json');
$result = $this->RequestHandler->respondAs('json');
$this->assertTrue($result);
$result = $RequestHandler->respondAs('text/xml');
$result = $this->RequestHandler->respondAs('text/xml');
$this->assertTrue($result);
Configure::write('debug', $debug);
}
/**
@@ -341,19 +336,14 @@ function testRespondAs() {
* @return void
*/
function testRespondAsWithAttachment() {
$debug = Configure::read('debug');
Configure::write('debug', 0);
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_header'));
$RequestHandler->expects($this->at(0))->method('_header')
->with('Content-Disposition: attachment; filename="myfile.xml"');
$RequestHandler->expects($this->at(1))->method('_header')
->with('Content-type: application/xml');
$this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download'));
$this->RequestHandler->response->expects($this->once())->method('download')
->with('myfile.xml');
$this->RequestHandler->response->expects($this->once())->method('type')
->with('application/xml');
$result = $RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
$result = $this->RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
$this->assertTrue($result);
Configure::write('debug', $debug);
}
/**
@@ -618,10 +608,11 @@ function testAjaxRedirectAsRequestAction() {
App::build(array(
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
), true);
$this->Controller->request = $this->getMock('CakeRequest');
$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
$this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'));
$this->Controller->RequestHandler->request = $this->Controller->request;
$this->Controller->RequestHandler->response = $this->Controller->response;
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
$this->Controller->RequestHandler->expects($this->once())->method('_stop');
@@ -648,8 +639,10 @@ function testAjaxRedirectAsRequestActionStillRenderingLayout() {
), true);
$this->Controller->request = $this->getMock('CakeRequest');
$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
$this->Controller->RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'));
$this->Controller->RequestHandler->request = $this->Controller->request;
$this->Controller->RequestHandler->response = $this->Controller->response;
$this->Controller->request->expects($this->any())->method('is')->will($this->returnValue(true));
$this->Controller->RequestHandler->expects($this->once())->method('_stop');
@@ -682,6 +675,7 @@ function testBeforeRedirectCallbackWithArrayUrl() {
));
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'));
$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader'));
$RequestHandler->request = new CakeRequest('posts/index');
ob_start();
@@ -703,12 +697,13 @@ function testBeforeRedirectCallingHeader() {
$controller = $this->getMock('Controller', array('header'));
$RequestHandler = $this->getMock('RequestHandlerComponent', array('_stop'));
$RequestHandler->response = $this->getMock('CakeResponse', array('_sendHeader','statusCode'));
$RequestHandler->request = $this->getMock('CakeRequest');
$RequestHandler->request->expects($this->once())->method('is')
->with('ajax')
->will($this->returnValue(true));
$controller->expects($this->once())->method('header')->with('HTTP/1.1 403 Forbidden');
$RequestHandler->response->expects($this->once())->method('statusCode')->with(403);
ob_start();
$RequestHandler->beforeRedirect($controller, 'request_handler_test/param_method/first/second', 403);

0 comments on commit 5fb320f

Please sign in to comment.
You can’t perform that action at this time.