Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Multiple Stub Requests #216

Merged
merged 3 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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