Skip to content

Commit

Permalink
Support for Multiple Stub Requests (#216)
Browse files Browse the repository at this point in the history
* Add support for multiple stub requests

* Add multiple stub request example on README file

* Remove unnecessary pipe
  • Loading branch information
scudelletti authored Dec 9, 2023
1 parent 2af3e74 commit 01cb24f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,23 @@ test "stub request works for Finch" do
assert Map.new(response.headers)["content-type"] == "text/html"
assert response.status_code == 200
end

test "stub multiple requests works on Finch" do
stubs = [
[url: "http://example.com/1", body: "Stub Response 1", status_code: 200],
[url: "http://example.com/2", body: "Stub Response 2", status_code: 404]
]

use_cassette :stub, stubs do
{:ok, response} = Finch.build(:get, "http://example.com/1") |> Finch.request(ExVCRFinch)
assert response.status == 200
assert response.body =~ ~r/Stub Response 1/

{:ok, response} = Finch.build(:get, "http://example.com/2") |> Finch.request(ExVCRFinch)
assert response.status == 404
assert response.body =~ ~r/Stub Response 2/
end
end
```
If the specified `:url` parameter doesn't match requests called inside the
Expand Down
13 changes: 12 additions & 1 deletion lib/exvcr/mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule ExVCR.Mock do
defmacro use_cassette(:stub, options, test) do
quote do
stub_fixture = "stub_fixture_#{ExVCR.Util.uniq_id}"
stub = prepare_stub_record(unquote(options), adapter_method())
stub = prepare_stub_records(unquote(options), adapter_method())
recorder = Recorder.start([fixture: stub_fixture, stub: stub, adapter: adapter_method()])

try do
Expand Down Expand Up @@ -135,6 +135,17 @@ defmodule ExVCR.Mock do
|> Task.await(:infinity)
end

@doc """
Prepare stub records
"""
def prepare_stub_records(options, adapter) do
if Keyword.keyword?(options) do
prepare_stub_record(options, adapter)
else
Enum.flat_map(options, &prepare_stub_record(&1, adapter))
end
end

@doc """
Prepare stub record based on specified option parameters.
"""
Expand Down
17 changes: 17 additions & 0 deletions test/adapter_finch_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ defmodule ExVCR.Adapter.FinchTest do
end
end

test "stub multiple requests works on Finch" do
stubs = [
[url: "http://example.com/1", body: "Stub Response 1", status_code: 200],
[url: "http://example.com/2", body: "Stub Response 2", status_code: 404]
]

use_cassette :stub, stubs do
{:ok, response} = Finch.build(:get, "http://example.com/1") |> Finch.request(ExVCRFinch)
assert response.status == 200
assert response.body =~ ~r/Stub Response 1/

{:ok, response} = Finch.build(:get, "http://example.com/2") |> Finch.request(ExVCRFinch)
assert response.status == 404
assert response.body =~ ~r/Stub Response 2/
end
end

test "single request using request!" do
use_cassette "example_finch" do
response = Finch.build(:get, "http://example.com") |> Finch.request!(ExVCRFinch)
Expand Down
17 changes: 17 additions & 0 deletions test/adapter_ibrowse_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,23 @@ defmodule ExVCR.Adapter.IBrowseTest do
end
end

test "stub multiple requests works for ibrowse" do
stubs = [
[url: "http://example.com/1", body: "Stub Response 1", status_code: 200],
[url: "http://example.com/2", body: "Stub Response 2", status_code: 404]
]

use_cassette :stub, stubs do
{:ok, status_code, _headers, body} = :ibrowse.send_req('http://example.com/1', [], :get)
assert status_code == '200'
assert to_string(body) =~ ~r/Stub Response 1/

{:ok, status_code, _headers, body} = :ibrowse.send_req('http://example.com/2', [], :get)
assert status_code == '404'
assert to_string(body) =~ ~r/Stub Response 2/
end
end

defp assert_response(response, function \\ nil) do
assert HTTPotion.Response.success?(response, :extra)
assert response.headers[:Connection] == "keep-alive"
Expand Down

0 comments on commit 01cb24f

Please sign in to comment.