Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make body() interoperable with PSR7 methods.
  • Loading branch information
markstory committed May 7, 2016
1 parent 76882b4 commit 11e6529
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
30 changes: 23 additions & 7 deletions src/Http/Client/Request.php
Expand Up @@ -16,6 +16,7 @@
use Cake\Core\Exception\Exception;
use Psr\Http\Message\RequestInterface;
use Zend\Diactoros\MessageTrait;
use Zend\Diactoros\Stream;
use Zend\Diactoros\RequestTrait;

/**
Expand All @@ -29,13 +30,6 @@ class Request extends Message implements RequestInterface
use MessageTrait;
use RequestTrait;

/**
* Request body to send.
*
* @var mixed
*/
protected $_body;

/**
* Constructor
*
Expand Down Expand Up @@ -209,4 +203,26 @@ public function version($version = null)
$this->protocol = $version;
return $this;
}

/**
* Get/set the body for the message.
*
* *Warning* This method mutates the request in-place for backwards
* compatibility reasons, and is not part of the PSR7 interface.
*
* @param string|null $body The body for the request. Leave null for get
* @return mixed Either $this or the body value.
* @deprecated 3.3.0 use getBody() and withBody() instead.
*/
public function body($body = null)
{
if ($body === null) {
$body = $this->getBody();
return $body ? $body->__toString() : '';
}
$stream = new Stream('php://memory', 'rw');
$stream->write($body);
$this->stream = $stream;
return $this;
}
}
9 changes: 8 additions & 1 deletion tests/TestCase/Network/Http/RequestTest.php
Expand Up @@ -118,7 +118,14 @@ public function testBody()
*/
public function testBodyInteroperability()
{
$this->markTestIncomplete();
$request = new Request();
$this->assertSame('', $request->body());

$data = '{"json":"data"}';
$request = new Request();
$request->body($data);
$this->assertSame($data, $request->body());
$this->assertSame($data, '' . $request->getBody());
}

/**
Expand Down

0 comments on commit 11e6529

Please sign in to comment.