mcrumm

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.
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
ityonemo
im surprised this doesn’t just add $callers into the header, that way the http request also has access to mox, and whatever else in the ecosystem gets that kind of concurrency support, in stead of just ecto
LostKobrakai
There’s no long running pid to set
$callersto for the phoenix sandbox. It’s meant to work for e2e tests for tools, which might not even run on the beam, which checkout and checkin db connections statelessly as far as the beam is concerned.mcrumm
Yeah ReqSandbox is only useful if you have an Elixir client communicating with a Phoenix/Plug server over HTTP. The client initiates the sandbox session and is responsible for including the given metadata on each request.
I decided to make ReqSandbox because anecdotally it’s a pain to account for the header value all the way thru your client application, especially if your app fires off HTTP requests in an ad-hoc fashion rather than passing around a client/request struct. Further, if you are using the sandbox to support integration tests it’s rather important to ensure that all of your requests are properly sandboxed, otherwise you run the risk of tainting your integration database with un-sandboxed operations.
mcrumm
v0.1.2 (2022-12-22)
Removes POST body content before making the sandbox request.
Adds missing
content-lengthheader on the sandbox request.Adds
ReqSandbox.token/0to fetch the current token if it exists.