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 support for Maps on request_body matcher #210

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 9 additions & 5 deletions lib/exvcr/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,15 @@ defmodule ExVCR.Handler do
request_body = response[:request].body || response[:request].request_body
key_body = keys[:request_body] |> to_string |> ExVCR.Filter.filter_sensitive_data

if match = Regex.run(~r/~r\/(.+)\//, request_body) do
pattern = Regex.compile!(Enum.at(match, 1))
Regex.match?(pattern, key_body)
else
request_body == key_body
cond do
match = Regex.run(~r/~r\/(.+)\//, request_body) ->
pattern = Regex.compile!(Enum.at(match, 1))
Regex.match?(pattern, key_body)

Regex.run(~r/^%{.*}$/, request_body) ->
{result, _} = Code.eval_string(request_body)
JSX.decode!(key_body) == JSX.decode!(JSX.encode!(result))
true -> request_body == key_body
end
else
true
Expand Down
8 changes: 8 additions & 0 deletions test/handler_stub_mode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ defmodule ExVCR.Adapter.HandlerStubModeTest do
end
end

test "request_body matches as map" do
use_cassette :stub, [url: 'http://localhost', method: :post, request_body: "%{param1: \"value1\", param2: \"value2\"}", body: "Hello World"] do
{:ok, status_code, _headers, body} = :ibrowse.send_req('http://localhost', [], :post, '{"param1":"value1", "param2":"value2"}')
assert status_code == '200'
assert to_string(body) =~ ~r/Hello World/
end
end

test "request_body mismatches as regex" do
assert_raise ExVCR.InvalidRequestError, fn ->
use_cassette :stub, [url: 'http://localhost', method: :post, request_body: "~r/param3/", body: "Hello World"] do
Expand Down
Loading