Skip to content

Commit

Permalink
Merge pull request #2803 from mapogolions/fix/emit-non-seekable-stream
Browse files Browse the repository at this point in the history
Emit non seekable streams
  • Loading branch information
l0gicgate committed Aug 15, 2019
2 parents a1ca5e9 + a023af2 commit 86a22f7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
8 changes: 6 additions & 2 deletions Slim/ResponseEmitter.php
Expand Up @@ -133,7 +133,11 @@ public function isResponseEmpty(ResponseInterface $response): bool
if (in_array($response->getStatusCode(), [204, 205, 304], true)) {
return true;
}
$contents = (string) $response->getBody();
return !strlen($contents);
$stream = $response->getBody();
$seekable = $stream->isSeekable();
if ($seekable) {
$stream->rewind();
}
return $seekable ? $stream->read(1) === '' : $stream->eof();
}
}
2 changes: 2 additions & 0 deletions tests/AppTest.php
Expand Up @@ -1564,6 +1564,7 @@ public function testRun()
$body .= $args[0];
$this->__toString()->willReturn($body);
});
$streamProphecy->read(1)->willReturn('_');
$streamProphecy->read('11')->will(function () {
$this->eof()->willReturn(true);
return $this->reveal()->__toString();
Expand Down Expand Up @@ -1616,6 +1617,7 @@ public function testRunWithoutPassingInServerRequest()
$body .= $args[0];
$this->__toString()->willReturn($body);
});
$streamProphecy->read(1)->willReturn('_');
$streamProphecy->read('11')->will(function () {
$this->eof()->willReturn(true);
return $this->reveal()->__toString();
Expand Down
33 changes: 30 additions & 3 deletions tests/ResponseEmitterTest.php
Expand Up @@ -38,7 +38,7 @@ public function testRespond()
$this->expectOutputString('Hello');
}

public function testRespondNoContent()
public function testResposeWithNoContentSkipsContentTypeAndContentLength()
{
$response = $this
->createResponse()
Expand All @@ -55,7 +55,7 @@ public function testRespondNoContent()
$this->expectOutputString('');
}

public function testNonEmptyResponse()
public function testNonEmptyResponseDoesNotSkipContentTypeAndContentLength()
{
$response = $this
->createResponse()
Expand Down Expand Up @@ -197,7 +197,34 @@ public function testIsResponseEmptyWithNonEmptyBodyAndTriggeringStatusCode()
$this->assertTrue($responseEmitter->isResponseEmpty($response));
}

public function testAvoidReadFromSlowStreamAccordingStatus()
public function testIsResponseEmptyDoesNotReadAllDataFromNonEmptySeekableResponse()
{
$body = $this->createStream('Hello');
$response = $this
->createResponse(200)
->withBody($body);
$responseEmitter = new ResponseEmitter();

$responseEmitter->isResponseEmpty($response);

$this->assertTrue($body->isSeekable());
$this->assertFalse($body->eof());
}

public function testIsResponseEmptyDoesNotDrainNonSeekableResponseWithContent()
{
$resource = popen('echo 12', 'r');
$body = $this->getStreamFactory()->createStreamFromResource($resource);
$response = $this->createResponse(200)->withBody($body);
$responseEmitter = new ResponseEmitter();

$responseEmitter->isResponseEmpty($response);

$this->assertFalse($body->isSeekable());
$this->assertSame('12', trim((string) $body));
}

public function testAvoidReadFromSlowStreamAccordingToStatus()
{
$body = new SlowPokeStream();
$response = $this
Expand Down

0 comments on commit 86a22f7

Please sign in to comment.