Skip to content
This repository
Browse code

add responseHeader() method to new base exception class, ExceptionRen…

…derer will pass the headers to the response.

Tests added.
  • Loading branch information...
commit d4986b5f2469c7f8fcbbc26a581849ccf228cdbf 1 parent 596f2c0
ceeram authored August 08, 2012
5  lib/Cake/Error/ExceptionRenderer.php
@@ -147,6 +147,11 @@ protected function _getController($exception) {
147 147
 			$request = new CakeRequest();
148 148
 		}
149 149
 		$response = new CakeResponse(array('charset' => Configure::read('App.encoding')));
  150
+
  151
+		if (method_exists($exception, 'responseHeader')) {
  152
+			$response->header($exception->responseHeader());
  153
+		}
  154
+
150 155
 		try {
151 156
 			if (class_exists('AppController')) {
152 157
 				$controller = new CakeErrorController($request, $response);
41  lib/Cake/Error/exceptions.php
@@ -19,6 +19,43 @@
19 19
  */
20 20
 
21 21
 /**
  22
+ * Base class that all Exceptions extend.
  23
+ *
  24
+ * @package       Cake.Error
  25
+ */
  26
+class CakeBaseException extends RuntimeException {
  27
+
  28
+/**
  29
+ * Array of headers to be passed to CakeResponse::header()
  30
+ *
  31
+ * @var array
  32
+ */
  33
+	protected $_responseHeaders = null;
  34
+
  35
+/**
  36
+ * Get/set the response header to be used
  37
+ *
  38
+ * See also CakeResponse::header()
  39
+ *
  40
+ * @param string|array $header. An array of header strings or a single header string
  41
+ *	- an associative array of "header name" => "header value"
  42
+ *	- an array of string headers is also accepted
  43
+ * @param string $value. The header value.
  44
+ * @return array
  45
+ */
  46
+	public function responseHeader($header = null, $value = null) {
  47
+		if ($header) {
  48
+			if (is_array($header)) {
  49
+				return $this->_responseHeaders = $header;
  50
+			}
  51
+			$this->_responseHeaders = array($header => $value);
  52
+		}
  53
+		return $this->_responseHeaders;
  54
+	}
  55
+
  56
+}
  57
+
  58
+/**
22 59
  * Parent class for all of the HTTP related exceptions in CakePHP.
23 60
  * All HTTP status/error related exceptions should extend this class so
24 61
  * catch blocks can be specifically typed.
@@ -26,7 +63,7 @@
26 63
  * @package       Cake.Error
27 64
  */
28 65
 if (!class_exists('HttpException')) {
29  
-	class HttpException extends RuntimeException {
  66
+	class HttpException extends CakeBaseException {
30 67
 	}
31 68
 }
32 69
 
@@ -168,7 +205,7 @@ public function __construct($message = null, $code = 500) {
168 205
  *
169 206
  * @package       Cake.Error
170 207
  */
171  
-class CakeException extends RuntimeException {
  208
+class CakeException extends CakeBaseException {
172 209
 
173 210
 /**
174 211
  * Array of attributes that are passed in from the constructor, and
21  lib/Cake/Test/Case/Error/ExceptionRendererTest.php
@@ -481,6 +481,27 @@ public function testError500Message() {
481 481
 	}
482 482
 
483 483
 /**
  484
+ * testExceptionResponseHeader method
  485
+ *
  486
+ * @return void
  487
+ */
  488
+	public function testExceptionResponseHeader() {
  489
+		$exception = new MethodNotAllowedException('Only allowing POST and DELETE');
  490
+		$exception->responseHeader(array('Allow: POST, DELETE'));
  491
+		$ExceptionRenderer = new ExceptionRenderer($exception);
  492
+
  493
+		//Replace response object with mocked object add back the original headers which had been set in ExceptionRenderer constructor
  494
+		$headers = $ExceptionRenderer->controller->response->header();
  495
+		$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  496
+		$ExceptionRenderer->controller->response->header($headers);
  497
+
  498
+		$ExceptionRenderer->controller->response->expects($this->at(1))->method('_sendHeader')->with('Allow', 'POST, DELETE');
  499
+		ob_start();
  500
+		$ExceptionRenderer->render();
  501
+		$result = ob_get_clean();
  502
+	}
  503
+
  504
+/**
484 505
  * testMissingController method
485 506
  *
486 507
  * @return void

0 notes on commit d4986b5

Please sign in to comment.
Something went wrong with that request. Please try again.