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
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
This is awesome! Req.Test started out as something pretty similar, GitHub - wojtekmach/bandit_mock · GitHub. ![]()
Popular in Announcing
Other popular 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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









