Skip to content

Commit

Permalink
Merge pull request #140 from ciaran/master
Browse files Browse the repository at this point in the history
Fix errors when using request headers with ignore_localhost enabled
  • Loading branch information
parroty committed Sep 24, 2018
2 parents 79d79af + a7a1471 commit 195f440
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
25 changes: 25 additions & 0 deletions fixture/vcr_cassettes/ignore_localhost_with_headers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[
{
"request": {
"body": "",
"headers": {
"User-Agent": "ExVCR"
},
"method": "get",
"options": [],
"request_body": "",
"url": "http://127.0.0.1:34006/server"
},
"response": {
"binary": false,
"body": "test_response_before",
"headers": {
"server": "Cowboy",
"date": "Wed, 05 Sep 2018 12:05:22 GMT",
"content-length": "20"
},
"status_code": 200,
"type": "ok"
}
}
]
6 changes: 5 additions & 1 deletion lib/exvcr/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ defmodule ExVCR.Handler do
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:")))
adapter = ExVCR.Recorder.options(recorder)[:adapter]
params = adapter.generate_keys_for_request(request)

url = to_string(params[:url])
Regex.match?(~r[https?://localhost], url)
else
false
end
Expand Down
13 changes: 13 additions & 0 deletions test/ignore_localhost_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,17 @@ defmodule ExVCR.IgnoreLocalhostTest do
HttpServer.stop(@port)
end
end

test "ignore_localhost option works with request headers" do
use_cassette "ignore_localhost_with_headers", ignore_localhost: true do
non_localhost_url = "http://127.0.0.1:#{@port}/server"
HttpServer.start(path: "/server", port: @port, response: "test_response_before")
assert HTTPotion.get(non_localhost_url, headers: ["User-Agent": "ExVCR"]).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(non_localhost_url, headers: ["User-Agent": "ExVCR"]).body =~ ~r/test_response_before/
HttpServer.stop(@port)
end
end
end

0 comments on commit 195f440

Please sign in to comment.