diff --git a/tests/HTTP/MessageTest.php b/tests/HTTP/MessageTest.php index 0ca19ba..78e5931 100644 --- a/tests/HTTP/MessageTest.php +++ b/tests/HTTP/MessageTest.php @@ -42,6 +42,45 @@ function testStringBody() { } + /** + * @expectedException \UnexpectedValueException + */ + function testCallbackBodyAsString() { + + $body = $this->createCallback(); + + $message = new MessageMock(); + $message->setBody($body); + + $message->getBodyAsString(); + + } + + /** + * @expectedException \UnexpectedValueException + */ + function testCallbackBodyAsStream() { + + $body = $this->createCallback(); + + $message = new MessageMock(); + $message->setBody($body); + + $message->getBodyAsStream(); + + } + + function testGetBodyWhenCallback() { + + $body = $this->createCallback(); + + $message = new MessageMock(); + $message->setBody($body); + + $this->assertEquals($body, $message->getBody()); + + } + /** * It's possible that streams contains more data than the Content-Length. * @@ -219,6 +258,15 @@ function testHasHeaders() { } + private function createCallback() + { + return function() { + $fd = fopen('php://output', 'r+'); + fwrite($fd, 'foo'); + fclose($fd); + }; + } + } class MessageMock extends Message { } diff --git a/tests/HTTP/SapiTest.php b/tests/HTTP/SapiTest.php index 158ce21..935b8f6 100644 --- a/tests/HTTP/SapiTest.php +++ b/tests/HTTP/SapiTest.php @@ -164,4 +164,25 @@ function testSendLimitedByContentLengthStream() { } + /** + * @runInSeparateProcess + * @depends testSend + */ + function testSendWorksWithCallbackAsBody() { + $response = new Response(200, [], function() { + $fd = fopen('php://output', 'r+'); + fwrite($fd, 'foo'); + fclose($fd); + }); + + ob_start(); + + Sapi::sendResponse($response); + + $result = ob_get_clean(); + + $this->assertEquals('foo', $result); + + } + }