Skip to content

Commit 6d5c62f

Browse files
committed
preserve protocol version for stream factories
1 parent 4721eee commit 6d5c62f

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎system/HTTP/ResponseTrait.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ public function download(string $filename = '', $data = '', bool $setMime = fals
820820
*/
821821
public function stream(callable|iterable $callbackOrChunks): StreamResponse
822822
{
823-
return new StreamResponse($callbackOrChunks);
823+
return (new StreamResponse($callbackOrChunks))->setProtocolVersion($this->getProtocolVersion());
824824
}
825825

826826
/**
@@ -830,7 +830,7 @@ public function stream(callable|iterable $callbackOrChunks): StreamResponse
830830
*/
831831
public function eventStream(callable $callback): SSEResponse
832832
{
833-
return new SSEResponse($callback);
833+
return (new SSEResponse($callback))->setProtocolVersion($this->getProtocolVersion());
834834
}
835835

836836
public function getCSP(): ContentSecurityPolicy

‎tests/system/HTTP/SSEResponseSendTest.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,22 @@ public function testSendEmitsHeadersCookiesAndStream(): void
5252
$this->assertHeaderNotEmitted('Connection: keep-alive');
5353
}
5454
}
55+
56+
#[PreserveGlobalState(false)]
57+
#[RunInSeparateProcess]
58+
#[WithoutErrorHandler]
59+
public function testEventStreamFactoryPreservesHttp2ProtocolHeaders(): void
60+
{
61+
$response = (new Response())
62+
->setProtocolVersion('2.0')
63+
->eventStream(static function (): void {
64+
});
65+
$response->pretend(false);
66+
67+
ob_start();
68+
$response->send();
69+
ob_end_clean();
70+
71+
$this->assertHeaderNotEmitted('Connection: keep-alive');
72+
}
5573
}

‎tests/system/HTTP/StreamResponseTest.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,31 @@ public function testStreamFactoryAcceptsIterable(): void
114114
$this->assertInstanceOf(StreamResponse::class, $response);
115115
}
116116

117+
public function testStreamFactoryCopiesProtocolVersion(): void
118+
{
119+
$response = (new Response())
120+
->setProtocolVersion('2.0')
121+
->stream(static function (): void {
122+
});
123+
124+
$this->assertSame('2.0', $response->getProtocolVersion());
125+
}
126+
117127
public function testEventStreamFactoryReturnsSSEResponse(): void
118128
{
119129
$response = (new Response())->eventStream(static function (): void {
120130
});
121131

122132
$this->assertInstanceOf(SSEResponse::class, $response);
123133
}
134+
135+
public function testEventStreamFactoryCopiesProtocolVersion(): void
136+
{
137+
$response = (new Response())
138+
->setProtocolVersion('2.0')
139+
->eventStream(static function (): void {
140+
});
141+
142+
$this->assertSame('2.0', $response->getProtocolVersion());
143+
}
124144
}

0 commit comments

Comments
 (0)