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

Commit

Permalink
Merge branch 'feature/88' into develop
Browse files Browse the repository at this point in the history
Close #88
Fixes #84
  • Loading branch information
weierophinney committed Oct 15, 2015
2 parents e268dd5 + 58088d4 commit e7f81fe
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ All notable changes to this project will be documented in this file, in reverse

### Added

- [#88](https://github.com/zendframework/zend-diactoros/pull/88) updates the
`SapiEmitter` to emit a `Content-Length` header with the content length as
reported by the response body stream.
- [#77](https://github.com/zendframework/zend-diactoros/pull/77) adds a new
response type, `Zend\Diactoros\Response\TextResponse`, for returning plain
text responses. By default, it sets the content type to `text/plain;
Expand Down
4 changes: 4 additions & 0 deletions src/Response/SapiEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function emit(ResponseInterface $response, $maxBufferLevel = null)
throw new RuntimeException('Unable to emit response; headers already sent');
}

if (! $response->hasHeader('Content-Length')) {
$response = $response->withHeader('Content-Length', (string) $response->getBody()->getSize());
}

$this->emitStatusLine($response);
$this->emitHeaders($response);
$this->emitBody($response, $maxBufferLevel);
Expand Down
2 changes: 1 addition & 1 deletion test/Response/SapiEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPUnit_Framework_TestCase as TestCase;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\SapiEmitter;
use Zend\Diactoros\Stream;
use ZendTest\Diactoros\TestAsset\HeaderStack;

class SapiEmitterTest extends TestCase
Expand Down Expand Up @@ -45,6 +44,7 @@ public function testEmitsResponseHeaders()
ob_end_clean();
$this->assertContains('HTTP/1.1 200 OK', HeaderStack::stack());
$this->assertContains('Content-Type: text/plain', HeaderStack::stack());
$this->assertContains('Content-Length: 8', HeaderStack::stack());
}

public function testEmitsMessageBody()
Expand Down
12 changes: 12 additions & 0 deletions test/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Server;
use Zend\Diactoros\Stream;
use ZendTest\Diactoros\TestAsset\HeaderStack;

class ServerTest extends TestCase
Expand Down Expand Up @@ -229,6 +230,7 @@ public function testEmitsHeadersWithMultipleValuesMultipleTimes()
*/
public function testHeaderOrderIsHonoredWhenEmitted($stack)
{
array_pop($stack); // ignore "Content-Length" automatically set by the response emitter
$header = array_pop($stack);
$this->assertContains(
'Set-Cookie: bar=baz; expires=Wed, 8 Oct 2014 10:30; path=/foo/bar; domain=example.com',
Expand All @@ -253,6 +255,16 @@ public function testListenPassesCallableArgumentToCallback()
->method('getHeaders')
->will($this->returnValue([]));

$responseBody = new Stream('php://temp');
$this->response
->expects($this->any())
->method('getBody')
->willReturn($responseBody);
$this->response
->expects($this->any())
->method('withHeader')
->willReturnSelf();

$final = function ($req, $res, $err = null) use ($phpunit, $request, $response, &$invoked) {
$phpunit->assertSame($request, $req);
$phpunit->assertSame($response, $res);
Expand Down

0 comments on commit e7f81fe

Please sign in to comment.