From 20d1e483cc13320c8e51b7a291296432c2e43872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Sat, 31 Jul 2010 16:58:13 -0430 Subject: [PATCH] Implementing and tesing CakeResponse::send() --- cake/libs/cake_response.php | 34 +++++++-- cake/tests/cases/libs/cake_response.test.php | 73 +++++++++++++++++++- 2 files changed, 102 insertions(+), 5 deletions(-) diff --git a/cake/libs/cake_response.php b/cake/libs/cake_response.php index 900545190..3f77dd824 100644 --- a/cake/libs/cake_response.php +++ b/cake/libs/cake_response.php @@ -334,15 +334,41 @@ class CakeResponse { * */ 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 the complete headers list to the client +* Sends a header to the client * +* @param $name the header name +* @param $value the header value */ - public function sendHeaders() { - + protected function _sendHeader($name, $value = null) { + if (is_null($value)) { + header($name); + } else { + header("{$name}: {$value}"); + } + } + +/** +* Sends a content string to the client +* +* @param $content string to send as response body +*/ + 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 ba32d6608..b992586b4 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