Skip to content

Commit c1ffd1a

Browse files
committed
Fix CurrentRecorder initial state
Initial state should be `nil` to properly bypass mock when no cassette is used. See parroty#159 (comment)
1 parent d757ad4 commit c1ffd1a

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

lib/exvcr/actor.ex

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ defmodule ExVCR.Actor do
5555

5656
use ExActor.GenServer, export: __MODULE__
5757

58-
defstart(start_link(arg), do: initial_state(arg))
58+
defstart(start_link(_arg), do: default_state() |> initial_state())
5959

6060
defcast(set(x), do: new_state(x))
6161
defcall(get, state: state, do: reply(state))
62+
63+
def default_state(), do: nil
6264
end
6365
end

test/adapter_hackney_test.exs

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ defmodule ExVCR.Adapter.HackneyTest do
1313
:ok
1414
end
1515

16+
test "passthrough works when CurrentRecorder has an initial state" do
17+
if ExVCR.Application.global_mock_enabled?() do
18+
ExVCR.Actor.CurrentRecorder.default_state()
19+
|> ExVCR.Actor.CurrentRecorder.set()
20+
end
21+
url = "http://localhost:#{@port}/server"
22+
{:ok, status_code, _headers, _body} = :hackney.request(:get, url, [], [], [with_body: true])
23+
assert status_code == 200
24+
end
25+
1626
test "passthrough works after cassette has been used" do
1727
url = "http://localhost:#{@port}/server"
1828
use_cassette "hackney_get_localhost" do

test/adapter_httpc_test.exs

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ defmodule ExVCR.Adapter.HttpcTest do
1313
:ok
1414
end
1515

16+
test "passthrough works when CurrentRecorder has an initial state" do
17+
if ExVCR.Application.global_mock_enabled?() do
18+
ExVCR.Actor.CurrentRecorder.default_state()
19+
|> ExVCR.Actor.CurrentRecorder.set()
20+
end
21+
url = "http://localhost:#{@port}/server" |> to_char_list()
22+
{:ok, result} = :httpc.request(url)
23+
{{_http_version, status_code, _reason_phrase}, _headers, _body} = result
24+
assert status_code == 200
25+
end
26+
1627
test "passthrough works after cassette has been used" do
1728
url = "http://localhost:#{@port}/server" |> to_char_list()
1829
use_cassette "httpc_get_localhost" do

test/adapter_ibrowse_test.exs

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ defmodule ExVCR.Adapter.IBrowseTest do
1313
:ok
1414
end
1515

16+
test "passthrough works when CurrentRecorder has an initial state" do
17+
if ExVCR.Application.global_mock_enabled?() do
18+
ExVCR.Actor.CurrentRecorder.default_state()
19+
|> ExVCR.Actor.CurrentRecorder.set()
20+
end
21+
url = "http://localhost:#{@port}/server" |> to_char_list()
22+
{:ok, status_code, _headers, _body} = :ibrowse.send_req(url, [], :get)
23+
assert status_code == '200'
24+
end
1625

1726
test "passthrough works after cassette has been used" do
1827
url = "http://localhost:#{@port}/server" |> to_char_list()

0 commit comments

Comments
 (0)