Skip to content

Commit ea78b23

Browse files
Document in-process middleware testing
Assisted-By: devx/eb0838cf-417f-4559-90ed-e9a0d84bfd2d
1 parent 8ad6ab6 commit ea78b23

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

‎guides/testing/readme.md‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,45 @@ This guide explains how to test `Async::HTTP` clients and servers without depend
44

55
Real network services make tests slower and less deterministic. Prefer one of these approaches:
66

7+
- Use [`sus-fixtures-protocol-http`](https://socketry.github.io/sus-fixtures-protocol-http/guides/getting-started/) to exercise HTTP middleware directly without a client or server.
78
- Use `sus-fixtures-async-http` to run an application with a managed local server and client.
89
- Use ruby:`Async::HTTP::Mock::Endpoint` when testing a client that expects to connect to a particular remote endpoint.
9-
- Use a small fake client when the HTTP protocol behavior itself is not under test.
1010

11-
## Testing an HTTP Application
11+
## Testing Middleware Directly
12+
13+
When a test only needs to construct requests and inspect responses, `sus-fixtures-protocol-http` can call `Protocol::HTTP` middleware in-process without starting a client or server. Add the fixture to your test dependencies:
14+
15+
~~~ bash
16+
$ bundle add sus --group test
17+
$ bundle add sus-fixtures-protocol-http --group test
18+
~~~
19+
20+
Include `MiddlewareContext` and provide the middleware under test:
21+
22+
~~~ ruby
23+
require "sus/fixtures/protocol/http/middleware_context"
24+
25+
describe "My HTTP application" do
26+
include Sus::Fixtures::Protocol::HTTP::MiddlewareContext
27+
28+
let(:middleware) do
29+
Protocol::HTTP::Middleware.for do |request|
30+
Protocol::HTTP::Response[200, {}, ["Hello #{request.path}"]]
31+
end
32+
end
33+
34+
it "handles a request directly" do
35+
response = client.get("/world")
36+
37+
expect(response.status).to be == 200
38+
expect(response.read).to be == "Hello /world"
39+
end
40+
end
41+
~~~
42+
43+
The fixture closes the final request, response, and middleware after each test. Use `sus-fixtures-async-http` when the test needs a real client/server exchange.
44+
45+
## Testing with a Client and Server
1246

1347
The `ServerContext` fixture manages an ephemeral listening endpoint, server task, and connected client. Add the fixture to your test dependencies:
1448

0 commit comments

Comments
 (0)