Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ClosedWithErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/Internal/Answer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/FrankenPhp/FrankenPhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions tests/Handler/BodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([[], []])
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions tests/Handler/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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],
]);
});

Expand Down Expand Up @@ -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([[], []])
Expand Down
4 changes: 2 additions & 2 deletions tests/Responses/ResponsesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<closed>")
->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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/Transport/TransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
Loading