Skip to content
Permalink
Browse files

Make body() interoperable with PSR7 methods.

  • Loading branch information...
markstory committed May 7, 2016
1 parent 76882b4 commit 11e6529724455d6d39e704cc3cfcf8759baeb85e
Showing with 31 additions and 8 deletions.
  1. +23 −7 src/Http/Client/Request.php
  2. +8 −1 tests/TestCase/Network/Http/RequestTest.php
@@ -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;
/**
@@ -29,13 +30,6 @@ class Request extends Message implements RequestInterface
use MessageTrait;
use RequestTrait;
/**
* Request body to send.
*
* @var mixed
*/
protected $_body;
/**
* Constructor
*
@@ -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;
}
}
@@ -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());
}
/**

0 comments on commit 11e6529

Please sign in to comment.
You can’t perform that action at this time.