Skip to content

Commit

Permalink
Merge c4f0579 into 172a14e
Browse files Browse the repository at this point in the history
  • Loading branch information
edgurgel committed May 7, 2014
2 parents 172a14e + c4f0579 commit 4c92eae
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 53 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -5,6 +5,7 @@ before_install:
- mkdir -p vendor/elixir
- wget -q https://github.com/elixir-lang/elixir/releases/download/v0.13.1/Precompiled.zip && unzip -qq Precompiled.zip -d vendor/elixir
- export PATH="$PATH:$PWD/vendor/elixir/bin"
- mix local.hex --force
script: "MIX_ENV=test mix do deps.get, test"
after_success:
- "mix compile && mix coveralls.travis"
12 changes: 6 additions & 6 deletions lib/exvcr/adapter/hackney.ex
Expand Up @@ -43,12 +43,12 @@ defmodule ExVCR.Adapter.Hackney do
Callback from ExVCR.Handler when response is retrieved from the json file cache.
"""
def hook_response_from_cache(nil), do: nil
def hook_response_from_cache(ExVCR.Response[type: "error"] = response), do: response
def hook_response_from_cache(ExVCR.Response[body: body] = response) do
def hook_response_from_cache(%ExVCR.Response{type: "error"} = response), do: response
def hook_response_from_cache(%ExVCR.Response{body: body} = response) do
client = make_ref
client_key_atom = client |> inspect |> binary_to_atom
Store.set(client_key_atom, body)
response.body(client)
%{response | body: client}
end

defp handle_body_request(recorder, [client]) do
Expand All @@ -63,11 +63,11 @@ defmodule ExVCR.Adapter.Hackney do

client_key_string = inspect(client)
ExVCR.Recorder.update(recorder,
fn([request: _request, response: response]) ->
fn(%{request: _request, response: response}) ->
response.body == client_key_string
end,
fn([request: request, response: response]) ->
[request: request, response: response.body(body)]
fn(%{request: request, response: response}) ->
%{request: request, response: %{response | body: body}}
end
)
end
Expand Down
15 changes: 8 additions & 7 deletions lib/exvcr/adapter/hackney/converter.ex
Expand Up @@ -6,33 +6,34 @@ defmodule ExVCR.Adapter.Hackney.Converter do
use ExVCR.Converter

defp string_to_response(string) do
Enum.map(string, fn({x, y}) -> {binary_to_atom(x), y} end) |> ExVCR.Response.new
response = Enum.traverse(string, fn({x, y}) -> {binary_to_atom(x), y} end)
struct(ExVCR.Response, response)
end

defp request_to_string([method, url, headers, body, options]) do
ExVCR.Request.new(
%ExVCR.Request{
url: parse_url(url),
headers: parse_headers(headers),
method: to_string(method),
body: parse_request_body(body),
options: options
)
}
end

# Client is already replaced by body through ExVCR.Adapter.Hackney adapter.
defp response_to_string({:ok, status_code, headers, client}) do
ExVCR.Response.new(
%ExVCR.Response{
type: "ok",
status_code: status_code,
headers: parse_headers(headers),
body: inspect client
)
}
end

defp response_to_string({:error, reason}) do
ExVCR.Response.new(
%ExVCR.Response{
type: "error",
body: atom_to_binary(reason)
)
}
end
end
19 changes: 10 additions & 9 deletions lib/exvcr/adapter/httpc/converter.ex
Expand Up @@ -6,14 +6,15 @@ defmodule ExVCR.Adapter.Httpc.Converter do
use ExVCR.Converter

defp string_to_response(string) do
response = Enum.map(string, fn({x, y}) -> {binary_to_atom(x), y} end) |> ExVCR.Response.new
response = Enum.traverse(string, fn({x, y}) -> {binary_to_atom(x), y} end)
response = struct(ExVCR.Response, response)

if response.status_code do
response = response.update(status_code: list_to_tuple(response.status_code))
response = %{response | status_code: list_to_tuple(response.status_code)}
end

if response.type == "error" do
response = response.update(body: {binary_to_atom(response.body), []})
response = %{response | body: {binary_to_atom(response.body), []}}
end

response
Expand All @@ -28,28 +29,28 @@ defmodule ExVCR.Adapter.Httpc.Converter do

# TODO: need to handle content_type
defp request_to_string([method, {url, headers, _content_type, body}, http_options, options]) do
ExVCR.Request.new(
%ExVCR.Request{
url: parse_url(url),
headers: parse_headers(headers),
method: to_string(method),
body: parse_request_body(body),
options: [httpc_options: parse_keyword_list(options), http_options: parse_keyword_list(http_options)]
)
}
end

defp response_to_string({:ok, {{http_version, status_code, reason_phrase}, headers, body}}) do
ExVCR.Response.new(
%ExVCR.Response{
type: "ok",
status_code: [to_string(http_version), status_code, to_string(reason_phrase)],
headers: parse_headers(headers),
body: to_string(body)
)
}
end

defp response_to_string({:error, {reason, _detail}}) do
ExVCR.Response.new(
%ExVCR.Response{
type: "error",
body: atom_to_binary(reason)
)
}
end
end
24 changes: 13 additions & 11 deletions lib/exvcr/adapter/ibrowse/converter.ex
Expand Up @@ -6,49 +6,51 @@ defmodule ExVCR.Adapter.IBrowse.Converter do
use ExVCR.Converter

defp string_to_response(string) do
response = Enum.map(string, fn({x, y}) -> {binary_to_atom(x), y} end) |> ExVCR.Response.new
response = Enum.traverse(string, fn({x, y}) -> {binary_to_atom(x), y} end)
response = struct(ExVCR.Response, response)

if response.status_code do
response = response.update(status_code: integer_to_list(response.status_code))
response = %{response | status_code: integer_to_list(response.status_code)}
end

if response.type == "error" do
response = response.update(string_to_error_reason(response.body))
body = string_to_error_reason(response.body)
response = %{response | body: body}
end

response
end

defp string_to_error_reason([reason, details]), do: [body: { binary_to_atom(reason), binary_to_tuple(details) }]
defp string_to_error_reason([reason]), do: [body: binary_to_atom(reason)]
defp string_to_error_reason([reason, details]), do: { binary_to_atom(reason), binary_to_tuple(details) }
defp string_to_error_reason([reason]), do: binary_to_atom(reason)

defp request_to_string([url, headers, method]), do: request_to_string([url, headers, method, [], []])
defp request_to_string([url, headers, method, body]), do: request_to_string([url, headers, method, body, []])
defp request_to_string([url, headers, method, body, options]), do: request_to_string([url, headers, method, body, options, 5000])
defp request_to_string([url, headers, method, body, options, _timeout]) do
ExVCR.Request.new(
%ExVCR.Request{
url: parse_url(url),
headers: parse_headers(headers),
method: atom_to_binary(method),
body: parse_request_body(body),
options: options
)
}
end

defp response_to_string({:ok, status_code, headers, body}) do
ExVCR.Response.new(
%ExVCR.Response{
type: "ok",
status_code: list_to_integer(status_code),
headers: parse_headers(headers),
body: to_string(body)
)
}
end

defp response_to_string({:error, reason}) do
ExVCR.Response.new(
%ExVCR.Response{
type: "error",
body: error_reason_to_string(reason)
)
}
end

defp error_reason_to_string({reason, details}), do: [atom_to_binary(reason), tuple_to_binary(details)]
Expand Down
9 changes: 5 additions & 4 deletions lib/exvcr/converter.ex
Expand Up @@ -8,21 +8,22 @@ defmodule ExVCR.Converter do
@doc """
Parse string format into original request / response tuples.
"""
def convert_from_string([{"request", request}, {"response", response}]) do
[ request: string_to_request(request), response: string_to_response(response) ]
def convert_from_string(%{"request" => request, "response" => response}) do
%{ request: string_to_request(request), response: string_to_response(response) }
end
defoverridable [convert_from_string: 1]

@doc """
Parse request and response tuples into string format.
"""
def convert_to_string(request, response) do
[ request: request_to_string(request), response: response_to_string(response) ]
%{ request: request_to_string(request), response: response_to_string(response) }
end
defoverridable [convert_to_string: 2]

defp string_to_request(string) do
Enum.map(string, fn({x,y}) -> {binary_to_atom(x),y} end) |> ExVCR.Request.new
request = Enum.map(string, fn({x,y}) -> {binary_to_atom(x),y} end) |> Enum.into(%{})
struct(ExVCR.Request, request)
end
defoverridable [string_to_request: 1]

Expand Down
2 changes: 1 addition & 1 deletion lib/exvcr/recorder.ex
Expand Up @@ -14,7 +14,7 @@ defmodule ExVCR.Recorder do
{:ok, act_responses} = Responses.start([])
{:ok, act_options} = Options.start(options)

recorder = ExVCR.Record.new(options: act_options, responses: act_responses)
recorder = %ExVCR.Record{options: act_options, responses: act_responses}
load(recorder)
recorder
end
Expand Down
23 changes: 18 additions & 5 deletions lib/exvcr/records.ex
@@ -1,5 +1,18 @@
defrecord ExVCR.Record, options: nil, responses: nil
defrecord ExVCR.Request, url: nil, headers: [], method: nil, body: nil, options: []
defrecord ExVCR.Response, type: "ok", status_code: nil, headers: [], body: nil
defrecord ExVCR.Checker.Results, dirs: nil, files: []
defrecord ExVCR.Checker.Counts, server: 0, cache: 0
defmodule ExVCR.Record do
defstruct options: nil, responses: nil
end

defmodule ExVCR.Request do
defstruct url: nil, headers: [], method: nil, body: nil, options: []
end

defmodule ExVCR.Response do
defstruct type: "ok", status_code: nil, headers: [], body: nil
end

defmodule ExVCR.Checker.Results do
defstruct dirs: nil, files: []
end
defmodule ExVCR.Checker.Counts do
defstruct server: 0, cache: 0
end
10 changes: 5 additions & 5 deletions lib/exvcr/task/runner.ex
Expand Up @@ -75,7 +75,7 @@ defmodule ExVCR.Task.Runner do
Check and show which cassettes are used by the test execution.
"""
def check_cassettes(record) do
count_hash = create_count_hash(record.files, HashDict.new)
count_hash = create_count_hash(record.files, %{})
Enum.each(record.dirs, fn(dir) ->
IO.puts "Showing hit counts of cassettes in [#{dir}]"
if File.exists?(dir) do
Expand All @@ -89,18 +89,18 @@ defmodule ExVCR.Task.Runner do
defp create_count_hash([], acc), do: acc
defp create_count_hash([{type, path}|tail], acc) do
file = Path.basename(path)
counts = HashDict.get(acc, file, ExVCR.Checker.Counts.new)
counts = Dict.get(acc, file, %ExVCR.Checker.Counts{})
hash = case type do
:cache -> HashDict.put(acc, file, counts.update(cache: counts.cache + 1))
:server -> HashDict.put(acc, file, counts.update(server: counts.server + 1))
:cache -> Dict.put(acc, file, %{counts | cache: counts.cache + 1})
:server -> Dict.put(acc, file, %{counts | server: counts.server + 1})
end
create_count_hash(tail, hash)
end

defp print_check_cassettes(items, counts_hash) do
printf(@check_header_format, ["[File Name]", "[Cassette Counts]", "[Server Counts]"])
Enum.each(items, fn({name, _date}) ->
counts = HashDict.get(counts_hash, name, ExVCR.Checker.Counts.new)
counts = Dict.get(counts_hash, name, %ExVCR.Checker.Counts{})
printf(@check_content_format, [name, counts.cache, counts.server])
end)
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Expand Up @@ -36,7 +36,7 @@ defmodule ExVCR.Mixfile do
[
{:meck, "0.8.1", github: "eproxus/meck"},
{:exactor, github: "sasa1977/exactor"},
{:jsex, github: "talentdeficit/jsex"},
{:jsex, "~> 2.0", override: true},
{:exprintf, github: "parroty/exprintf"}
]
end
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Expand Up @@ -10,8 +10,8 @@
"httpotion": {:git, "git://github.com/myfreeweb/httpotion.git", "467ab1e0e024d7d82423b16e1c4da93b21568ffd", []},
"ibrowse": {:git, "git://github.com/cmullaparthi/ibrowse.git", "866b0ff5aca229f1ef53653eabc8ed1720c13cd6", [ref: "866b0ff5aca229f1ef53653eabc8ed1720c13cd6"]},
"idna": {:git, "git://github.com/benoitc/erlang-idna.git", "21aa158b7f36bf2a000f12fe40d7f21eece9cf65", [tag: "hackney-0.11.2"]},
"jsex": {:git, "git://github.com/talentdeficit/jsex.git", "03ad4ff0967331afd01464857d41e5e497ed198c", []},
"jsx": {:git, "git://github.com/talentdeficit/jsx.git", "507fa4c41db33c81e925ab53f4d789d234aaff2f", [tag: "v2.0.1"]},
"jsex": {:package, "2.0.0"},
"jsx": {:package, "2.0.2"},
"meck": {:git, "git://github.com/eproxus/meck.git", "9987eea73dab37ce014dbd4fa715a31cc07d351e", []},
"mimetypes": {:git, "git://github.com/spawngrid/mimetypes.git", "47d37a977a7d633199822bf6b08353007483d00f", [ref: "master"]},
"ranch": {:git, "git://github.com/extend/ranch.git", "41705752ffc7e3ecb96eef8374015ead4ca7f699", [ref: "0.8.5"]}}
4 changes: 2 additions & 2 deletions test/task_runner_test.exs
Expand Up @@ -29,9 +29,9 @@ defmodule ExVCR.TaskRunnerTest do

test "check vcr cassettes task prints json file summary" do
result = capture_io(fn ->
record = ExVCR.Checker.Results.new(
record = %ExVCR.Checker.Results{
dirs: ["test/cassettes"],
files: [{:cache, "test1.json"}, {:cache, "test2.json"}, {:server, "test1.json"}, {:server, "test1.json"}])
files: [{:cache, "test1.json"}, {:cache, "test2.json"}, {:server, "test1.json"}, {:server, "test1.json"}]}
ExVCR.Task.Runner.check_cassettes(record)
end)

Expand Down

0 comments on commit 4c92eae

Please sign in to comment.