From 01cb24f2feb5e66659130885ad6fe31a427cbfd0 Mon Sep 17 00:00:00 2001 From: scudelletti Date: Sat, 9 Dec 2023 14:26:37 +0100 Subject: [PATCH] Support for Multiple Stub Requests (#216) * Add support for multiple stub requests * Add multiple stub request example on README file * Remove unnecessary pipe --- README.md | 17 +++++++++++++++++ lib/exvcr/mock.ex | 13 ++++++++++++- test/adapter_finch_test.exs | 17 +++++++++++++++++ test/adapter_ibrowse_test.exs | 17 +++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7effbf3..c9c0df1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/exvcr/mock.ex b/lib/exvcr/mock.ex index 518c7fd..c06f43e 100644 --- a/lib/exvcr/mock.ex +++ b/lib/exvcr/mock.ex @@ -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 @@ -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. """ diff --git a/test/adapter_finch_test.exs b/test/adapter_finch_test.exs index 6ca0c89..5b592ae 100644 --- a/test/adapter_finch_test.exs +++ b/test/adapter_finch_test.exs @@ -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) diff --git a/test/adapter_ibrowse_test.exs b/test/adapter_ibrowse_test.exs index 3ee725d..78b8711 100644 --- a/test/adapter_ibrowse_test.exs +++ b/test/adapter_ibrowse_test.exs @@ -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"