Packages
exvcr
0.13.4
0.17.1
0.17.0
0.16.0
0.15.2
0.15.1
0.15.0
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.13.5
0.13.4
0.13.3
0.13.2
0.13.1
0.13.0
0.12.3
0.12.2
0.12.1
0.12.0
0.11.2
0.11.1
0.11.0
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.9.1
0.9.0
0.8.12
0.8.11
0.8.10
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.9
0.3.8
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.7
0.1.6
0.1.5
HTTP request/response recording library for elixir, inspired by VCR.
Current section
Files
Jump to
Current section
Files
lib/exvcr/adapter/finch.ex
if Code.ensure_loaded?(Finch) do
defmodule ExVCR.Adapter.Finch do
@moduledoc """
Provides adapter methods to mock Finch methods.
"""
use ExVCR.Adapter
alias ExVCR.Util
defmacro __using__(_opts) do
# do nothing
end
defdelegate convert_from_string(string), to: ExVCR.Adapter.Finch.Converter
defdelegate convert_to_string(request, response), to: ExVCR.Adapter.Finch.Converter
defdelegate parse_request_body(request_body), to: ExVCR.Adapter.Finch.Converter
@doc """
Returns the name of the mock target module.
"""
def module_name do
Finch
end
@doc """
Returns list of the mock target methods with function name and callback.
Implementation for global mock.
"""
def target_methods() do
[
{:request, &ExVCR.Recorder.request([&1,&2])},
{:request, &ExVCR.Recorder.request([&1,&2,&3])}
]
end
@doc """
Returns list of the mock target methods with function name and callback.
"""
def target_methods(recorder) do
[
{:request, &ExVCR.Recorder.request(recorder, [&1,&2])},
{:request, &ExVCR.Recorder.request(recorder, [&1,&2,&3])}
]
end
@doc """
Generate key for searching response.
"""
def generate_keys_for_request(request) do
req = Enum.fetch!(request, 0)
url = Util.build_url(req.scheme, req.host, req.path, req.port, req.query)
[url: url, method: String.downcase(req.method), request_body: req.body, headers: req.headers]
end
@doc """
Callback from ExVCR.Handler when response is retrieved from the HTTP server.
"""
def hook_response_from_server(response) do
apply_filters(response)
end
@doc """
Callback from ExVCR.Handler to get the response content tuple from the ExVCR.Reponse record.
"""
def get_response_value_from_cache(response) do
if response.type == "error" do
{:error, response.body}
else
finch_response = %Finch.Response{
status: response.status_code,
headers: response.headers,
body: response.body
}
{:ok, finch_response}
end
end
defp apply_filters({:ok, %Finch.Response{} = response}) do
replaced_body = to_string(response.body) |> ExVCR.Filter.filter_sensitive_data
filtered_headers = ExVCR.Filter.remove_blacklisted_headers(response.headers)
filtered_response = Map.put(response, :body, replaced_body)
|> Map.put(:headers, filtered_headers)
{:ok, filtered_response}
end
defp apply_filters({:error, reason}), do: {:error, reason}
@doc """
Default definitions for stub.
"""
def default_stub_params(:headers), do: %{"content-type" => "text/html"}
def default_stub_params(:status_code), do: 200
end
else
defmodule ExVCR.Adapter.Finch do
def module_name, do: raise "Missing dependency: Finch"
def target_methods, do: raise "Missing dependency: Finch"
end
end