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

Commit 35f5af1

Browse files
committed
Reverse emitStatusLine() and emitHeaders() calls
To ensure that the emitted response will always be correct according with the response object.
1 parent fcb80a8 commit 35f5af1

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
lines changed

src/Response/SapiEmitter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public function emit(ResponseInterface $response)
2626
{
2727
$this->assertNoPreviousOutput();
2828

29-
$this->emitStatusLine($response);
3029
$this->emitHeaders($response);
30+
$this->emitStatusLine($response);
3131
$this->emitBody($response);
3232
}
3333

src/Response/SapiEmitterTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ private function assertNoPreviousOutput()
3838
* Emits the status line using the protocol version and status code from
3939
* the response; if a reason phrase is available, it, too, is emitted.
4040
*
41+
* It's important to mention that, in order to prevent PHP from changing
42+
* the status code of the emitted response, this method should be called
43+
* after `emitHeaders()`
44+
*
4145
* @param ResponseInterface $response
46+
*
47+
* @see \Zend\Diactoros\Response\SapiEmitterTrait::emitHeaders()
4248
*/
4349
private function emitStatusLine(ResponseInterface $response)
4450
{

src/Response/SapiStreamEmitter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class SapiStreamEmitter implements EmitterInterface
2727
public function emit(ResponseInterface $response, $maxBufferLength = 8192)
2828
{
2929
$this->assertNoPreviousOutput();
30-
$this->emitStatusLine($response);
3130
$this->emitHeaders($response);
31+
$this->emitStatusLine($response);
3232

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

test/Response/AbstractEmitterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public function testMultipleSetCookieHeadersAreNotReplaced()
6666
$this->emitter->emit($response);
6767

6868
$expectedStack = [
69-
['header' => 'HTTP/1.1 200 OK', 'replace' => true, 'status_code' => 200],
7069
['header' => 'Set-Cookie: foo=bar', 'replace' => false, 'status_code' => 200],
7170
['header' => 'Set-Cookie: bar=baz', 'replace' => false, 'status_code' => 200],
71+
['header' => 'HTTP/1.1 200 OK', 'replace' => true, 'status_code' => 200],
7272
];
7373

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

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

9292
$this->assertSame($expectedStack, HeaderStack::stack());

test/ServerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,23 @@ public function testEmitsHeadersWithMultipleValuesMultipleTimes()
246246
*/
247247
public function testHeaderOrderIsHonoredWhenEmitted($stack)
248248
{
249+
$header = array_pop($stack);
250+
$this->assertContains('Content-Type: text/plain', $header);
251+
249252
$header = array_pop($stack);
250253
$this->assertContains(
251254
'Set-Cookie: bar=baz; expires=Wed, 8 Oct 2014 10:30; path=/foo/bar; domain=example.com',
252255
$header
253256
);
257+
254258
$header = array_pop($stack);
255259
$this->assertContains(
256260
'Set-Cookie: foo=bar; expires=Wed, 1 Oct 2014 10:30; path=/foo; domain=example.com',
257261
$header
258262
);
263+
264+
$header = array_pop($stack);
265+
$this->assertContains('HTTP/1.1 200 OK', $header);
259266
}
260267

261268
public function testListenPassesCallableArgumentToCallback()

0 commit comments

Comments
 (0)