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

Add ignore_localhost config option #133

Merged
merged 1 commit into from
Apr 15, 2018
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
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ config :exvcr, [
],
filter_url_params: false,
filter_request_headers: [],
response_headers_blacklist: []
response_headers_blacklist: [],
ignore_localhost: false
]
1 change: 1 addition & 0 deletions fixture/vcr_cassettes/ignore_localhost_on.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
23 changes: 23 additions & 0 deletions fixture/vcr_cassettes/ignore_localhost_unset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"request": {
"body": "",
"headers": [],
"method": "get",
"options": [],
"request_body": "",
"url": "http://localhost:34006/server"
},
"response": {
"binary": false,
"body": "test_response_before",
"headers": {
"server": "Cowboy",
"date": "Sun, 08 Apr 2018 12:50:46 GMT",
"content-length": "20"
},
"status_code": 200,
"type": "ok"
}
}
]
7 changes: 7 additions & 0 deletions lib/exvcr/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ defmodule ExVCR.Config do
blacklist = Enum.map(headers_blacklist, fn(x) -> String.downcase(x) end)
Setting.set(:response_headers_blacklist, blacklist)
end

@doc """
Skip recording cassettes for localhost requests when set
"""
def ignore_localhost(value) do
Setting.set(:ignore_localhost, value)
end
end
4 changes: 4 additions & 0 deletions lib/exvcr/config_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,9 @@ defmodule ExVCR.ConfigLoader do
else
Config.response_headers_blacklist([])
end

if env[:ignore_localhost] != nil do
Config.ignore_localhost(env[:ignore_localhost])
end
end
end
25 changes: 20 additions & 5 deletions lib/exvcr/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ defmodule ExVCR.Handler do
Get response from either server or cache.
"""
def get_response(recorder, request) do
get_response_from_cache(request, recorder) || get_response_from_server(request, recorder)
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)
end
end

@doc """
Expand Down Expand Up @@ -133,16 +137,27 @@ defmodule ExVCR.Handler do
end
end

defp get_response_from_server(request, recorder) do
raise_error_if_cassette_already_exists(recorder, inspect(request))
defp get_response_from_server(request, recorder, record?) do
adapter = ExVCR.Recorder.options(recorder)[:adapter]
response = :meck.passthrough(request)
|> adapter.hook_response_from_server
Recorder.append(recorder, adapter.convert_to_string(request, response))
ExVCR.Checker.add_server_count(recorder)
if record? do
raise_error_if_cassette_already_exists(recorder, inspect(request))
Recorder.append(recorder, adapter.convert_to_string(request, response))
ExVCR.Checker.add_server_count(recorder)
end
response
end

defp ignore_request?(request, recorder) do
ignore_localhost = ExVCR.Recorder.options(recorder)[:ignore_localhost] || ExVCR.Setting.get(:ignore_localhost)
if ignore_localhost do
Enum.any?(request, &(String.starts_with?("#{&1}", "http://localhost:")))
else
false
end
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
36 changes: 36 additions & 0 deletions test/ignore_localhost_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule ExVCR.IgnoreLocalhostTest do
use ExVCR.Mock
use ExUnit.Case, async: false

@port 34006
@url "http://localhost:#{@port}/server"

setup_all do
HTTPotion.start
:ok
end

test "it does not record localhost requests when the config has been set" do
use_cassette "ignore_localhost_on", ignore_localhost: true do
HttpServer.start(path: "/server", port: @port, response: "test_response_before")
assert HTTPotion.get(@url, []).body =~ ~r/test_response_before/
HttpServer.stop(@port)
# this method call should NOT be mocked
HttpServer.start(path: "/server", port: @port, response: "test_response_after")
assert HTTPotion.get(@url, []).body =~ ~r/test_response_after/
HttpServer.stop(@port)
end
end

test "it records localhost requests when the config has not been set" do
use_cassette "ignore_localhost_unset" do
HttpServer.start(path: "/server", port: @port, response: "test_response_before")
assert HTTPotion.get(@url, []).body =~ ~r/test_response_before/
HttpServer.stop(@port)
# this method call should be mocked
HttpServer.start(path: "/server", port: @port, response: "test_response_after")
assert HTTPotion.get(@url, []).body =~ ~r/test_response_before/
HttpServer.stop(@port)
end
end
end