Photo by Takeshi Morisato on Unsplash
Req plugin for Phoenix.Ecto.SQL.Sandbox.
ReqSandbox simplifies making concurrent, transactional requests to a Phoenix server. Just before making a request, the sandbox metadata is applied via the specified request header. If there is no metadata available, then ReqSandbox creates a new sandbox session and saves the metadata for future requests. This is mostly useful in client test environments to ensure logical isolation between concurrent tests.
Usage
The Ecto SQL Sandbox Usage Guide included in the docs is a Livebook with an interactive demonstration of all the sandbox features.
Mix.install([
{:req, "~> 0.3.0"},
{:req_sandbox, "~> 0.1.0"}
])
req = Req.new(base_url: "http://localhost:4000" |> ReqSandbox.attach()
Req.post!(req, url: "/api/posts", json: %{"post" => %{"msg" => "Hello, world!"}}).body
# => %{"data" => %{"id" => 2, "msg" => "Hello, world!"}}
ReqSandbox.delete!(req)
# => "BeamMetadata (g2gCZAACdjF0AAAAA2QABW93bmVyWGQAInZ2ZXMzM2o1LWxpdmVib29...)"
Audience
If you are writing HTTP clients in Elixir to communicate with Phoenix/Plug + Ecto services, then this package is for you! Sandboxing client requests is an effective way to provide developers with an integration environment that can be operated concurrently while maintaining logical isolation between development sessions.