View Source TestServer (TestServer v0.1.16)

No fuzz ExUnit test server to mock third party services.

Features:

  • HTTP/1
  • HTTP/2
  • WebSocket
  • Built-in TLS with self-signed certificates
  • Plug route matching

Summary

Functions

Adds a route to the current test server.

Adds a route to a test server instance.

Gets current test server instance if running.

Adds a plug to the current test server.

Adds a route to a test server instance.

Start a test server instance.

Shuts down the current test server.

Shuts down a test server instance.

Produces a URL for current test server.

Produces a URL for a test server instance.

Adds a message handler to a websocket instance.

Sends an message to a websocket instance.

Adds a websocket route to current test server.

Adds a websocket route to a test server.

Fetches the generated x509 suite for the current test server.

Fetches the generated x509 suite for a test server instance.

Types

@type instance() :: pid()
@type route() :: reference()
@type stacktrace() :: list()
@type websocket_frame() :: {atom(), any()}
@type websocket_reply() ::
  {:reply, websocket_frame(), websocket_state()} | {:ok, websocket_state()}
@type websocket_socket() :: {instance(), route()}
@type websocket_state() :: any()

Functions

@spec add(binary()) :: :ok
@spec add(
  binary(),
  keyword()
) :: :ok
@spec add(pid(), binary()) :: :ok

Adds a route to the current test server.

Options

  • :via - matches the route against some specific HTTP method(s) specified as an atom, like :get or :put, or a list, like [:get, :post].
  • :match - an anonymous function that will be called to see if a route matches, defaults to matching with arguments of uri and :via option.
  • :to - a Plug or anonymous function that will be called when the route matches, defaults to return the http scheme.
Link to this function

add(instance, uri, options)

View Source
@spec add(pid(), binary(), keyword()) :: :ok

Adds a route to a test server instance.

See add/2 for options.

@spec get_instance() :: pid() | nil

Gets current test server instance if running.

@spec plug(atom() | function()) :: :ok

Adds a plug to the current test server.

This plug will be called for all requests before route is matched.

@spec plug(pid(), atom() | function()) :: :ok

Adds a route to a test server instance.

See plug/1 for options.

@spec start(keyword()) :: {:ok, pid()}

Start a test server instance.

The instance will be terminated when the test case finishes.

Options

  • :port - integer of port number, defaults to random port that can be opened;
  • :scheme - an atom for the http scheme. Defaults to :http;
  • :http_server - HTTP server configuration. Defaults to {TestServer.HTTPServer.Httpd, []};
  • :tls - Passthru options for TLS configuration handled by the webserver;
  • :ipfamily - The IP address type to use, either :inet or :inet6. Defaults to :inet;
@spec stop() :: :ok | {:error, term()}

Shuts down the current test server.

@spec stop(pid()) :: :ok | {:error, term()}

Shuts down a test server instance.

@spec url() :: binary()
@spec url(binary() | keyword()) :: binary()
@spec url(
  binary(),
  keyword()
) :: binary()
@spec url(pid(), binary()) :: binary()

Produces a URL for current test server.

Options

  • :host - binary host value, it'll be added to inet for IP 127.0.0.1 and ::1, defaults to "localhost";
Link to this function

url(instance, uri, opts)

View Source
@spec url(pid(), binary(), keyword()) :: binary()

Produces a URL for a test server instance.

See url/2 for options.

Link to this function

websocket_handle(socket)

View Source
@spec websocket_handle(websocket_socket()) :: :ok | {:error, term()}
Link to this function

websocket_handle(socket, options)

View Source
@spec websocket_handle(
  websocket_socket(),
  keyword()
) :: :ok

Adds a message handler to a websocket instance.

Options

  • :match - an anonymous function that will be called to see if a message matches, defaults to matching anything.
  • :to - an anonymous function that will be called when the message matches, defaults to returning received message.
Link to this function

websocket_info(socket, callback \\ nil)

View Source
@spec websocket_info(websocket_socket(), function() | nil) :: :ok

Sends an message to a websocket instance.

@spec websocket_init(binary()) :: {:ok, websocket_socket()} | {:error, term()}
Link to this function

websocket_init(uri, options)

View Source
@spec websocket_init(
  binary(),
  keyword()
) :: {:ok, websocket_socket()}
@spec websocket_init(pid(), binary()) :: {:ok, websocket_socket()}

Adds a websocket route to current test server.

Options

Takes the same options as add/2, except :to.

Link to this function

websocket_init(instance, uri, options)

View Source
@spec websocket_init(pid(), binary(), keyword()) :: {:ok, websocket_socket()}

Adds a websocket route to a test server.

See websocket_init/2 for options.

@spec x509_suite() :: term()

Fetches the generated x509 suite for the current test server.

@spec x509_suite(pid()) :: term()

Fetches the generated x509 suite for a test server instance.