diff --git a/lib/exvcr/iex.ex b/lib/exvcr/iex.ex index 4ca19fe..9db83c3 100644 --- a/lib/exvcr/iex.ex +++ b/lib/exvcr/iex.ex @@ -26,8 +26,7 @@ defmodule ExVCR.IEx do after ExVCR.MockLock.release_lock() Recorder.get(recorder) - |> JSX.encode! - |> JSX.prettify! + |> Jason.encode_to_iodata!(pretty: true) |> IO.puts end :ok diff --git a/lib/exvcr/json.ex b/lib/exvcr/json.ex index fd7e63e..138d4e3 100644 --- a/lib/exvcr/json.ex +++ b/lib/exvcr/json.ex @@ -10,8 +10,7 @@ defmodule ExVCR.JSON do json = recordings |> Enum.map(&encode_binary_data/1) |> Enum.reverse() - |> JSX.encode!() - |> JSX.prettify!() + |> Jason.encode_to_iodata!(pretty: true) unless File.exists?(path = Path.dirname(file_name)), do: File.mkdir_p!(path) File.write!(file_name, json) @@ -48,7 +47,7 @@ defmodule ExVCR.JSON do def read_json_file(file_name) do file_name |> File.read!() - |> JSX.decode!() + |> Jason.decode!() |> Enum.map(&load_binary_data/1) end diff --git a/lib/exvcr/records.ex b/lib/exvcr/records.ex index 161a9cd..77ad7d7 100644 --- a/lib/exvcr/records.ex +++ b/lib/exvcr/records.ex @@ -4,10 +4,57 @@ end defmodule ExVCR.Request do defstruct url: nil, headers: [], method: nil, body: nil, options: [], request_body: "" + + defimpl Jason.Encoder do + def encode(request, opts) do + request + |> Map.update!(:headers, &Map.new/1) + |> Map.update!(:options, fn options -> options |> clean_invalid() |> Map.new() end) + |> Map.take([:url, :headers, :method, :body, :options, :request_body]) + |> Jason.Encode.map(opts) + end + + defp clean_invalid([{_, _} | _] = kw) do + kw |> Enum.map(fn {k, v} -> {k, clean_invalid(v)} end) |> Map.new() + end + + defp clean_invalid([value | rest]) do + [clean_invalid(value) | clean_invalid(rest)] + end + + defp clean_invalid([] = empty), do: empty + + defp clean_invalid(map) when is_map(map) do + map |> Map.to_list() |> clean_invalid() |> Map.new() + end + + defp clean_invalid(tuple) when is_tuple(tuple) do + tuple |> Tuple.to_list() |> clean_invalid() |> List.to_tuple() + end + + defp clean_invalid(bin) when is_binary(bin) do + clean_bin(bin) + end + + defp clean_invalid(other), do: other + + defp clean_bin(<>), do: <> <> clean_bin(rest) + defp clean_bin(<<_invalid, rest::bytes>>), do: "�" <> clean_bin(rest) + defp clean_bin(<<>> = empty), do: empty + end end defmodule ExVCR.Response do defstruct type: "ok", status_code: nil, headers: [], body: nil, binary: false + + defimpl Jason.Encoder do + def encode(response, opts) do + response + |> Map.update!(:headers, &Map.new/1) + |> Map.take([:type, :status_code, :headers, :body, :binary]) + |> Jason.Encode.map(opts) + end + end end defmodule ExVCR.Checker.Results do diff --git a/lib/exvcr/task/show.ex b/lib/exvcr/task/show.ex index 4bba7c7..0f14e6a 100644 --- a/lib/exvcr/task/show.ex +++ b/lib/exvcr/task/show.ex @@ -16,7 +16,7 @@ defmodule ExVCR.Task.Show do IO.puts "\e[32mShowing #{file}\e[m" IO.puts "\e[32m**************************************\e[m" json = File.read!(file) - IO.puts json |> JSX.prettify! |> String.replace(~r/\\n/, "\n") + IO.puts json |> Jason.Formatter.pretty_print() |> String.replace(~r/\\n/, "\n") display_parsed_body(json) IO.puts "\e[32m**************************************\e[m" else @@ -25,17 +25,15 @@ defmodule ExVCR.Task.Show do end defp display_parsed_body(json) do - case extract_body(json) |> JSX.prettify do - {:ok, body_json } -> - IO.puts "\n\e[33m[Showing parsed JSON body]\e[m" - IO.puts body_json - _ -> nil - end + body = extract_body(json) || "" + output = Jason.Formatter.pretty_print(body) + IO.puts("\n\e[33m[Showing parsed JSON body]\e[m") + IO.puts(output) end defp extract_body(json) do json - |> JSX.decode!() + |> Jason.decode!() |> List.first() |> Enum.into(%{}) |> get_in(["responce", "body"]) diff --git a/mix.exs b/mix.exs index 20f9720..eb78abb 100644 --- a/mix.exs +++ b/mix.exs @@ -20,14 +20,14 @@ defmodule ExVCR.Mixfile do end def application do - [applications: [:meck, :exactor, :exjsx], mod: {ExVCR.Application, []}] + [applications: [:meck, :exactor], mod: {ExVCR.Application, []}] end def deps do [ {:meck, "~> 0.8"}, {:exactor, "~> 2.2"}, - {:exjsx, "~> 4.0"}, + {:jason, "~> 1.0"}, {:ibrowse, "4.4.0", optional: true}, {:httpotion, "~> 3.1", optional: true}, {:httpoison, "~> 1.0 or ~> 2.0", optional: true}, diff --git a/mix.lock b/mix.lock index 5ac62d1..87970c9 100644 --- a/mix.lock +++ b/mix.lock @@ -3,12 +3,10 @@ "certifi": {:hex, :certifi, "2.8.0", "d4fb0a6bb20b7c9c3643e22507e42f356ac090a1dcea9ab99e27e0376d695eba", [:rebar3], [], "hexpm", "6ac7efc1c6f8600b08d625292d4bbf584e14847ce1b6b5c44d983d273e1097ea"}, "cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [:rebar3], [{:cowlib, "~> 1.0.2", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "f4763bbe08233eceed6f24bc4fcc8d71c17cfeafa6439157c57349aa1bb4f17c"}, "cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], [], "hexpm", "db622da03aa039e6366ab953e31186cc8190d32905e33788a1acb22744e6abd2"}, - "earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm", "8cf8a291ebf1c7b9539e3cddb19e9cef066c2441b1640f13c34c1d3cfc825fec"}, "earmark_parser": {:hex, :earmark_parser, "1.4.19", "de0d033d5ff9fc396a24eadc2fcf2afa3d120841eb3f1004d138cbf9273210e8", [:mix], [], "hexpm", "527ab6630b5c75c3a3960b75844c314ec305c76d9899bb30f71cb85952a9dc45"}, "ex_doc": {:hex, :ex_doc, "0.28.0", "7eaf526dd8c80ae8c04d52ac8801594426ae322b52a6156cd038f30bafa8226f", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "e55cdadf69a5d1f4cfd8477122ebac5e1fadd433a8c1022dafc5025e48db0131"}, "exactor": {:hex, :exactor, "2.2.4", "5efb4ddeb2c48d9a1d7c9b465a6fffdd82300eb9618ece5d34c3334d5d7245b1", [:mix], [], "hexpm", "1222419f706e01bfa1095aec9acf6421367dcfab798a6f67c54cf784733cd6b5"}, "excoveralls": {:hex, :excoveralls, "0.14.4", "295498f1ae47bdc6dce59af9a585c381e1aefc63298d48172efaaa90c3d251db", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "e3ab02f2df4c1c7a519728a6f0a747e71d7d6e846020aae338173619217931c1"}, - "exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm", "32e95820a97cffea67830e91514a2ad53b888850442d6d395f53a1ac60c82e07"}, "finch": {:hex, :finch, "0.16.0", "40733f02c89f94a112518071c0a91fe86069560f5dbdb39f9150042f44dcfb1a", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6 or ~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f660174c4d519e5fec629016054d60edd822cdfe2b7270836739ac2f97735ec5"}, "hackney": {:hex, :hackney, "1.18.0", "c4443d960bb9fba6d01161d01cd81173089686717d9490e5d3606644c48d121f", [:rebar3], [{:certifi, "~>2.8.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "9afcda620704d720db8c6a3123e9848d09c87586dc1c10479c42627b905b5c5e"}, "hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"}, @@ -17,8 +15,7 @@ "httpotion": {:hex, :httpotion, "3.1.0", "14d20d9b0ce4e86e253eb91e4af79e469ad949f57a5d23c0a51b2f86559f6589", [:mix], [{:ibrowse, "~> 4.4", [hex: :ibrowse, repo: "hexpm", optional: false]}], "hexpm", "2e1f3da5398258f67be9522793c2ccef157d3c9f7a4f69ec8e87184393efe9e0"}, "ibrowse": {:hex, :ibrowse, "4.4.0", "2d923325efe0d2cb09b9c6a047b2835a5eda69d8a47ed6ff8bc03628b764e991", [:rebar3], [], "hexpm", "6a8e5988872086f0506bef68311493551ac5beae7c06ba2a00d5e9f97a60f1c2"}, "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, - "jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"}, - "jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], [], "hexpm", "fc3499fed7a726995aa659143a248534adc754ebd16ccd437cd93b649a95091f"}, + "jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"}, "makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"}, "makeup_elixir": {:hex, :makeup_elixir, "0.15.2", "dc72dfe17eb240552857465cc00cce390960d9a0c055c4ccd38b70629227e97c", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "fd23ae48d09b32eff49d4ced2b43c9f086d402ee4fd4fcb2d7fad97fa8823e75"}, "makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},