From cf9585aaac7ecb48834bab0223c2ea3293bbb46d Mon Sep 17 00:00:00 2001 From: Yury Gargay Date: Thu, 12 Nov 2020 19:25:08 +0100 Subject: [PATCH] Fix CurrentRecorder initial state Initial state should be `nil` to properly bypass mock when no cassette is used. See https://github.com/parroty/exvcr/issues/159#issuecomment-726233421 --- lib/exvcr/actor.ex | 4 +++- lib/exvcr/mock.ex | 3 ++- test/adapter_hackney_test.exs | 10 ++++++++++ test/adapter_httpc_test.exs | 11 +++++++++++ test/adapter_ibrowse_test.exs | 9 +++++++++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/exvcr/actor.ex b/lib/exvcr/actor.ex index 3447012..b22ec20 100644 --- a/lib/exvcr/actor.ex +++ b/lib/exvcr/actor.ex @@ -55,9 +55,11 @@ defmodule ExVCR.Actor do use ExActor.GenServer, export: __MODULE__ - defstart(start_link(arg), do: initial_state(arg)) + defstart(start_link(_arg), do: default_state() |> initial_state()) defcast(set(x), do: new_state(x)) defcall(get, state: state, do: reply(state)) + + def default_state(), do: nil end end diff --git a/lib/exvcr/mock.ex b/lib/exvcr/mock.ex index 3f3e44e..b2c5459 100644 --- a/lib/exvcr/mock.ex +++ b/lib/exvcr/mock.ex @@ -102,7 +102,8 @@ defmodule ExVCR.Mock do @doc false def unload(module_name) do if ExVCR.Application.global_mock_enabled?() do - ExVCR.Actor.CurrentRecorder.set(nil) + ExVCR.Actor.CurrentRecorder.default_state() + |> ExVCR.Actor.CurrentRecorder.set() else :meck.unload(module_name) end diff --git a/test/adapter_hackney_test.exs b/test/adapter_hackney_test.exs index 4789091..335e1e8 100644 --- a/test/adapter_hackney_test.exs +++ b/test/adapter_hackney_test.exs @@ -13,6 +13,16 @@ defmodule ExVCR.Adapter.HackneyTest do :ok end + test "passthrough works when CurrentRecorder has an initial state" do + if ExVCR.Application.global_mock_enabled?() do + ExVCR.Actor.CurrentRecorder.default_state() + |> ExVCR.Actor.CurrentRecorder.set() + end + url = "http://localhost:#{@port}/server" + {:ok, status_code, _headers, _body} = :hackney.request(:get, url, [], [], [with_body: true]) + assert status_code == 200 + end + test "passthrough works after cassette has been used" do url = "http://localhost:#{@port}/server" use_cassette "hackney_get_localhost" do diff --git a/test/adapter_httpc_test.exs b/test/adapter_httpc_test.exs index d4e05e7..dddb3c3 100644 --- a/test/adapter_httpc_test.exs +++ b/test/adapter_httpc_test.exs @@ -13,6 +13,17 @@ defmodule ExVCR.Adapter.HttpcTest do :ok end + test "passthrough works when CurrentRecorder has an initial state" do + if ExVCR.Application.global_mock_enabled?() do + ExVCR.Actor.CurrentRecorder.default_state() + |> ExVCR.Actor.CurrentRecorder.set() + end + url = "http://localhost:#{@port}/server" |> to_char_list() + {:ok, result} = :httpc.request(url) + {{_http_version, status_code, _reason_phrase}, _headers, _body} = result + assert status_code == 200 + end + test "passthrough works after cassette has been used" do url = "http://localhost:#{@port}/server" |> to_char_list() use_cassette "httpc_get_localhost" do diff --git a/test/adapter_ibrowse_test.exs b/test/adapter_ibrowse_test.exs index 3b8a363..6ab436d 100644 --- a/test/adapter_ibrowse_test.exs +++ b/test/adapter_ibrowse_test.exs @@ -13,6 +13,15 @@ defmodule ExVCR.Adapter.IBrowseTest do :ok end + test "passthrough works when CurrentRecorder has an initial state" do + if ExVCR.Application.global_mock_enabled?() do + ExVCR.Actor.CurrentRecorder.default_state() + |> ExVCR.Actor.CurrentRecorder.set() + end + url = "http://localhost:#{@port}/server" |> to_char_list() + {:ok, status_code, _headers, _body} = :ibrowse.send_req(url, [], :get) + assert status_code == '200' + end test "passthrough works after cassette has been used" do url = "http://localhost:#{@port}/server" |> to_char_list()