From 9a17f807701ffec47f4a83f252ef0a8165bd3d5f Mon Sep 17 00:00:00 2001 From: stefansorge <3419073+sorge-it@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:06:15 +0100 Subject: [PATCH] fix: name the way out in four more messages The second review found that the message of ClosedWithErrors told a test without an expected error to drop onError, against the README. It now names the onError that fails the test at the error; three other messages name their fix too. --- CHANGELOG.md | 5 +++++ src/ClosedWithErrors.php | 2 +- src/Internal/Answer.php | 4 ++-- src/Server.php | 2 +- tests/FrankenPhp/FrankenPhpTest.php | 2 +- tests/Handler/BodyTest.php | 6 +++--- tests/Handler/HandlerTest.php | 8 ++++---- tests/Responses/ResponsesTest.php | 4 ++-- tests/Transport/TransportTest.php | 2 +- 9 files changed, 20 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index faddde8..3f5a4a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ All notable changes to this package are in this file. The format follows ### Changed +- Four more messages name the way out: `ClosedWithErrors` names a request cut by `close()` and the + `onError` that fails the test at the error; a chunk that is not a string names `json_encode()`; a handler that returns `null` + is told to return a `Response` from every path; a host that is not loopback names where the + browser runs. + - `SECURITY.md` moved to `.github/` and is no longer in the package. The security link in `composer.json` points to the security policy on GitHub. - Three messages name the fix. A handler that gives another type than `Response` names the full diff --git a/src/ClosedWithErrors.php b/src/ClosedWithErrors.php index 4f941e1..0668bfe 100644 --- a/src/ClosedWithErrors.php +++ b/src/ClosedWithErrors.php @@ -24,7 +24,7 @@ final class ClosedWithErrors extends LogicException public function __construct(public readonly array $errors, Request $request) { parent::__construct(sprintf( - 'The server closed with %d %s that no onError callback received; ClosedWithErrors::$errors lists each one. Fix the handler; only a test that expects the error passes onError to the Server. The first, from %s %s: %s', + 'The server closed with %d %s that no onError callback received; ClosedWithErrors::$errors lists each one. Fix the handler, or let each request finish before close(). To fail the test at the error instead of at close(), pass onError: static fn (Throwable $e): never => throw $e; a test that expects the error passes an onError that records it. The first, from %s %s: %s', count($errors), count($errors) === 1 ? 'error' : 'errors', $request->method, diff --git a/src/Internal/Answer.php b/src/Internal/Answer.php index 8210838..218c5a1 100644 --- a/src/Internal/Answer.php +++ b/src/Internal/Answer.php @@ -62,7 +62,7 @@ private function __construct(public string $head, public array $parts, public bo public static function check(mixed $response): Response { if (! $response instanceof Response) { - throw new UnexpectedValueException('The handler gave '.get_debug_type($response).', not a '.Response::class.'. A handler for a framework converts its response; see "Works with Symfony, Laravel and PSR-15" in the README.'); + throw new UnexpectedValueException('The handler gave '.get_debug_type($response).', not a '.Response::class.'. '.($response === null ? 'Return a Response from every path of the handler.' : 'A handler for a framework converts its response; see "Works with Symfony, Laravel and PSR-15" in the README of sorge-it/http-server.')); } if ($response->file !== null && (! is_file($response->file) || ! is_readable($response->file))) { @@ -287,7 +287,7 @@ private static function body(iterable $chunks, bool $sent, bool $counted, bool $ private static function chunk(mixed $chunk): string { if (! is_string($chunk)) { - throw new UnexpectedValueException('The body gave '.get_debug_type($chunk).', not a string.'); + throw new UnexpectedValueException('The body gave '.get_debug_type($chunk).', not a string. A streamed body yields strings; for JSON, pass json_encode($data) as the body.'); } return $chunk; diff --git a/src/Server.php b/src/Server.php index 53f5ae8..2f8d38b 100644 --- a/src/Server.php +++ b/src/Server.php @@ -118,7 +118,7 @@ public function __construct(callable $handler, string $host = '127.0.0.1', int $ $host = substr($name, 0, (int) strrpos($name, ':')); if (! $this->onLoopback(trim($host, '[]'))) { - throw new ListenFailed(sprintf('The server listens on loopback only, not on %s.', $host)); + throw new ListenFailed(sprintf('The server listens on loopback only, not on %s. Run the browser on the same host or in the same container as the test.', $host)); } // Without `tcp_nodelay`, Linux holds the body back until the client acknowledges the head, diff --git a/tests/FrankenPhp/FrankenPhpTest.php b/tests/FrankenPhp/FrankenPhpTest.php index c28b515..6d80610 100644 --- a/tests/FrankenPhp/FrankenPhpTest.php +++ b/tests/FrankenPhp/FrankenPhpTest.php @@ -236,7 +236,7 @@ function frankenPhpCases(): array [$answer, $errors] = withDisplayErrors('0', static fn (): array => failed(static fn (): Response => new Response(body: new ArrayIterator([1])), "GET / HTTP/1.1\r\nHost: a\r\nConnection: close\r\n\r\n")); expect($answer)->toStartWith("HTTP/1.1 500 Internal Server Error\r\n")->toContain("\r\nContent-Length: 0\r\n") - ->and($errors)->toBe(['UnexpectedValueException: The body gave int, not a string.']); + ->and($errors)->toBe(['UnexpectedValueException: The body gave int, not a string. A streamed body yields strings; for JSON, pass json_encode($data) as the body.']); }); it('gives onError each throwable PHP turns into output, once, and answers the next request', function (string $target, string $start): void { diff --git a/tests/Handler/BodyTest.php b/tests/Handler/BodyTest.php index 445424b..331399e 100644 --- a/tests/Handler/BodyTest.php +++ b/tests/Handler/BodyTest.php @@ -163,7 +163,7 @@ expect(converse($server, $client, "PUT /upload?x=1 HTTP/1.1\r\nHost: a\r\nContent-Length: 10\r\n\r\nabc"))->toBe('') ->and(closedWithErrors($server))->toBe([ ['LogicException: The server closed before the body was complete, with 3 of 10 bytes read', 'RuntimeException: read abc'], - 'The server closed with 2 errors that no onError callback received; ClosedWithErrors::$errors lists each one. Fix the handler; only a test that expects the error passes onError to the Server. The first, from PUT /upload?x=1: The server closed before the body was complete, with 3 of 10 bytes read', + 'The server closed with 2 errors that no onError callback received; ClosedWithErrors::$errors lists each one. Fix the handler, or let each request finish before close(). To fail the test at the error instead of at close(), pass onError: static fn (Throwable $e): never => throw $e; a test that expects the error passes an onError that records it. The first, from PUT /upload?x=1: The server closed before the body was complete, with 3 of 10 bytes read', true, ]) ->and($server->streams())->toBe([[], []]) @@ -183,7 +183,7 @@ expect(converse($server, $client, "POST /early HTTP/1.1\r\nHost: a\r\nContent-Length: 10\r\n\r\nabc"))->toBe('') ->and(closedWithErrors($server))->toBe([ ['LogicException: The server closed before the body was complete, with 3 of 10 bytes read'], - 'The server closed with 1 error that no onError callback received; ClosedWithErrors::$errors lists each one. Fix the handler; only a test that expects the error passes onError to the Server. The first, from POST /early: The server closed before the body was complete, with 3 of 10 bytes read', + 'The server closed with 1 error that no onError callback received; ClosedWithErrors::$errors lists each one. Fix the handler, or let each request finish before close(). To fail the test at the error instead of at close(), pass onError: static fn (Throwable $e): never => throw $e; a test that expects the error passes an onError that records it. The first, from POST /early: The server closed before the body was complete, with 3 of 10 bytes read', true, ]); } finally { @@ -211,7 +211,7 @@ expect(converse($server, $client, "PUT /upload HTTP/1.1\r\nHost: a\r\nContent-Length: 10\r\n\r\nabc"))->toBe('') ->and(closedWithErrors($server))->toBe([ ['LogicException: The server closed before the body was complete, with 3 of 10 bytes read'], - 'The server closed with 1 error that no onError callback received; ClosedWithErrors::$errors lists each one. Fix the handler; only a test that expects the error passes onError to the Server. The first, from PUT /upload: The server closed before the body was complete, with 3 of 10 bytes read', + 'The server closed with 1 error that no onError callback received; ClosedWithErrors::$errors lists each one. Fix the handler, or let each request finish before close(). To fail the test at the error instead of at close(), pass onError: static fn (Throwable $e): never => throw $e; a test that expects the error passes an onError that records it. The first, from PUT /upload: The server closed before the body was complete, with 3 of 10 bytes read', true, ]) ->and($caught)->toStartWith('LogicException: '.$message); diff --git a/tests/Handler/HandlerTest.php b/tests/Handler/HandlerTest.php index 670600d..d3456c6 100644 --- a/tests/Handler/HandlerTest.php +++ b/tests/Handler/HandlerTest.php @@ -13,8 +13,8 @@ ->and($errors)->toHaveCount(1) ->and($errors[0] ?? '')->toContain($message); })->with([ - 'null' => [static fn (Request $request): mixed => $request->path() === '/bad' ? null : new Response(200, [], 'ok'), 'The handler gave null, not a SorgeIt\\HttpServer\\Response. A handler for a framework converts its response; see "Works with Symfony, Laravel and PSR-15" in the README.'], - 'a string' => [static fn (Request $request): mixed => $request->path() === '/bad' ? 'text' : new Response(200, [], 'ok'), 'The handler gave string, not a SorgeIt\\HttpServer\\Response. A handler for a framework converts its response; see "Works with Symfony, Laravel and PSR-15" in the README.'], + 'null' => [static fn (Request $request): mixed => $request->path() === '/bad' ? null : new Response(200, [], 'ok'), 'The handler gave null, not a SorgeIt\\HttpServer\\Response. Return a Response from every path of the handler.'], + 'a string' => [static fn (Request $request): mixed => $request->path() === '/bad' ? 'text' : new Response(200, [], 'ok'), 'The handler gave string, not a SorgeIt\\HttpServer\\Response. A handler for a framework converts its response; see "Works with Symfony, Laravel and PSR-15" in the README of sorge-it/http-server.'], // @phpstan-ignore argument.type (a handler result the server cannot send) 'a header value that is a number' => [static fn (Request $request): Response => $request->path() === '/bad' ? new Response(200, ['X-Count' => 5]) : new Response(200, [], 'ok'), 'ValueError: '.Response::class.'::__construct(): Argument #2 ($headers) has a value of X-Count that is not a string'], // @phpstan-ignore argument.type (a handler result the server cannot send) @@ -179,7 +179,7 @@ public function handle(Request $request): Response expect(substr_count($answer, 'HTTP/1.1 500 '))->toBe(2) ->and($calls)->toBe([ [LogicException::class, 'broken', '/throws', 1], - [UnexpectedValueException::class, 'The handler gave null, not a SorgeIt\\HttpServer\\Response. A handler for a framework converts its response; see "Works with Symfony, Laravel and PSR-15" in the README.', '/null', 1], + [UnexpectedValueException::class, 'The handler gave null, not a SorgeIt\\HttpServer\\Response. Return a Response from every path of the handler.', '/null', 1], ]); }); @@ -263,7 +263,7 @@ public function record(Throwable $throwable): void expect(substr_count($answers, "HTTP/1.1 500 Internal Server Error\r\n"))->toBe(2) ->and(closedWithErrors($server))->toBe([ ['LogicException: broken /a', 'LogicException: broken /b'], - 'The server closed with 2 errors that no onError callback received; ClosedWithErrors::$errors lists each one. Fix the handler; only a test that expects the error passes onError to the Server. The first, from GET /a: broken /a', + 'The server closed with 2 errors that no onError callback received; ClosedWithErrors::$errors lists each one. Fix the handler, or let each request finish before close(). To fail the test at the error instead of at close(), pass onError: static fn (Throwable $e): never => throw $e; a test that expects the error passes an onError that records it. The first, from GET /a: broken /a', true, ]) ->and($server->streams())->toBe([[], []]) diff --git a/tests/Responses/ResponsesTest.php b/tests/Responses/ResponsesTest.php index 2f4de8c..4e73cf4 100644 --- a/tests/Responses/ResponsesTest.php +++ b/tests/Responses/ResponsesTest.php @@ -149,7 +149,7 @@ ->and($errors)->toBe([$message, $message]); })->with([ 'a throw' => [static fn (): Generator => yield throw new RuntimeException('first'), 'RuntimeException: first'], - 'an integer' => [static fn (): array => [1], 'UnexpectedValueException: The body gave int, not a string.'], + 'an integer' => [static fn (): array => [1], 'UnexpectedValueException: The body gave int, not a string. A streamed body yields strings; for JSON, pass json_encode($data) as the body.'], ]); it('ends the body for a later chunk that throws, as FrankenPHP for a script that throws, and gives onError the throwable', function (): void { @@ -171,7 +171,7 @@ expect(substr_count($answer, 'HTTP/1.1 '))->toBe(1) ->and($answer)->toEndWith("\r\n\r\n2\r\nok\r\n\n") - ->and($errors)->toBe(['UnexpectedValueException: The body gave int, not a string.']); + ->and($errors)->toBe(['UnexpectedValueException: The body gave int, not a string. A streamed body yields strings; for JSON, pass json_encode($data) as the body.']); }); it('gives onError the LogicException of a close() from a later chunk, and stays open', function (): void { diff --git a/tests/Transport/TransportTest.php b/tests/Transport/TransportTest.php index 4fb02a9..400c092 100644 --- a/tests/Transport/TransportTest.php +++ b/tests/Transport/TransportTest.php @@ -133,7 +133,7 @@ function hasIpv6Loopback(): bool $taken->close(); } - expect($message)->toBe('The server listens on loopback only, not on '.$address.'.'); + expect($message)->toBe('The server listens on loopback only, not on '.$address.'. Run the browser on the same host or in the same container as the test.'); })->with([ 'IPv4, each address' => ['0.0.0.0', '0.0.0.0'], 'IPv4, each address, short' => ['0', '0.0.0.0'],