Moxinet (moxinet v0.8.0)
Copy MarkdownMoxinet 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
Functions
@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
Returns the header needed to be included in requests to the mock servers in order to support parallel runs.
@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 toself()times: The amount of times the mock signature may be used. Defaults to1storage: The signature storage to be used. Defaults toMoxinet.SignatureStorage
Examples:
Moxinet.expect(MyMock, :get, "/path/to/resource", fn _body ->
%Moxinet.Response{status: 200, body: "My response body"}end)
Turns a pid into a reference which could be used for indexing the signatures.
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. Requiredport: The port your mock server will run on. Requiredname: Name of the moxinet supervisor. Defaults toMoxinetsignature_storage: Name of the signature storage server. Defaults toMoxinet.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!