From 79672ffe6cb07db3363e32199380cb034f9bfbb9 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Wed, 18 Sep 2019 19:15:30 +0200 Subject: [PATCH] fixed SimpleStreamResponseSender ignored defined content length --- .../SimpleStreamResponseSender.php | 10 +++++++++- .../SimpleStreamResponseSenderTest.php | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/ResponseSender/SimpleStreamResponseSender.php b/src/ResponseSender/SimpleStreamResponseSender.php index 3d1be8f75..4c9d86340 100644 --- a/src/ResponseSender/SimpleStreamResponseSender.php +++ b/src/ResponseSender/SimpleStreamResponseSender.php @@ -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(); } diff --git a/test/ResponseSender/SimpleStreamResponseSenderTest.php b/test/ResponseSender/SimpleStreamResponseSenderTest.php index aca16d9ed..105103b26 100644 --- a/test/ResponseSender/SimpleStreamResponseSenderTest.php +++ b/test/ResponseSender/SimpleStreamResponseSenderTest.php @@ -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)