Skip to content

Commit

Permalink
Implementing and tesing CakeResponse::send()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 31, 2010
1 parent d1808db commit 20d1e48
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 5 deletions.
34 changes: 30 additions & 4 deletions cake/libs/cake_response.php
Expand Up @@ -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 comment has been minimized.

Copy link
@rodrigorm

rodrigorm Aug 2, 2010

Contributor

I think this is $codeMessage =)

$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;
}

/**
Expand Down
73 changes: 72 additions & 1 deletion cake/tests/cases/libs/cake_response.test.php
@@ -1,6 +1,6 @@
<?php

App::import('Core', array('CakeResponse', 'CakeRequest'));
App::import('Core', 'CakeResponse');

class CakeRequestTestCase extends CakeTestCase {

Expand Down Expand Up @@ -128,4 +128,75 @@ public function testHeader() {
$headers += array('Content-Encoding' => '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();
}
}

0 comments on commit 20d1e48

Please sign in to comment.