Implementing some function in the response object, adding test case

This commit is contained in:
José Lorenzo Rodríguez 2010-07-31 12:33:02 -04:30
parent ffd7066fa4
commit f432de830c
2 changed files with 84 additions and 3 deletions

View file

@ -191,6 +191,19 @@ class CakeResponse {
* @return void
*/
public function __construct(array $options = array(), CakeRequest $request = null) {
$this->_request = $request;
if (isset($options['body'])) {
$this->body($options['body']);
}
if (isset($options['status'])) {
$this->statusCode($options['status']);
}
if (isset($options['type'])) {
$this->type($options['type']);
}
if (isset($options['encoding'])) {
$this->encoding($options['encoding']);
}
}
/**
@ -225,7 +238,10 @@ class CakeResponse {
* @return string current message buffer if $content param is passed as null
*/
public function body($content = null) {
if (is_null($content)) {
return $this->_body;
}
return $this->_body = $content;
}
/**
@ -233,10 +249,48 @@ class CakeResponse {
* if $code is null the current code is returned
*
* @param integer $code
* @return integer current status code if $code param is passed as null
* @return integer current status code
*/
public function statusCode($code = null) {
if (is_null($code)) {
return $this->_status;
}
if (!isset($this->_statusCodes[$code])) {
throw new OutOfRangeException(__('Unknown status code'));
}
return $this->_status = $code;
}
/**
* Sets the response content type. It can be either a file extension
* which will be mapped internally to a mime-type or a string representing a mime-type
* if $contentType is null the current content type is returned
*
* @param string $contentType
* @return string current content type
*/
public function type($contentType = null) {
if (is_null($contentType)) {
return $this->_contentType;
}
if (isset($this->_mimeTypes[$contentType])) {
$contentType = $this->_mimeTypes[$contentType];
}
return $this->_contentType = $contentType;
}
/**
* Sets the response encoding or charset
* if $encoding is null the current encoding is returned
*
* @param string $encoding
* @return string current status code
*/
public function encoding($encoding = null) {
if (is_null($encoding)) {
return $this->_encoding;
}
return $this->_encoding = $encoding;
}
/**

View file

@ -0,0 +1,27 @@
<?php
App::import('Core', array('CakeResponse', 'CakeRequest'));
class CakeRequestTestCase extends CakeTestCase {
public function testConstruct() {
$response = new CakeResponse();
$this->assertNull($response->body());
$this->assertEquals($response->encoding(), 'UTF-8');
$this->assertEquals($response->type(), 'text/html');
$this->assertEquals($response->statusCode(), 200);
$options = array(
'body' => 'This is the body',
'encoding' => 'my-custom-encoding',
'type' => 'mp3',
'status' => '203'
);
$response = new CakeResponse($options);
$this->assertEquals($response->body(), 'This is the body');
$this->assertEquals($response->encoding(), 'my-custom-encoding');
$this->assertEquals($response->type(), 'audio/mpeg');
$this->assertEquals($response->statusCode(), 203);
}
}