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

Fixes :httpc's response body should be a charlist. #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/exvcr/adapter/httpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/exvcr/adapter/httpc/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions test/recorder_httpc_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@ 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

test "replace sensitive data in body" 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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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")
Expand Down