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

Commit

Permalink
Reverse emitStatusLine() and emitHeaders() calls
Browse files Browse the repository at this point in the history
To ensure that the emitted response will always be correct according
with the response object.
  • Loading branch information
lcobucci committed Oct 12, 2017
1 parent fcb80a8 commit 35f5af1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Response/SapiEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function emit(ResponseInterface $response)
{
$this->assertNoPreviousOutput();

$this->emitStatusLine($response);
$this->emitHeaders($response);
$this->emitStatusLine($response);
$this->emitBody($response);
}

Expand Down
6 changes: 6 additions & 0 deletions src/Response/SapiEmitterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ private function assertNoPreviousOutput()
* Emits the status line using the protocol version and status code from
* the response; if a reason phrase is available, it, too, is emitted.
*
* It's important to mention that, in order to prevent PHP from changing
* the status code of the emitted response, this method should be called
* after `emitHeaders()`
*
* @param ResponseInterface $response
*
* @see \Zend\Diactoros\Response\SapiEmitterTrait::emitHeaders()
*/
private function emitStatusLine(ResponseInterface $response)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Response/SapiStreamEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class SapiStreamEmitter implements EmitterInterface
public function emit(ResponseInterface $response, $maxBufferLength = 8192)
{
$this->assertNoPreviousOutput();
$this->emitStatusLine($response);
$this->emitHeaders($response);
$this->emitStatusLine($response);

$range = $this->parseContentRange($response->getHeaderLine('Content-Range'));

Expand Down
4 changes: 2 additions & 2 deletions test/Response/AbstractEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public function testMultipleSetCookieHeadersAreNotReplaced()
$this->emitter->emit($response);

$expectedStack = [
['header' => 'HTTP/1.1 200 OK', 'replace' => true, 'status_code' => 200],
['header' => 'Set-Cookie: foo=bar', 'replace' => false, 'status_code' => 200],
['header' => 'Set-Cookie: bar=baz', 'replace' => false, 'status_code' => 200],
['header' => 'HTTP/1.1 200 OK', 'replace' => true, 'status_code' => 200],
];

$this->assertSame($expectedStack, HeaderStack::stack());
Expand All @@ -84,9 +84,9 @@ public function testDoesNotLetResponseCodeBeOverriddenByPHP()
$this->emitter->emit($response);

$expectedStack = [
['header' => 'HTTP/1.1 202 Accepted', 'replace' => true, 'status_code' => 202],
['header' => 'Location: http://api.my-service.com/12345678', 'replace' => true, 'status_code' => 202],
['header' => 'Content-Type: text/plain', 'replace' => true, 'status_code' => 202],
['header' => 'HTTP/1.1 202 Accepted', 'replace' => true, 'status_code' => 202],
];

$this->assertSame($expectedStack, HeaderStack::stack());
Expand Down
7 changes: 7 additions & 0 deletions test/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,23 @@ public function testEmitsHeadersWithMultipleValuesMultipleTimes()
*/
public function testHeaderOrderIsHonoredWhenEmitted($stack)
{
$header = array_pop($stack);
$this->assertContains('Content-Type: text/plain', $header);

$header = array_pop($stack);
$this->assertContains(
'Set-Cookie: bar=baz; expires=Wed, 8 Oct 2014 10:30; path=/foo/bar; domain=example.com',
$header
);

$header = array_pop($stack);
$this->assertContains(
'Set-Cookie: foo=bar; expires=Wed, 1 Oct 2014 10:30; path=/foo; domain=example.com',
$header
);

$header = array_pop($stack);
$this->assertContains('HTTP/1.1 200 OK', $header);
}

public function testListenPassesCallableArgumentToCallback()
Expand Down

0 comments on commit 35f5af1

Please sign in to comment.