/
converter.ex
91 lines (75 loc) · 2.34 KB
/
converter.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
if Code.ensure_loaded?(Finch) do
defmodule ExVCR.Adapter.Finch.Converter do
@moduledoc """
Provides helpers to mock Finch methods.
"""
use ExVCR.Converter
alias ExVCR.Util
defp string_to_response(string) do
response = Enum.map(string, fn {x, y} -> {String.to_atom(x), y} end)
response = struct(ExVCR.Response, response)
response =
if response.type == "error" do
body = string_to_error_reason(response.body)
%{response | body: body}
else
response
end
response =
if is_map(response.headers) do
headers =
response.headers
|> Map.to_list()
|> Enum.map(fn {k, v} -> {k, v} end)
%{response | headers: headers}
else
response
end
response
end
defp string_to_error_reason(reason) do
{reason_struct, _} = Code.eval_string(reason)
reason_struct
end
defp request_to_string([request, finch_module]) do
request_to_string([request, finch_module, []])
end
defp request_to_string([request, _finch_module, opts]) do
url =
Util.build_url(request.scheme, request.host, request.path, request.port, request.query)
%ExVCR.Request{
url: parse_url(url),
headers: parse_headers(request.headers),
method: String.downcase(request.method),
body: parse_request_body(request.body),
options: parse_options(sanitize_options(opts))
}
end
# If option value is tuple, make it as list, for encoding as json.
defp sanitize_options(options) do
Enum.map(options, fn {key, value} ->
if is_tuple(value) do
{key, Tuple.to_list(value)}
else
{key, value}
end
end)
end
defp response_to_string({:ok, %Finch.Response{} = response}), do: response_to_string(response)
defp response_to_string(%Finch.Response{} = response) do
%ExVCR.Response{
type: "ok",
status_code: response.status,
headers: parse_headers(response.headers),
body: to_string(response.body)
}
end
defp response_to_string({:error, reason}) do
%ExVCR.Response{
type: "error",
body: error_reason_to_string(reason)
}
end
defp error_reason_to_string(reason), do: Macro.to_string(reason)
end
end