HttpDouble.Route (http_double v1.0.0)

Copy Markdown View Source

Representation of a static HTTP route.

Routes can be defined as simple maps or as more advanced structures. This module normalises external route specifications into a uniform internal form and provides matching helpers.

Summary

Types

Unique route identifier.

t()

Internal route representation.

Functions

Deletes a route from the list using either :id or {method, path}.

Finds the first matching route and resolves its response specification.

Builds a route struct from a user-facing map specification.

Updates a route inside a route list using either :id or {method, path}.

Types

id()

@type id() :: reference()

Unique route identifier.

t()

@type t() :: %HttpDouble.Route{
  body_match: nil | binary() | Regex.t(),
  header_matches: [{String.t(), String.t() | :any | Regex.t()}],
  host: String.t() | nil,
  id: id(),
  method: String.t(),
  path: String.t(),
  path_prefix: String.t() | nil,
  path_regex: Regex.t() | nil,
  query: %{optional(String.t()) => String.t() | :any},
  response: HttpDouble.response_spec()
}

Internal route representation.

Functions

delete_from_list(routes, spec)

@spec delete_from_list([t()], map()) :: [t()]

Deletes a route from the list using either :id or {method, path}.

find_match(routes, req)

@spec find_match([t()], HttpDouble.Request.t()) ::
  {:ok, t(), HttpDouble.response_spec()} | :nomatch

Finds the first matching route and resolves its response specification.

from_spec(spec)

@spec from_spec(map()) :: t()

Builds a route struct from a user-facing map specification.

Supported keys:

  • :method – string or atom, e.g. "GET" or :post
  • :path – exact path match
  • :path_prefix – prefix match
  • :path_regex – regular expression
  • :host – optional host match
  • :query – map of query parameters, values can be exact strings or :any
  • :headers – header match map or list of {name, value} (value can be string, :any or regex)
  • :body – exact body match or regex
  • :response – response specification (map, HttpDouble.Response, callback, etc.)

update_in_list(routes, spec)

@spec update_in_list([t()], map()) :: [t()]

Updates a route inside a route list using either :id or {method, path}.