HttpDouble.PlugAdapter (http_double v1.0.0)

Copy Markdown View Source

Converts between Plug.Conn and HttpDouble.Request/response_spec.

Used when HttpDouble runs on Plug.Cowboy: the Plug forwards every request to the Server, which returns a response_spec; this module turns that into a Plug.Conn response.

Summary

Functions

Builds an HttpDouble.Request from a Plug.Conn (after body has been read).

Sends the response_spec back through the Plug.Conn.

Functions

conn_to_request(conn, body, conn_id, request_index)

@spec conn_to_request(Plug.Conn.t(), binary(), non_neg_integer(), non_neg_integer()) ::
  HttpDouble.Request.t()

Builds an HttpDouble.Request from a Plug.Conn (after body has been read).

conn_id and request_index should be set by the caller (Server).

response_spec_to_conn(conn, spec)

@spec response_spec_to_conn(Plug.Conn.t(), HttpDouble.response_spec()) ::
  Plug.Conn.t()

Sends the response_spec back through the Plug.Conn.

Supports: map/struct (status, body, json, headers), {:delay, ms, inner}, {:raw, iodata}, and {:partial, [iodata]} (chunks concatenated into one response body).

:timeout returns 504 after delay. :close terminates the request process to emulate abrupt connection drop (no valid HTTP response). Route matches may pass {spec, route_id} with a reference route_id; that tuple is unwrapped before handling.

Other unsupported specs fall back to a 200 empty body (with a log warning).