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

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

1 Like

I’m not sure if this is along the lines of what you had in mind when you said “Req.Test mocks backed by Bandit,” and if it’s not then I’d love to hear your thoughts.

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!

2 Likes