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

Fix CurrentRecorder initial state #163

Merged
merged 1 commit into from
Nov 15, 2020
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
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