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

Commit f860801

Browse files
committed
Updated SapiEmitterTest to changes for #90
Since a stream's `getSize()` **CAN** return `null`, we need to **SKIP** injection of the `Content-Length` header when it does. The `CallbackStream` introduced in #90 leverages this feature, so we **MUST** support it.
1 parent 51ea364 commit f860801

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Response/SapiEmitter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public function emit(ResponseInterface $response, $maxBufferLevel = null)
3030
}
3131

3232
if (! $response->hasHeader('Content-Length')) {
33-
$response = $response->withHeader('Content-Length', (string) $response->getBody()->getSize());
33+
// PSR-7 indicates int OR null for the stream size; for null values,
34+
// we will not auto-inject the Content-Length.
35+
if (null !== $response->getBody()->getSize()) {
36+
$response = $response->withHeader('Content-Length', (string) $response->getBody()->getSize());
37+
}
3438
}
3539

3640
$this->emitStatusLine($response);

test/Response/SapiEmitterTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace ZendTest\Diactoros\Response;
1111

1212
use PHPUnit_Framework_TestCase as TestCase;
13+
use Psr\Http\Message\ResponseInterface;
14+
use Psr\Http\Message\StreamInterface;
1315
use Zend\Diactoros\Response;
1416
use Zend\Diactoros\Response\SapiEmitter;
1517
use ZendTest\Diactoros\TestAsset\HeaderStack;
@@ -57,4 +59,21 @@ public function testEmitsMessageBody()
5759
$this->expectOutputString('Content!');
5860
$this->emitter->emit($response);
5961
}
62+
63+
public function testDoesNotInjectContentLengthHeaderIfStreamSizeIsUnknown()
64+
{
65+
$stream = $this->prophesize('Psr\Http\Message\StreamInterface');
66+
$stream->__toString()->willReturn('Content!');
67+
$stream->getSize()->willReturn(null);
68+
$response = (new Response())
69+
->withStatus(200)
70+
->withBody($stream->reveal());
71+
72+
ob_start();
73+
$this->emitter->emit($response);
74+
ob_end_clean();
75+
foreach (HeaderStack::stack() as $header) {
76+
$this->assertNotContains('Content-Length:', $header);
77+
}
78+
}
6079
}

0 commit comments

Comments
 (0)