View Source ReqSandbox (req_sandbox v0.2.0)

CI Docs Hex pm

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 contains a full demonstration of the sandbox features.

Mix.install([
  {:req, "~> 0.4.0"},
  {:req_sandbox, "~> 0.2.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...)"

License

MIT License. Copyright (c) 2022 Michael A. Crumm Jr.

Summary

Functions

Attaches to a Req request.

Deletes the sandbox token from the process dictionary if it exists.

Deletes the sandbox from the server and the process dictionary, if it exists.

Returns the current sandbox token or nil if no sandbox exists.

Functions

Link to this function

attach(req, options \\ [])

View Source

Attaches to a Req request.

Options

  • :sandbox_url - The path to the sandbox. The default value is "/sandbox".

  • :sandbox_header - The header to put on the request. The default value is :user_agent.

  • :sandbox_header_token - If provided, the sandbox header value. Otherwise, an existing token from the process will be used. If no token is found, one will be requested from the path at :sandbox_url. The default value is nil.

@spec delete!() :: String.t() | nil

Deletes the sandbox token from the process dictionary if it exists.

@spec delete!(Req.Request.t()) :: String.t() | nil

Deletes the sandbox from the server and the process dictionary, if it exists.

@spec token() :: nil | String.t()

Returns the current sandbox token or nil if no sandbox exists.

Examples

ReqSandbox.token()
#=> "BeamMetadata (g2gCZAACdjF0AAAAA2QABW93bmVyWGQAInZ2ZXMzM2o1LWxpdmVib29...)"