Implementing and tesing CakeResponse::send()

This commit is contained in:
José Lorenzo Rodríguez 2010-07-31 16:58:13 -04:30
parent d1808db0df
commit 20d1e483cc
2 changed files with 102 additions and 5 deletions

View file

@ -334,15 +334,41 @@ class CakeResponse {
* *
*/ */
public function send() { 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;
} }
/** /**

View file

@ -1,6 +1,6 @@
<?php <?php
App::import('Core', array('CakeResponse', 'CakeRequest')); App::import('Core', 'CakeResponse');
class CakeRequestTestCase extends CakeTestCase { class CakeRequestTestCase extends CakeTestCase {
@ -128,4 +128,75 @@ class CakeRequestTestCase extends CakeTestCase {
$headers += array('Content-Encoding' => 'gzip', 'Vary' => '*', 'Pragma' => 'no-cache'); $headers += array('Content-Encoding' => 'gzip', 'Vary' => '*', 'Pragma' => 'no-cache');
$this->assertEquals($response->header(), $headers); $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();
}
} }