Skip to content

Commit

Permalink
Use Jason Instead of JSX
Browse files Browse the repository at this point in the history
Given that the broader Elixir community is circling their wagons around
Jason, and issue parroty#153, it seems that moving to Jason would be a wise
choice.
  • Loading branch information
jherdman committed Sep 6, 2021
1 parent ae8e8f7 commit 0f2d587
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 20 deletions.
7 changes: 4 additions & 3 deletions lib/exvcr/iex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/exvcr/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
36 changes: 36 additions & 0 deletions lib/exvcr/records.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 12 additions & 7 deletions lib/exvcr/task/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"])
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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", optional: true},
Expand Down
2 changes: 0 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"ex_doc": {:hex, :ex_doc, "0.21.3", "857ec876b35a587c5d9148a2512e952e24c24345552259464b98bfbb883c7b42", [:mix], [{:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "0db1ee8d1547ab4877c5b5dffc6604ef9454e189928d5ba8967d4a58a801f161"},
"exactor": {:hex, :exactor, "2.2.4", "5efb4ddeb2c48d9a1d7c9b465a6fffdd82300eb9618ece5d34c3334d5d7245b1", [:mix], [], "hexpm", "1222419f706e01bfa1095aec9acf6421367dcfab798a6f67c54cf784733cd6b5"},
"excoveralls": {:hex, :excoveralls, "0.14.2", "f9f5fd0004d7bbeaa28ea9606251bb643c313c3d60710bad1f5809c845b748f0", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "ca6fd358621cb4d29311b29d4732c4d47dac70e622850979bc54ed9a3e50f3e1"},
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm", "32e95820a97cffea67830e91514a2ad53b888850442d6d395f53a1ac60c82e07"},
"finch": {:hex, :finch, "0.8.0", "9fe1b7b1613f4f0f43ac4be94462a0f3eb13264e5e9a624005da5670e452a1d1", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "07650fd74da3fb51984b9f62bf4db3ef720f3c06d914d716cceeb13a2bc2542d"},
"hackney": {:hex, :hackney, "1.17.4", "99da4674592504d3fb0cfef0db84c3ba02b4508bae2dff8c0108baa0d6e0977c", [:rebar3], [{:certifi, "~>2.6.1", [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", "de16ff4996556c8548d512f4dbe22dd58a587bf3332e7fd362430a7ef3986b16"},
"http_server": {:git, "https://github.com/parroty/http_server.git", "922d10420836a51289ed04f0bb5022bf695da1ab", []},
Expand All @@ -16,7 +15,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.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "a10c6eb62cca416019663129699769f0c2ccf39428b3bb3c0cb38c718a0c186d"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"},
"meck": {:hex, :meck, "0.9.2", "85ccbab053f1db86c7ca240e9fc718170ee5bda03810a6292b5306bf31bae5f5", [:rebar3], [], "hexpm", "81344f561357dc40a8344afa53767c32669153355b626ea9fcbc8da6b3045826"},
Expand Down
10 changes: 10 additions & 0 deletions test/recorder_hackney_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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

Expand Down

0 comments on commit 0f2d587

Please sign in to comment.