jswanner

jswanner

ReqTestBandit - Req.Test plugin to use Bandit for server mocks

I guess you can say @wojtekmach nerd sniped me into trying out backing Req.Test mocks with Bandit, so that your test Req test requests are actually going through an HTTP stack (instead of only invoking a Plug function). You can optionally use X509 in order to use HTTPS for those test requests.

Caveat: I have no idea if this is a good idea or not, but it’s fun to play with and it’s working for me!

With ReqTestBandit, you still use Req.Test to register the stub & mock requests, and it still uses Req.Test’s ownership model, in fact as a user of this library not much needs to change.

If you have a setup similar to that given in Req.Test’s example, then you’d want to add bandit: true to the app config:

config :myapp, weather_req_options: [
  bandit: true,
  plug: {Req.Test, MyApp.Weather},
  x509: true # optional, if you want to use HTTPS
]

Lastly the plugin needs to be attached:

def get_temperature(location) do
  [
    base_url: "https://weather-service",
    params: [location: location]
  ]
  |> Keyword.merge(Application.get_env(:myapp, :weather_req_options, []))
  |> Req.new()
  |> ReqTestBandit.attach()
  |> Req.request()
end

Most Liked

wojtekmach

wojtekmach

Hex Core Team

Yup, that’s what I had in mind. Id still like to have something like this built-in but now that a package exists, less priority!

Btw, for upcoming Req v0.6, I’m planning to make this change (with deprecation)

- plug: {Req.Test, name}
+ adapter: {:test, name}

So it will be more natural for your package that actually does make a real request. Stay tuned!

wojtekmach

wojtekmach

Hex Core Team

This is awesome! Req.Test started out as something pretty similar, GitHub - wojtekmach/bandit_mock. :slight_smile:

Where Next?

Popular in Announcing Top

BartOtten
This powerful library works together with Phoenix Router to provide the ultimate routing solution. It simplifies route manipulation, givi...
New
metehan
ExTerm is a minimal Elixir library that lets you expose your terminal in a web browser. It’s mostly a toy project for learning and experi...
New
Gigitsu
Hi everyone! I’d like to share a small library I’ve recently extracted from a project I’m working on: ginject. ginject provides a minim...
New
wkirschbaum
Hi everyone. We released Spreadsheet a couple of months ago which we have been using internally for parsing xlsx files internally for our...
New
mikehostetler
LLM DB - LLM Model Metadata Package This package was extracted out of the ReqLLM project. LLM DB is a model metadata catalog with fast, ...
New
garrison
Hobbes is a scalable, fault-tolerant transactional record store written in Elixir. Hobbes is designed to be: Scalable - Hobbes can sha...
New
Antrater
Hi there! At Moon Design System, we have been working hard for the past six months on the next generation of our LiveView component libra...
New
Mati365
Phoenix has great tooling overall, but one thing that’s still lacking is a solid, plug-and-play integration with a modern WYSIWYG editor....
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
sevensidedmarble
Announcing Live Toast: a replacement toast/flash component for Phoenix LiveView, heavily inspired by the look of Sonner (the amazing toas...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement