Skip to content

Commit

Permalink
Merge 4708b66 into b45c6af
Browse files Browse the repository at this point in the history
  • Loading branch information
LexMalta committed Feb 23, 2020
2 parents b45c6af + 4708b66 commit b423a90
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
3 changes: 2 additions & 1 deletion config/config.exs
Expand Up @@ -10,5 +10,6 @@ config :exvcr, [
filter_request_headers: [],
response_headers_blacklist: [],
ignore_localhost: false,
enable_global_settings: false
enable_global_settings: false,
strict_mode: false
]
7 changes: 7 additions & 0 deletions lib/exvcr/config.ex
Expand Up @@ -84,4 +84,11 @@ defmodule ExVCR.Config do
def ignore_localhost(value) do
Setting.set(:ignore_localhost, value)
end

@doc """
Throw error if there is no matching cassette for an HTTP request
"""
def strict_mode(value) do
Setting.set(:strict_mode, value)
end
end
4 changes: 4 additions & 0 deletions lib/exvcr/config_loader.ex
Expand Up @@ -56,5 +56,9 @@ defmodule ExVCR.ConfigLoader do
if env[:ignore_localhost] != nil do
Config.ignore_localhost(env[:ignore_localhost])
end

if env[:strict_mode] != nil do
Config.strict_mode(env[:strict_mode])
end
end
end
19 changes: 18 additions & 1 deletion lib/exvcr/handler.ex
Expand Up @@ -14,7 +14,9 @@ defmodule ExVCR.Handler do
if ignore_request?(request, recorder) do
get_response_from_server(request, recorder, false)
else
get_response_from_cache(request, recorder) || get_response_from_server(request, recorder, true)
get_response_from_cache(request, recorder) ||
ignore_server_fetch!(request, recorder) ||
get_response_from_server(request, recorder, true)
end
end

Expand Down Expand Up @@ -182,6 +184,21 @@ defmodule ExVCR.Handler do
end
end

defp ignore_server_fetch!(request, recorder) do
strict_mode = ExVCR.Recorder.options(recorder)[:strict_mode] || ExVCR.Setting.get(:strict_mode)
if strict_mode do
message = """
A matching cassette was not found for this request.
An error was raise, rather than recording a cassette, because the option `strict_mode` is turned on.
Request: #{inspect(request)}
"""
raise ExVCR.RequestNotMatchError, message: message
end
false
end

defp raise_error_if_cassette_already_exists(recorder, request_description) do
file_path = ExVCR.Recorder.get_file_path(recorder)
if File.exists?(file_path) do
Expand Down
51 changes: 51 additions & 0 deletions test/strict_mode_test.exs
@@ -0,0 +1,51 @@
defmodule ExVCR.StrictModeTest do
use ExVCR.Mock
use ExUnit.Case, async: false

@dummy_cassette_dir "tmp/vcr_tmp/vcr_cassettes_strict_mode"
@port 34007
@url "http://localhost:#{@port}/server"
@http_server_opts [path: "/server", port: @port, response: "test_response"]

setup_all do
File.rm_rf(@dummy_cassette_dir)

on_exit fn ->
File.rm_rf(@dummy_cassette_dir)
HttpServer.stop(@port)
:ok
end

HTTPotion.start
HttpServer.start(@http_server_opts)
:ok
end

setup do
ExVCR.Config.cassette_library_dir(@dummy_cassette_dir)
end

test "it makes HTTP calls if not set" do
use_cassette "strict_mode_off", strict_mode: false do
assert HTTPotion.get(@url, []).body =~ ~r/test_response/
end
end

test "it throws an error when set and no cassette recorded" do
use_cassette "strict_mode_on", strict_mode: true do
assert_raise ExVCR.RequestNotMatchError, fn ->
HTTPotion.get(@url, []).body =~ ~r/test_response/
end
end
end

test "it uses a cassette if it exists" do
use_cassette "strict_mode_cassette", strict_mode: false do
assert HTTPotion.get(@url, []).body =~ ~r/test_response/
end

use_cassette "strict_mode_cassette", strict_mode: true do
assert HTTPotion.get(@url, []).body =~ ~r/test_response/
end
end
end

0 comments on commit b423a90

Please sign in to comment.