Skip to content

Commit

Permalink
sendBody tests + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
evert committed Oct 7, 2012
1 parent dba804f commit 693c250
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Sabre/HTTP/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function sendBody($body) {
if (is_scalar($body)) {
fwrite($this->getBody(), $body);
} elseif (is_resource($body)) {
stream_copy_to_stream($this->getBody(), $body);
stream_copy_to_stream($body, $this->getBody());
} else {
throw new \InvalidArgumentException('You must either pass a string or a stream');
}
Expand All @@ -59,7 +59,7 @@ public function sendBody($body) {
*/
public function getBody() {

if (is_null($body)) {
if (is_null($this->body)) {
// Creating a new body stream
$this->body = fopen('php://temp', 'r+');
}
Expand Down
29 changes: 29 additions & 0 deletions tests/Sabre/HTTP/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,35 @@ function testHeaders() {

}

function testSendBody() {

$message = new MessageMock();

// String
$message->sendBody('foo');

// Stream
$h = fopen('php://memory','r+');
fwrite($h,'bar');
rewind($h);
$message->sendBody($h);

$body = $message->getBody();
rewind($body);

$this->assertEquals('foobar', stream_get_contents($body));

}

/**
* @expectedException InvalidArgumentException
*/
function testSendBadBody() {

$message = new MessageMock();
$message->sendBody(array());

}

}

Expand Down

0 comments on commit 693c250

Please sign in to comment.