Moxinet (moxinet v0.8.0)

Copy Markdown

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

Summary

Functions

Allows pid_to_allow to use mocks defined by pid_with_access.

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.

Verifies that all defined expectations have been called to prevent tests from defining expectations that aren't used. Recommended usage is to set it up in your test setup

Types

http_method()

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

Functions

allow(pid_with_access, pid_to_allow)

@spec allow(pid(), pid() | (-> pid())) :: :ok | {:error, NimbleOwnership.Error.t()}

Allows pid_to_allow to use mocks defined by pid_with_access.

This is useful when a test spawns processes that need access to the tests expectations. By default, most mocks set up with expect/5 will work out of the box but in scenarios where the allowance cannot be inferred through $callers, it must be explicitly defined.

Example

A common use case is when the code under test spawns a process that makes HTTP requests:

test "spawned process can use parent mocks" do
  parent = self()

  Moxinet.expect(MyMock, :get, "/users", fn _body ->
    %Moxinet.Response{status: 200, body: ~s({"id": 1})}
  end)

  task =
    spawn(fn ->
      Moxinet.allow(parent, self())

      # Now this process can make requests that use the mock
      {:ok, response} = MyHTTPClient.get("/users")
      response

      send(parent, response)
    end)

  assert_receive %{id: 1}
end

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
  • signature_storage: Name of the signature storage server. Defaults to Moxinet.SignatureStorage

verify_usage!(test_pid, storage_pid \\ Moxinet.SignatureStorage)

Verifies that all defined expectations have been called to prevent tests from defining expectations that aren't used. Recommended usage is to set it up in your test setup:

setup :verify_usage!