diff --git a/cake/libs/cake_response.php b/cake/libs/cake_response.php index 900545190c5..3f77dd824af 100644 --- a/cake/libs/cake_response.php +++ b/cake/libs/cake_response.php @@ -334,15 +334,41 @@ public function __construct(array $options = array()) { * */ public function send() { - + if (isset($this->_headers['Location']) && $this->_status === 200) { + $this->statusCode(302); + } + + $codeMesasge = $this->_statusCodes[$this->_status]; + $this->_sendHeader("{$this->_protocol} {$this->_status} {$codeMesasge}"); + $this->_sendHeader('Content-Type', "{$this->_contentType}; charset={$this->_charset}"); + + foreach ($this->_headers as $header => $value) { + $this->_sendHeader($header, $value); + } + $this->_sendContent($this->_body); + } + +/** +* Sends a header to the client +* +* @param $name the header name +* @param $value the header value +*/ + protected function _sendHeader($name, $value = null) { + if (is_null($value)) { + header($name); + } else { + header("{$name}: {$value}"); + } } /** -* Sends the complete headers list to the client +* Sends a content string to the client * +* @param $content string to send as response body */ - public function sendHeaders() { - + protected function _sendContent($content) { + echo $content; } /** diff --git a/cake/tests/cases/libs/cake_response.test.php b/cake/tests/cases/libs/cake_response.test.php index ba32d660811..b992586b458 100644 --- a/cake/tests/cases/libs/cake_response.test.php +++ b/cake/tests/cases/libs/cake_response.test.php @@ -1,6 +1,6 @@ 'gzip', 'Vary' => '*', 'Pragma' => 'no-cache'); $this->assertEquals($response->header(), $headers); } + +/** +* Tests the send method +* +*/ + public function testSend() { + $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent')); + $response->header(array( + 'Content-Language' => 'es', + 'WWW-Authenticate' => 'Negotiate' + )); + $response->body('the response body'); + $response->expects($this->once())->method('_sendContent')->with('the response body'); + $response->expects($this->at(0)) + ->method('_sendHeader')->with('HTTP/1.1 200 OK'); + $response->expects($this->at(1)) + ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8'); + $response->expects($this->at(2)) + ->method('_sendHeader')->with('Content-Language', 'es'); + $response->expects($this->at(3)) + ->method('_sendHeader')->with('WWW-Authenticate', 'Negotiate'); + $response->send(); + } + +/** +* Tests the send method and changing the content type +* +*/ + public function testSendChangingContentYype() { + $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent')); + $response->type('mp3'); + $response->body('the response body'); + $response->expects($this->once())->method('_sendContent')->with('the response body'); + $response->expects($this->at(0)) + ->method('_sendHeader')->with('HTTP/1.1 200 OK'); + $response->expects($this->at(1)) + ->method('_sendHeader')->with('Content-Type', 'audio/mpeg; charset=UTF-8'); + $response->send(); + } + +/** +* Tests the send method and changing the content type +* +*/ + public function testSendChangingContentType() { + $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent')); + $response->type('mp3'); + $response->body('the response body'); + $response->expects($this->once())->method('_sendContent')->with('the response body'); + $response->expects($this->at(0)) + ->method('_sendHeader')->with('HTTP/1.1 200 OK'); + $response->expects($this->at(1)) + ->method('_sendHeader')->with('Content-Type', 'audio/mpeg; charset=UTF-8'); + $response->send(); + } + +/** +* Tests the send method and changing the content type +* +*/ + public function testSendWithLocation() { + $response = $this->getMock('CakeResponse', array('_sendHeader', '_sendContent')); + $response->header('Location', 'http://www.example.com'); + $response->expects($this->at(0)) + ->method('_sendHeader')->with('HTTP/1.1 302 Found'); + $response->expects($this->at(1)) + ->method('_sendHeader')->with('Content-Type', 'text/html; charset=UTF-8'); + $response->expects($this->at(2)) + ->method('_sendHeader')->with('Location', 'http://www.example.com'); + $response->send(); + } } \ No newline at end of file