Skip to content

Commit

Permalink
Fix CurrentRecorder initial state
Browse files Browse the repository at this point in the history
Initial state should be `nil` to properly bypass mock when no cassette
is used.

See parroty#159 (comment)
  • Loading branch information
surik committed Nov 12, 2020
1 parent d757ad4 commit cf9585a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/exvcr/actor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion lib/exvcr/mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/adapter_hackney_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/adapter_httpc_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/adapter_ibrowse_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit cf9585a

Please sign in to comment.