Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

fixed SimpleStreamResponseSender ignored defined content length #315

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/ResponseSender/SimpleStreamResponseSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ public function sendStream(SendResponseEvent $event)
if ($event->contentSent()) {
return $this;
}

$response = $event->getResponse();
$stream = $response->getStream();
fpassthru($stream);
$length = $response->getContentLength();

if ($length) {
stream_copy_to_stream($stream, fopen('php://output', 'wb'), $length);
} else {
fpassthru($stream);
}

$event->setContentSent();
}

Expand Down
20 changes: 20 additions & 0 deletions test/ResponseSender/SimpleStreamResponseSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ public function testSendResponseTwoTimesPrintsResponseOnlyOnce()
$this->assertEquals('', $body);
}

public function testSendResponseContentLength()
{
$file = fopen(__DIR__ . '/TestAsset/sample-stream-file.txt', 'rb');
$mockResponse = $this->createMock(Response\Stream::class);
$mockResponse->expects($this->once())->method('getStream')->will($this->returnValue($file));
$mockResponse->expects($this->once())->method('getContentLength')->will($this->returnValue(12));
$mockSendResponseEvent = $this->getSendResponseEventMock($mockResponse);
$responseSender = new SimpleStreamResponseSender();
ob_start();
$responseSender($mockSendResponseEvent);
$body = ob_get_clean();
$expected = file_get_contents(__DIR__ . '/TestAsset/sample-stream-file.txt');
$this->assertEquals(substr($expected, 0, 12), $body);

ob_start();
$responseSender($mockSendResponseEvent);
$body = ob_get_clean();
$this->assertEquals('', $body);
}

protected function getSendResponseEventMock($response)
{
$mockSendResponseEvent = $this->getMockBuilder(ResponseSender\SendResponseEvent::class)
Expand Down