Skip to content

Commit 8f31c6a

Browse files
committed
Throw an exception if user sends 1xx response with body
1 parent 48e6c7c commit 8f31c6a

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/HttpWorker.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ public function waitRequest(): ?Request
6565
/**
6666
* @throws \JsonException
6767
*/
68-
public function respond(int $status, string|Generator $body, array $headers = [], bool $endOfStream = true): void
68+
public function respond(int $status, string|Generator $body = '', array $headers = [], bool $endOfStream = true): void
6969
{
70+
if ($status < 200 && $status >= 100 && $body !== '') {
71+
throw new \InvalidArgumentException('Unable to send a body with informational status code.');
72+
}
73+
7074
if ($body instanceof Generator) {
7175
$this->respondStream($status, $body, $headers, $endOfStream);
7276
return;

tests/Feature/StreamResponseTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ public function testStopStreamResponse(): void
9696
self::assertSame(\implode("\n", ['Hel', 'lo,']), \trim(ServerRunner::getBuffer()));
9797
}
9898

99+
public function testSend1xxWithBody(): void
100+
{
101+
$httpWorker = $this->makeHttpWorker();
102+
103+
$this->expectExceptionMessage('Unable to send a body with informational status code');
104+
105+
$httpWorker->respond(
106+
103,
107+
(function () {
108+
yield 'Hel';
109+
yield 'lo,';
110+
})(),
111+
);
112+
}
113+
99114
public function testExceptionInGenerator(): void
100115
{
101116
$httpWorker = $this->makeHttpWorker();

0 commit comments

Comments
 (0)