HttpDouble.Response (http_double v1.0.0)

Copy Markdown View Source

Helpers and struct for building HTTP responses.

Most callers will use the simple map form:

%{status: 200, body: "ok"}
%{status: 201, json: %{result: "accepted"}}

This module makes it easy to construct responses and turn them into raw wire-format bytes to be sent via TCP.

Summary

Types

t()

Simple HTTP response structure.

Functions

Builds an empty-body response.

Builds a JSON response using Jason for encoding.

Builds a plain text response.

Converts a response specification (map or struct) into raw HTTP/1.1 bytes.

Types

t()

@type t() :: %HttpDouble.Response{
  body: iodata(),
  chunked?: boolean(),
  headers: [{String.t(), String.t()}],
  json: map() | list() | nil,
  status: non_neg_integer()
}

Simple HTTP response structure.

Functions

empty(status, opts \\ [])

@spec empty(
  non_neg_integer(),
  keyword()
) :: t()

Builds an empty-body response.

json(status, data, opts \\ [])

@spec json(non_neg_integer(), map() | list(), keyword()) :: t()

Builds a JSON response using Jason for encoding.

text(status, body, opts \\ [])

@spec text(non_neg_integer(), iodata(), keyword()) :: t()

Builds a plain text response.

to_iodata(spec, request \\ nil)

@spec to_iodata(map() | t(), HttpDouble.Request.t() | nil) :: iodata()

Converts a response specification (map or struct) into raw HTTP/1.1 bytes.

When request is provided, it is used for behaviours like HEAD responses (no body even when one is present).