Skip to content

Commit

Permalink
Add Client/Response::getStringBody().
Browse files Browse the repository at this point in the history
Deprecate Client/Response::body().
  • Loading branch information
ADmad committed Nov 21, 2018
1 parent 2f56d07 commit af446e8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
21 changes: 18 additions & 3 deletions src/Http/Client/Response.php
Expand Up @@ -55,7 +55,7 @@
* You can get the body string using:
*
* ```
* $content = $response->getBody()->getContents();
* $content = $response->getStringBody();
* ```
*
* If your response body is in XML or JSON you can use
Expand Down Expand Up @@ -141,7 +141,7 @@ class Response extends Message implements ResponseInterface
*/
protected $_deprecatedMagicProperties = [
'cookies' => 'getCookies()',
'body' => 'getBody()->getContents()',
'body' => 'getStringBody()',
'json' => 'getJson()',
'xml' => 'getXml()',
'headers' => 'getHeaders()',
Expand Down Expand Up @@ -556,9 +556,14 @@ public function version()
* @param callable|null $parser The callback to use to decode
* the response body.
* @return mixed The response body.
* @deprecated 3.7.0 Use getStringBody()/getJson()/getXml() instead.
*/
public function body($parser = null)
{
deprecationWarning(
'Response::body() is deprecated. Use getStringBody()/getJson()/getXml() instead.'
);

$stream = $this->stream;
$stream->rewind();
if ($parser) {
Expand All @@ -568,6 +573,16 @@ public function body($parser = null)
return $stream->getContents();
}

/**
* Get the response body as string.
*
* @return string
*/
public function getStringBody()
{
return $this->_getBody();
}

/**
* Get the response body as JSON decoded data.
*
Expand Down Expand Up @@ -641,7 +656,7 @@ protected function _getHeaders()
/**
* Provides magic __get() support.
*
* @return array
* @return string
*/
protected function _getBody()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Http/Client/Adapter/StreamTest.php
Expand Up @@ -152,7 +152,7 @@ public function testSendByUsingCakephpProtocol()
$responses = $stream->send($request, []);
$this->assertInstanceOf('Cake\Http\Client\Response', $responses[0]);

$this->assertEquals(20000, strlen($responses[0]->body()));
$this->assertEquals(20000, strlen($responses[0]->getStringBody()));
}

/**
Expand Down
38 changes: 26 additions & 12 deletions tests/TestCase/Http/Client/ResponseTest.php
Expand Up @@ -96,21 +96,35 @@ public function testHeaderParsing()
*/
public function testBody()
{
$data = [
'property' => 'value'
];
$encoded = json_encode($data);
$this->deprecated(function () {
$data = [
'property' => 'value'
];
$encoded = json_encode($data);

$response = new Response([], $encoded);
$response = new Response([], $encoded);

$this->assertEquals($encoded, $response->getBody()->getContents());
$this->assertEquals($encoded, $response->body());

$this->assertEquals($encoded, $response->getBody()->getContents());
$this->assertEquals($encoded, $response->body());
$result = $response->body('json_decode');
$this->assertEquals($data['property'], $result->property);
$stream = $response->getBody();
$stream->rewind();
$this->assertEquals($encoded, $stream->getContents());
});
}

/**
* Test getStringBody()
*
* @return void
*/
public function getStringBody()
{
$response = new Response([], 'string');

$result = $response->body('json_decode');
$this->assertEquals($data['property'], $result->property);
$stream = $response->getBody();
$stream->rewind();
$this->assertEquals($encoded, $stream->getContents());
$this->assertEquals('string', $response->getStringBody());
}

/**
Expand Down

0 comments on commit af446e8

Please sign in to comment.