HTTP.Test (http_test v1.0.0-rc.9)

Copy Markdown View Source

Owner-isolated fixtures for HTTP Family requests.

HTTP.Test.expect(:service, fn request ->
  HTTP.Test.json(%{path: request.url.path})
end)
response = HTTP.fetch("https://service.test/items", adapter: {HTTP.Test, :service})
           |> HTTP.Promise.await()
HTTP.Test.verify!()

Fixtures belong to the process that registers them. Tasks inherit their caller chain; authorize an independent GenServer with allow/3. Stub and expectation modes are mutually exclusive per owner/name. Expectations are claimed atomically in registration order; cancellation does not restore a claimed call.

Call verify_on_exit!/0 in ExUnit setup. Verification freezes claims, cancels and joins simulated resources with a finite budget, then checks counts and callback failures. Callback exceptions retain their cause and stacktrace and fail verification even when a Promise was never awaited. Intentional transport_error/1 fixtures are ordinary client failures.

This adapter performs one request attempt. Manual redirects are exposed; error redirects fail with :redirect; follow candidates fail with {:unsupported_test_policy, :redirect_follow}. Only 301/302/303/307/308 with a binary Location are candidates. Default nonredirect fixtures work normally. Default follow completion remains unsupported. Mock resource cleanup cannot establish socket, TLS or pool behavior.

Summary

Functions

Authorizes allowed to use one fixture belonging to owner.

Registers an ordered expectation, consumed exactly count times (default one).

Waits at a cooperative response-header gate; abort returns an intentional client failure.

Replaces response headers, preserving duplicate fields and order.

Builds a UTF-8 HTML response.

Builds a JSON byte response; Fetch does not decode the body into a map.

Invokes an optional buffered Plug bridge with a finite body limit (default 1 MiB).

Builds a buffered response, preserving ordered duplicate headers.

Builds an empty response with the given status.

Creates a response using a real acknowledged HTTP.Stream inside a fixture.

Registers a repeatable fixture in the current owner's namespace.

Builds a UTF-8 text response.

Creates an intentional transport failure, subject to Fetch's error mode.

Verifies and settles this owner's fixtures. Further claims are rejected.

Registers an ExUnit exit verifier with an opaque token that survives owner exit.

Functions

allow(name, owner, allowed)

Authorizes allowed to use one fixture belonging to owner.

expect(name, count \\ 1, callback)

Registers an ordered expectation, consumed exactly count times (default one).

gate(observer, tag, callback, timeout \\ 5000)

Waits at a cooperative response-header gate; abort returns an intentional client failure.

headers(response, headers)

Replaces response headers, preserving duplicate fields and order.

html(body, status \\ 200)

Builds a UTF-8 HTML response.

json(value, status \\ 200)

Builds a JSON byte response; Fetch does not decode the body into a map.

plug(request, plug, opts \\ [])

Invokes an optional buffered Plug bridge with a finite body limit (default 1 MiB).

response(status, body \\ "", headers \\ [])

Builds a buffered response, preserving ordered duplicate headers.

status(code)

Builds an empty response with the given status.

stream(steps, opts \\ [])

Creates a response using a real acknowledged HTTP.Stream inside a fixture.

Steps are binary/iodata chunks, {:gate, observer, tag}, or {:error, reason}. A gate sends {:http_test_gate, tag, producer, ref} and waits for {:http_test_release, ref}. Each following step begins only after the previous chunk's acknowledgement. trailers: preserves field order. observer: receives {:http_test_stream, stream, producer} for deterministic cleanup assertions. The finite timeout: bounds each chunk and gate (default 5 seconds).

stub(name, callback)

Registers a repeatable fixture in the current owner's namespace.

text(body, status \\ 200)

Builds a UTF-8 text response.

transport_error(reason)

Creates an intentional transport failure, subject to Fetch's error mode.

verify!()

Verifies and settles this owner's fixtures. Further claims are rejected.

verify_on_exit!()

Registers an ExUnit exit verifier with an opaque token that survives owner exit.