From d4986b5f2469c7f8fcbbc26a581849ccf228cdbf Mon Sep 17 00:00:00 2001 From: Ceeram Date: Wed, 8 Aug 2012 13:13:28 +0200 Subject: [PATCH] add responseHeader() method to new base exception class, ExceptionRenderer will pass the headers to the response. Tests added. --- lib/Cake/Error/ExceptionRenderer.php | 5 +++ lib/Cake/Error/exceptions.php | 41 ++++++++++++++++++- .../Test/Case/Error/ExceptionRendererTest.php | 21 ++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Error/ExceptionRenderer.php b/lib/Cake/Error/ExceptionRenderer.php index 372a643e2ed..fb55b3ad608 100644 --- a/lib/Cake/Error/ExceptionRenderer.php +++ b/lib/Cake/Error/ExceptionRenderer.php @@ -147,6 +147,11 @@ protected function _getController($exception) { $request = new CakeRequest(); } $response = new CakeResponse(array('charset' => Configure::read('App.encoding'))); + + if (method_exists($exception, 'responseHeader')) { + $response->header($exception->responseHeader()); + } + try { if (class_exists('AppController')) { $controller = new CakeErrorController($request, $response); diff --git a/lib/Cake/Error/exceptions.php b/lib/Cake/Error/exceptions.php index 94952f12e4f..3d0757106e6 100644 --- a/lib/Cake/Error/exceptions.php +++ b/lib/Cake/Error/exceptions.php @@ -18,6 +18,43 @@ * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ +/** + * Base class that all Exceptions extend. + * + * @package Cake.Error + */ +class CakeBaseException extends RuntimeException { + +/** + * Array of headers to be passed to CakeResponse::header() + * + * @var array + */ + protected $_responseHeaders = null; + +/** + * Get/set the response header to be used + * + * See also CakeResponse::header() + * + * @param string|array $header. An array of header strings or a single header string + * - an associative array of "header name" => "header value" + * - an array of string headers is also accepted + * @param string $value. The header value. + * @return array + */ + public function responseHeader($header = null, $value = null) { + if ($header) { + if (is_array($header)) { + return $this->_responseHeaders = $header; + } + $this->_responseHeaders = array($header => $value); + } + return $this->_responseHeaders; + } + +} + /** * Parent class for all of the HTTP related exceptions in CakePHP. * All HTTP status/error related exceptions should extend this class so @@ -26,7 +63,7 @@ * @package Cake.Error */ if (!class_exists('HttpException')) { - class HttpException extends RuntimeException { + class HttpException extends CakeBaseException { } } @@ -168,7 +205,7 @@ public function __construct($message = null, $code = 500) { * * @package Cake.Error */ -class CakeException extends RuntimeException { +class CakeException extends CakeBaseException { /** * Array of attributes that are passed in from the constructor, and diff --git a/lib/Cake/Test/Case/Error/ExceptionRendererTest.php b/lib/Cake/Test/Case/Error/ExceptionRendererTest.php index 22d44316d55..04cb2df8c52 100644 --- a/lib/Cake/Test/Case/Error/ExceptionRendererTest.php +++ b/lib/Cake/Test/Case/Error/ExceptionRendererTest.php @@ -480,6 +480,27 @@ public function testError500Message() { $this->assertRegExp('/

An Internal Error Has Occurred<\/h2>/', $result); } +/** + * testExceptionResponseHeader method + * + * @return void + */ + public function testExceptionResponseHeader() { + $exception = new MethodNotAllowedException('Only allowing POST and DELETE'); + $exception->responseHeader(array('Allow: POST, DELETE')); + $ExceptionRenderer = new ExceptionRenderer($exception); + + //Replace response object with mocked object add back the original headers which had been set in ExceptionRenderer constructor + $headers = $ExceptionRenderer->controller->response->header(); + $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('_sendHeader')); + $ExceptionRenderer->controller->response->header($headers); + + $ExceptionRenderer->controller->response->expects($this->at(1))->method('_sendHeader')->with('Allow', 'POST, DELETE'); + ob_start(); + $ExceptionRenderer->render(); + $result = ob_get_clean(); + } + /** * testMissingController method *