diff --git a/lib/exvcr/adapter/httpc.ex b/lib/exvcr/adapter/httpc.ex index 24dbc7a..09de1f8 100644 --- a/lib/exvcr/adapter/httpc.ex +++ b/lib/exvcr/adapter/httpc.ex @@ -56,7 +56,7 @@ defmodule ExVCR.Adapter.Httpc do defp apply_filters({:ok, {status_code, headers, body}}) do replaced_body = to_string(body) |> ExVCR.Filter.filter_sensitive_data filtered_headers = ExVCR.Filter.remove_blacklisted_headers(headers) - {:ok, {status_code, filtered_headers, replaced_body}} + {:ok, {status_code, filtered_headers, to_char_list(replaced_body)}} end defp apply_filters({:error, reason}) do diff --git a/lib/exvcr/adapter/httpc/converter.ex b/lib/exvcr/adapter/httpc/converter.ex index 06b279d..e353dd7 100644 --- a/lib/exvcr/adapter/httpc/converter.ex +++ b/lib/exvcr/adapter/httpc/converter.ex @@ -36,6 +36,15 @@ defmodule ExVCR.Adapter.Httpc.Converter do response end + response = + if response.body do + body = response.body + |> convert_string_to_char_list() + %{response | body: body} + else + response + end + response end diff --git a/test/recorder_httpc_test.exs b/test/recorder_httpc_test.exs index 73179dd..fbdb24b 100644 --- a/test/recorder_httpc_test.exs +++ b/test/recorder_httpc_test.exs @@ -23,19 +23,19 @@ defmodule ExVCR.RecorderHttpcTest do test "forcefully getting response from server by removing json in advance" do use_cassette "server1" do {:ok, {_, _, body}} = :httpc.request(@url) - assert body =~ ~r/test_response/ + assert to_string(body) =~ ~r/test_response/ end end test "forcefully getting response from server, then loading from cache by recording twice" do use_cassette "server2" do {:ok, {_, _, body}} = :httpc.request(@url) - assert body =~ ~r/test_response/ + assert to_string(body) =~ ~r/test_response/ end use_cassette "server2" do {:ok, {_, _, body}} = :httpc.request(@url) - assert body =~ ~r/test_response/ + assert to_string(body) =~ ~r/test_response/ end end @@ -43,7 +43,7 @@ defmodule ExVCR.RecorderHttpcTest do ExVCR.Config.filter_sensitive_data("test_response", "PLACEHOLDER") use_cassette "server_sensitive_data_in_body" do {:ok, {_, _, body}} = :httpc.request(@url) - assert body =~ ~r/PLACEHOLDER/ + assert to_string(body) =~ ~r/PLACEHOLDER/ end ExVCR.Config.filter_sensitive_data(nil) end @@ -52,7 +52,7 @@ defmodule ExVCR.RecorderHttpcTest do ExVCR.Config.filter_sensitive_data("password=[a-z]+", "password=***") use_cassette "server_sensitive_data_in_query" do {:ok, {_, _, body}} = :httpc.request(@url_with_query) - assert body =~ ~r/test_response/ + assert to_string(body) =~ ~r/test_response/ end # The recorded cassette should contain replaced data. @@ -67,7 +67,7 @@ defmodule ExVCR.RecorderHttpcTest do ExVCR.Config.filter_request_headers("X-My-Secret-Token") use_cassette "sensitive_data_in_request_header" do {:ok, {_, _, body}} = :httpc.request(:get, {@url_with_query, [{'X-My-Secret-Token', 'my-secret-token'}]}, [], []) - assert body == "test_response" + assert to_string(body) == "test_response" end # The recorded cassette should contain replaced data. @@ -81,7 +81,7 @@ defmodule ExVCR.RecorderHttpcTest do ExVCR.Config.filter_url_params(true) use_cassette "example_ignore_url_params" do {:ok, {_, _, body}} = :httpc.request('#{@url}?should_not_be_contained') - assert body =~ ~r/test_response/ + assert to_string(body) =~ ~r/test_response/ end json = File.read!("#{__DIR__}/../#{@dummy_cassette_dir}/example_ignore_url_params.json") refute String.contains?(json, "should_not_be_contained")