From 77348e28f83723610f9a80509a8b3e784e83a838 Mon Sep 17 00:00:00 2001 From: Caio Ergos Date: Sat, 10 Sep 2022 11:13:40 -0300 Subject: [PATCH] Better document the ignore_urls setting (#187) * Allow append to be passed to a previously nil setting * Update README with ignore_urls instructions --- README.md | 16 ++++++++++++++++ lib/exvcr/setting.ex | 5 ++++- test/setting_test.exs | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3868841..f2cfc5f 100644 --- a/README.md +++ b/README.md @@ -270,6 +270,22 @@ test "replace sensitive data in request options" do end ``` +#### Allowed hosts + +The `:ignore_urls` can be used to allow requests to be made to certain hosts. + +```elixir +setup do + ExVCR.Setting.set(:ignore_urls, [~/example.com/]) + ExVCR.Setting.append(:ignore_urls, ~/anotherurl.com/) +end + +test "an actual request is made to example.com" do + HTTPoison.get!("https://example.com/path?query=true") + HTTPoison.get!("https://anotherurl.com/path?query=true") +end +``` + #### Ignoring query params in URL If `ExVCR.Config.filter_url_params(true)` is specified, query params in URL diff --git a/lib/exvcr/setting.ex b/lib/exvcr/setting.ex index b70769c..fb3970b 100644 --- a/lib/exvcr/setting.ex +++ b/lib/exvcr/setting.ex @@ -14,7 +14,10 @@ defmodule ExVCR.Setting do end def append(key, value) do - set(key, [value | ExVCR.Setting.get(key)]) + case __MODULE__.get(key) do + [_ | _] = values -> __MODULE__.set(key, [value | values]) + _ -> __MODULE__.set(key, [value]) + end end defp setup do diff --git a/test/setting_test.exs b/test/setting_test.exs index 4e6bc72..15f2081 100644 --- a/test/setting_test.exs +++ b/test/setting_test.exs @@ -27,4 +27,20 @@ defmodule ExVCR.SettingTest do ExVCR.Setting.set(:response_headers_blacklist, ["Content-Type", "Accept"]) assert ExVCR.Setting.get(:response_headers_blacklist) == ["Content-Type", "Accept"] end + + test "set ignore_urls" do + ExVCR.Setting.set(:ignore_urls, ["example.com"]) + assert ExVCR.Setting.get(:ignore_urls) == ["example.com"] + end + + test "append ignore_urls when there are no existing values" do + ExVCR.Setting.append(:ignore_urls, "example.com") + assert ExVCR.Setting.get(:ignore_urls) == ["example.com"] + end + + test "append ignore_urls when there are existing values" do + ExVCR.Setting.set(:ignore_urls, [~r/example.com/]) + ExVCR.Setting.append(:ignore_urls, ~r/example2.com/) + assert ExVCR.Setting.get(:ignore_urls) == [~r/example2.com/, ~r/example.com/] + end end