Loopback.Interceptor (Loopback v0.1.0)

Copy Markdown View Source

Interceptors replace functions of HTTP clients, so that their requests reach the fakes.

An interceptor is a module for a specific HTTP client. It defines functions with the same names and arities as the client's functions that send requests, like request, get or post. Once it's installed, they replace the client's functions in every process, and the client's other functions run unchanged. Each of the interceptor's functions:

  1. Maps the client's request to a Loopback.HTTPRequest, with its headers passed through normalize_headers/1.
  2. Dispatches it to the fakes with dispatch/1.
  3. Maps the result: a Loopback.HTTPResponse to the client's response, with its headers passed through normalize_headers/1 too, a Loopback.HTTPError to the client's error, and :passthrough to a call of the client's original function.

use Loopback.Interceptor, with the client module as the :for option, generates the @original attribute, which points to the client's original module under a new name.

Interceptors are installed from the :intercept config when Loopback starts, see the README. Installing the same interceptor again does nothing. A client can have only one interceptor, so installing another one for it raises.

Here's a basic interceptor of a client whose get/1 returns {:ok, body} or {:error, reason}:

defmodule MyClientInterceptor do
  use Loopback.Interceptor, for: MyClient

  def get(url) do
    request = Loopback.HTTPRequest.new(method: "GET", url: url)

    case Loopback.Interceptor.dispatch(request) do
      %Loopback.HTTPResponse{body: body} -> {:ok, body}
      %Loopback.HTTPError{reason: reason} -> {:error, reason}
      :passthrough -> @original.get(url)
    end
  end
end

An interceptor has to replace every public function of the client that sends a request, because calls between the client's own private functions aren't intercepted.

Options

  • :for - the client module that the interceptor replaces. Required.

Summary

Functions

Dispatches an intercepted request to the first fake that matches it, and returns the fake's reply.

Turns HTTP headers into a list of {name, value} strings, with the names lowercased.

Returns the standard reason phrase of an HTTP status, for clients that return one, or an empty string for a status that has none.

Functions

dispatch(request)

Dispatches an intercepted request to the first fake that matches it, and returns the fake's reply.

A :passthrough reply means that the interceptor calls the original function, through @original. When no fake matches, it raises, or returns :passthrough when the fakes were started with unmatched: :passthrough. Raises if no fakes were started for the calling process.

normalize_headers(headers)

@spec normalize_headers(Enumerable.t()) :: [{String.t(), String.t()}]

Turns HTTP headers into a list of {name, value} strings, with the names lowercased.

It keeps the order and the repeated headers, and takes a list of tuples or a map, with names as strings, atoms or charlists.

iex> headers = [{"Content-Type", "text/plain"}, {:accept, ~c"*/*"}]
iex> normalize_headers(headers)
[{"content-type", "text/plain"}, {"accept", "*/*"}]

Values become strings too, and a map works the same way:

iex> normalize_headers(%{"Content-Length" => 12})
[{"content-length", "12"}]

The order and the repeated headers are kept:

iex> headers = [{"Accept", "text/html"}, {"X-Id", "1"}, {"accept", "*/*"}]
iex> normalize_headers(headers)
[{"accept", "text/html"}, {"x-id", "1"}, {"accept", "*/*"}]

reason_phrase(status)

@spec reason_phrase(non_neg_integer()) :: String.t()

Returns the standard reason phrase of an HTTP status, for clients that return one, or an empty string for a status that has none.

iex> reason_phrase(404)
"Not Found"

iex> reason_phrase(299)
""