Moxinet (moxinet v0.4.0)

Moxinet helps you mock the internet at the HTTP layer without sacrificing parallel testing.

Summary

Functions

Returns the header needed to be included in requests to the mock servers in order to support parallel runs.

Mocks a call for the passed module by defining a signature based on the pid, http method and path.

Turns a pid into a reference which could be used for indexing the signatures.

The start functions which will start the Moxinet server.

Types

http_method()

@type http_method() :: :get | :post | :patch | :put | :delete | :options

Functions

build_mock_header(pid \\ self())

@spec build_mock_header(pid()) :: {String.t(), String.t()}

Returns the header needed to be included in requests to the mock servers in order to support parallel runs.

expect(module, http_method, path, callback, options \\ [])

@spec expect(
  module(),
  http_method(),
  binary(),
  Moxinet.SignatureStorage.Mock.callback(),
  Moxinet.SignatureStorage.store_options()
) :: :ok

Mocks a call for the passed module by defining a signature based on the pid, http method and path.

Options:

  • pid: The source pid that the mock will be applied for. Defaults to self()
  • times: The amount of times the mock signature may be used. Defaults to 1
  • storage: The signature storage to be used. Defaults to Moxinet.SignatureStorage

Examples:

Moxinet.expect(MyMock, :get, "/path/to/resource", fn _body ->

%Moxinet.Response{status: 200, body: "My response body"}

end)

pid_reference(pid)

@spec pid_reference(pid()) :: String.t()

Turns a pid into a reference which could be used for indexing the signatures.

start(opts)

@spec start(Keyword.t()) :: {:ok, pid()} | {:error, atom()}

The start functions which will start the Moxinet server.

You'd most likely want to put run this function from your test_helper.exs:

{:ok, _pid} = Moxinet.start(router: MyMockServer, port: 4010)

Options

  • router: A reference to your mock server. Required
  • port: The port your mock server will run on. Required
  • name: Name of the moxinet supervisor. Defaults to Moxinet