Skip to content

Commit

Permalink
Add body() method and add support for deserializers.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Dec 27, 2012
1 parent 873d0e4 commit 53e5fd3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
32 changes: 25 additions & 7 deletions lib/Cake/Network/Http/Response.php
Expand Up @@ -22,6 +22,7 @@
*
* ### Check the status code
*
*
*/
class Response {

Expand All @@ -44,21 +45,21 @@ class Response {
protected $_headers;

/**
* The response content
* The response body
*
* @var string
*/
protected $_content;
protected $_body;

/**
* Constructor
*
* @param array $headers Unparsed headers.
* @param string $content The response body.
* @param string $body The response body.
*/
public function __construct($headers, $content) {
public function __construct($headers, $body) {
$this->_parseHeaders($headers);
$this->_content = $content;
$this->_body = $body;
}

/**
Expand Down Expand Up @@ -157,8 +158,25 @@ public function header($name = null) {
return $this->_headers[$name];
}

public function content($content) {
return $this->_content;
/**
* Get the response body.
*
* By passing in a $parser callable, you can get the decoded
* response content back.
*
* For example to get the json data as an object:
*
* `$body = $response->body('json_decode');`
*
* @param callable $parser The callback to use to decode
* the response body.
* @return mixed The response body.
*/
public function body($parser = null) {
if ($parser) {
return $parser($this->_body);
}
return $this->_body;
}

}
17 changes: 15 additions & 2 deletions lib/Cake/Test/TestCase/Network/Http/ResponseTest.php
Expand Up @@ -40,8 +40,21 @@ public function testHeaderParsing() {
);
}

public function testContent() {
$this->markTestIncomplete();
/**
* Test body()
*
* @return void
*/
public function testBody() {
$data = [
'property' => 'value'
];
$encoded = json_encode($data);

$response = new Response([], $encoded);
$result = $response->body('json_decode');
$this->assertEquals($data['property'], $result->property);
$this->assertEquals($encoded, $response->body());
}

/**
Expand Down

0 comments on commit 53e5fd3

Please sign in to comment.