From 065ac71cb20be0a52d93c539e69f4eb1eb3f52ce Mon Sep 17 00:00:00 2001 From: James Herdman Date: Fri, 23 Jun 2023 21:18:35 +0800 Subject: [PATCH 1/3] Use Jason Instead of JSX Given that the broader Elixir community is circling their wagons around Jason, and issue #153, it seems that moving to Jason would be a wise choice. --- lib/exvcr/iex.ex | 7 ++++--- lib/exvcr/json.ex | 12 ++++++------ lib/exvcr/records.ex | 36 ++++++++++++++++++++++++++++++++++ lib/exvcr/task/show.ex | 19 +++++++++++------- mix.exs | 4 ++-- mix.lock | 3 --- test/recorder_hackney_test.exs | 10 ++++++++++ 7 files changed, 70 insertions(+), 21 deletions(-) diff --git a/lib/exvcr/iex.ex b/lib/exvcr/iex.ex index 4ca19fe..ddd5d17 100644 --- a/lib/exvcr/iex.ex +++ b/lib/exvcr/iex.ex @@ -25,9 +25,10 @@ defmodule ExVCR.IEx do unquote(test) after ExVCR.MockLock.release_lock() - Recorder.get(recorder) - |> JSX.encode! - |> JSX.prettify! + + recorder + |> Recorder.get() + |> Jason.encode_to_iodata!(pretty: true) |> IO.puts end :ok diff --git a/lib/exvcr/json.ex b/lib/exvcr/json.ex index fd7e63e..f32a35b 100644 --- a/lib/exvcr/json.ex +++ b/lib/exvcr/json.ex @@ -7,11 +7,11 @@ defmodule ExVCR.JSON do Save responses into the json file. """ def save(file_name, recordings) do - json = recordings - |> Enum.map(&encode_binary_data/1) - |> Enum.reverse() - |> JSX.encode!() - |> JSX.prettify!() + json = + recordings + |> Enum.map(&encode_binary_data/1) + |> Enum.reverse() + |> 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 +48,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..1720e3a 100644 --- a/lib/exvcr/records.ex +++ b/lib/exvcr/records.ex @@ -2,17 +2,53 @@ defmodule ExVCR.Record do defstruct options: nil, responses: nil end +defmodule ExVCR.Utils do + @moduledoc false + + def keyword_to_map(kw) do + kw + |> Enum.sort(fn {k1, _}, {k2, _} -> k1 <= k2 end) + |> Enum.reduce(%{}, fn {k, v}, acc -> Map.put(acc, k, v) end) + end +end + defmodule ExVCR.Request do defstruct url: nil, headers: [], method: nil, body: nil, options: [], request_body: "" + + defimpl Jason.Encoder, for: ExVCR.Request do + def encode(value, opts) do + %{headers: headers, options: options} = Map.take(value, [:headers, :options]) + + map = value + |> Map.take([:url, :method, :body, :request_body]) + |> Map.put(:headers, ExVCR.Utils.keyword_to_map(headers)) + |> Map.put(:options, ExVCR.Utils.keyword_to_map(options)) + + Jason.Encode.map(map, opts) + end + end end defmodule ExVCR.Response do defstruct type: "ok", status_code: nil, headers: [], body: nil, binary: false + + defimpl Jason.Encoder, for: ExVCR.Response do + def encode(value, opts) do + headers = Map.get(value, :headers) + + map = value + |> Map.take([:type, :status_code, :body, :binary]) + |> Map.put(:headers, ExVCR.Utils.keyword_to_map(headers)) + + Jason.Encode.map(map, opts) + end + end end defmodule ExVCR.Checker.Results do defstruct dirs: nil, files: [] end + defmodule ExVCR.Checker.Counts do defstruct server: 0, cache: 0 end diff --git a/lib/exvcr/task/show.ex b/lib/exvcr/task/show.ex index 4bba7c7..8755b1a 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,22 @@ 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 + try do + output = json + |> extract_body() + |> Jason.Formatter.pretty_print() + + IO.puts "\n\e[33m[Showing parsed JSON body]\e[m" + IO.puts output + rescue + _ -> + nil end 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..9cbfd86 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"}, @@ -18,7 +16,6 @@ "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"}, "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"}, diff --git a/test/recorder_hackney_test.exs b/test/recorder_hackney_test.exs index 1ab110d..9e2e0b8 100644 --- a/test/recorder_hackney_test.exs +++ b/test/recorder_hackney_test.exs @@ -154,6 +154,7 @@ defmodule ExVCR.RecorderHackneyTest do ExVCR.Config.response_headers_blacklist([]) end + @tag :wip test "hackney request with ssl options" do use_cassette "record_hackney_with_ssl_options" do host = @url |> URI.parse() |> Map.get(:host) |> to_charlist() @@ -163,6 +164,15 @@ defmodule ExVCR.RecorderHackneyTest do end end + test "HTTPoison with ssl options" do + use_cassette "record_hackney_with_ssl_options" do + response = + HTTPoison.post!("https://example.com", {:form, []}, [], ssl: [{:versions, [:"tlsv1.2"]}]) + + assert response.status_code == 200 + end + end + for option <- [:with_body, {:with_body, true}] do @option option From b1085a67fdb163fc1bfe16093f4360ce2c8a9b85 Mon Sep 17 00:00:00 2001 From: ruslandoga <67764432+ruslandoga@users.noreply.github.com> Date: Fri, 23 Jun 2023 21:19:23 +0800 Subject: [PATCH 2/3] make tests pass --- lib/exvcr/json.ex | 44 +++++++++++++++-------- lib/exvcr/records.ex | 66 ++++++++++++++++++++-------------- lib/exvcr/task/show.ex | 24 +++++-------- mix.lock | 2 +- test/recorder_hackney_test.exs | 20 +++-------- 5 files changed, 83 insertions(+), 73 deletions(-) diff --git a/lib/exvcr/json.ex b/lib/exvcr/json.ex index f32a35b..409c6cb 100644 --- a/lib/exvcr/json.ex +++ b/lib/exvcr/json.ex @@ -17,16 +17,21 @@ defmodule ExVCR.JSON do File.write!(file_name, json) end - defp encode_binary_data(%{request: _, response: %ExVCR.Response{body: nil}} = recording), do: recording + defp encode_binary_data(%{request: _, response: %ExVCR.Response{body: nil}} = recording), + do: recording defp encode_binary_data(%{response: response} = recording) do case String.valid?(response.body) do - true -> recording + true -> + recording + false -> - body = response.body - |> :erlang.term_to_binary() - |> Base.encode64() - %{ recording | response: %{ response | body: body, binary: true } } + body = + response.body + |> :erlang.term_to_binary() + |> Base.encode64() + + %{recording | response: %{response | body: body, binary: true}} end end @@ -35,10 +40,15 @@ defmodule ExVCR.JSON do For options, this method just refers to the :custom attribute is set or not. """ def load(file_name, custom_mode, adapter) do - case { File.exists?(file_name), custom_mode } do - { true, _ } -> read_json_file(file_name) |> Enum.map(&adapter.convert_from_string/1) - { false, true } -> raise ExVCR.FileNotFoundError, message: "cassette file \"#{file_name}\" not found" - { false, _ } -> [] + case {File.exists?(file_name), custom_mode} do + {true, _} -> + read_json_file(file_name) |> Enum.map(&adapter.convert_from_string/1) + + {false, true} -> + raise ExVCR.FileNotFoundError, message: "cassette file \"#{file_name}\" not found" + + {false, _} -> + [] end end @@ -52,11 +62,15 @@ defmodule ExVCR.JSON do |> Enum.map(&load_binary_data/1) end - defp load_binary_data(%{"response" => %{"body" => body, "binary" => true} = response} = recording) do - body = body - |> Base.decode64!() - |> :erlang.binary_to_term() - %{ recording | "response" => %{ response | "body" => body } } + defp load_binary_data( + %{"response" => %{"body" => body, "binary" => true} = response} = recording + ) do + body = + body + |> Base.decode64!() + |> :erlang.binary_to_term() + + %{recording | "response" => %{response | "body" => body}} end defp load_binary_data(recording), do: recording diff --git a/lib/exvcr/records.ex b/lib/exvcr/records.ex index 1720e3a..741eb07 100644 --- a/lib/exvcr/records.ex +++ b/lib/exvcr/records.ex @@ -2,45 +2,57 @@ defmodule ExVCR.Record do defstruct options: nil, responses: nil end -defmodule ExVCR.Utils do - @moduledoc false - - def keyword_to_map(kw) do - kw - |> Enum.sort(fn {k1, _}, {k2, _} -> k1 <= k2 end) - |> Enum.reduce(%{}, fn {k, v}, acc -> Map.put(acc, k, v) end) - end -end - defmodule ExVCR.Request do defstruct url: nil, headers: [], method: nil, body: nil, options: [], request_body: "" - defimpl Jason.Encoder, for: ExVCR.Request do - def encode(value, opts) do - %{headers: headers, options: options} = Map.take(value, [:headers, :options]) + 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 - map = value - |> Map.take([:url, :method, :body, :request_body]) - |> Map.put(:headers, ExVCR.Utils.keyword_to_map(headers)) - |> Map.put(:options, ExVCR.Utils.keyword_to_map(options)) + defp clean_invalid([] = empty), do: empty - Jason.Encode.map(map, opts) + 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, for: ExVCR.Response do - def encode(value, opts) do - headers = Map.get(value, :headers) - - map = value - |> Map.take([:type, :status_code, :body, :binary]) - |> Map.put(:headers, ExVCR.Utils.keyword_to_map(headers)) - - Jason.Encode.map(map, opts) + 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 diff --git a/lib/exvcr/task/show.ex b/lib/exvcr/task/show.ex index 8755b1a..bf58388 100644 --- a/lib/exvcr/task/show.ex +++ b/lib/exvcr/task/show.ex @@ -13,29 +13,23 @@ defmodule ExVCR.Task.Show do defp print_file(file) do if File.exists?(file) do - IO.puts "\e[32mShowing #{file}\e[m" - IO.puts "\e[32m**************************************\e[m" + IO.puts("\e[32mShowing #{file}\e[m") + IO.puts("\e[32m**************************************\e[m") json = File.read!(file) - IO.puts json |> Jason.Formatter.pretty_print() |> 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" + IO.puts("\e[32m**************************************\e[m") else - IO.puts "Specified file [#{file}] was not found." + IO.puts("Specified file [#{file}] was not found.") end end defp display_parsed_body(json) do - try do - output = json - |> extract_body() - |> Jason.Formatter.pretty_print() + body = extract_body(json) || "" + output = Jason.Formatter.pretty_print(body) - IO.puts "\n\e[33m[Showing parsed JSON body]\e[m" - IO.puts output - rescue - _ -> - nil - end + IO.puts("\n\e[33m[Showing parsed JSON body]\e[m") + IO.puts(output) end defp extract_body(json) do diff --git a/mix.lock b/mix.lock index 9cbfd86..87970c9 100644 --- a/mix.lock +++ b/mix.lock @@ -15,7 +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"}, + "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"}, diff --git a/test/recorder_hackney_test.exs b/test/recorder_hackney_test.exs index 9e2e0b8..d9cd8a9 100644 --- a/test/recorder_hackney_test.exs +++ b/test/recorder_hackney_test.exs @@ -10,11 +10,11 @@ defmodule ExVCR.RecorderHackneyTest do setup_all do File.rm_rf(@dummy_cassette_dir) - on_exit(fn -> - File.rm_rf(@dummy_cassette_dir) - HttpServer.stop(@port) - :ok - end) + # on_exit(fn -> + # File.rm_rf(@dummy_cassette_dir) + # HttpServer.stop(@port) + # :ok + # end) HTTPoison.start() HttpServer.start(path: "/server", port: @port, response: "test_response") @@ -154,7 +154,6 @@ defmodule ExVCR.RecorderHackneyTest do ExVCR.Config.response_headers_blacklist([]) end - @tag :wip test "hackney request with ssl options" do use_cassette "record_hackney_with_ssl_options" do host = @url |> URI.parse() |> Map.get(:host) |> to_charlist() @@ -164,15 +163,6 @@ defmodule ExVCR.RecorderHackneyTest do end end - test "HTTPoison with ssl options" do - use_cassette "record_hackney_with_ssl_options" do - response = - HTTPoison.post!("https://example.com", {:form, []}, [], ssl: [{:versions, [:"tlsv1.2"]}]) - - assert response.status_code == 200 - end - end - for option <- [:with_body, {:with_body, true}] do @option option From 4e4e258610902054042c481a3bec1d57ee5b2a05 Mon Sep 17 00:00:00 2001 From: ruslandoga <67764432+ruslandoga@users.noreply.github.com> Date: Fri, 23 Jun 2023 21:24:09 +0800 Subject: [PATCH 3/3] min changes --- lib/exvcr/iex.ex | 4 +-- lib/exvcr/json.ex | 53 ++++++++++++---------------------- lib/exvcr/records.ex | 1 - lib/exvcr/task/show.ex | 11 ++++--- test/recorder_hackney_test.exs | 10 +++---- 5 files changed, 30 insertions(+), 49 deletions(-) diff --git a/lib/exvcr/iex.ex b/lib/exvcr/iex.ex index ddd5d17..9db83c3 100644 --- a/lib/exvcr/iex.ex +++ b/lib/exvcr/iex.ex @@ -25,9 +25,7 @@ defmodule ExVCR.IEx do unquote(test) after ExVCR.MockLock.release_lock() - - recorder - |> Recorder.get() + Recorder.get(recorder) |> Jason.encode_to_iodata!(pretty: true) |> IO.puts end diff --git a/lib/exvcr/json.ex b/lib/exvcr/json.ex index 409c6cb..138d4e3 100644 --- a/lib/exvcr/json.ex +++ b/lib/exvcr/json.ex @@ -7,31 +7,25 @@ defmodule ExVCR.JSON do Save responses into the json file. """ def save(file_name, recordings) do - json = - recordings - |> Enum.map(&encode_binary_data/1) - |> Enum.reverse() - |> Jason.encode_to_iodata!(pretty: true) + json = recordings + |> Enum.map(&encode_binary_data/1) + |> Enum.reverse() + |> Jason.encode_to_iodata!(pretty: true) unless File.exists?(path = Path.dirname(file_name)), do: File.mkdir_p!(path) File.write!(file_name, json) end - defp encode_binary_data(%{request: _, response: %ExVCR.Response{body: nil}} = recording), - do: recording + defp encode_binary_data(%{request: _, response: %ExVCR.Response{body: nil}} = recording), do: recording defp encode_binary_data(%{response: response} = recording) do case String.valid?(response.body) do - true -> - recording - + true -> recording false -> - body = - response.body - |> :erlang.term_to_binary() - |> Base.encode64() - - %{recording | response: %{response | body: body, binary: true}} + body = response.body + |> :erlang.term_to_binary() + |> Base.encode64() + %{ recording | response: %{ response | body: body, binary: true } } end end @@ -40,15 +34,10 @@ defmodule ExVCR.JSON do For options, this method just refers to the :custom attribute is set or not. """ def load(file_name, custom_mode, adapter) do - case {File.exists?(file_name), custom_mode} do - {true, _} -> - read_json_file(file_name) |> Enum.map(&adapter.convert_from_string/1) - - {false, true} -> - raise ExVCR.FileNotFoundError, message: "cassette file \"#{file_name}\" not found" - - {false, _} -> - [] + case { File.exists?(file_name), custom_mode } do + { true, _ } -> read_json_file(file_name) |> Enum.map(&adapter.convert_from_string/1) + { false, true } -> raise ExVCR.FileNotFoundError, message: "cassette file \"#{file_name}\" not found" + { false, _ } -> [] end end @@ -62,15 +51,11 @@ defmodule ExVCR.JSON do |> Enum.map(&load_binary_data/1) end - defp load_binary_data( - %{"response" => %{"body" => body, "binary" => true} = response} = recording - ) do - body = - body - |> Base.decode64!() - |> :erlang.binary_to_term() - - %{recording | "response" => %{response | "body" => body}} + defp load_binary_data(%{"response" => %{"body" => body, "binary" => true} = response} = recording) do + body = body + |> Base.decode64!() + |> :erlang.binary_to_term() + %{ recording | "response" => %{ response | "body" => body } } end defp load_binary_data(recording), do: recording diff --git a/lib/exvcr/records.ex b/lib/exvcr/records.ex index 741eb07..77ad7d7 100644 --- a/lib/exvcr/records.ex +++ b/lib/exvcr/records.ex @@ -60,7 +60,6 @@ end defmodule ExVCR.Checker.Results do defstruct dirs: nil, files: [] end - defmodule ExVCR.Checker.Counts do defstruct server: 0, cache: 0 end diff --git a/lib/exvcr/task/show.ex b/lib/exvcr/task/show.ex index bf58388..0f14e6a 100644 --- a/lib/exvcr/task/show.ex +++ b/lib/exvcr/task/show.ex @@ -13,21 +13,20 @@ defmodule ExVCR.Task.Show do defp print_file(file) do if File.exists?(file) do - IO.puts("\e[32mShowing #{file}\e[m") - IO.puts("\e[32m**************************************\e[m") + IO.puts "\e[32mShowing #{file}\e[m" + IO.puts "\e[32m**************************************\e[m" json = File.read!(file) - IO.puts(json |> Jason.Formatter.pretty_print() |> 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") + IO.puts "\e[32m**************************************\e[m" else - IO.puts("Specified file [#{file}] was not found.") + IO.puts "Specified file [#{file}] was not found." end end defp display_parsed_body(json) do 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 diff --git a/test/recorder_hackney_test.exs b/test/recorder_hackney_test.exs index d9cd8a9..1ab110d 100644 --- a/test/recorder_hackney_test.exs +++ b/test/recorder_hackney_test.exs @@ -10,11 +10,11 @@ defmodule ExVCR.RecorderHackneyTest do setup_all do File.rm_rf(@dummy_cassette_dir) - # on_exit(fn -> - # File.rm_rf(@dummy_cassette_dir) - # HttpServer.stop(@port) - # :ok - # end) + on_exit(fn -> + File.rm_rf(@dummy_cassette_dir) + HttpServer.stop(@port) + :ok + end) HTTPoison.start() HttpServer.start(path: "/server", port: @port, response: "test_response")