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 · GitHub. :slight_smile:

Where Next?

Popular in Announcing Top

devonestes
Introducing assertions, the library that helps you write really great test assertions! GitHub: GitHub - devonestes/assertions: Helpful a...
New
sasajuric
I’d like to announce a small library called boundaries. This is an experimental project which explores the idea of enforcing boundaries ...
New
dominicletz
Hi, I thought I had posted my library before but seems I hadn’t. The project is still in early stages but it’s growing and so I think it...
New
benlime
LiveMotion enables high performance animations declared on the server and run on the client. As a follow up to my previous thread A libr...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
scohen
Lexical Lexical is a next-generation language server for the Elixir programming language. Features Context aware code completion As-you...
New
Flo0807
Hello everyone! I am excited to share our heart project Backpex with you. After building several Phoenix applications, we realized that...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement