Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/req_api_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ defmodule AntlUtilsElixir.ReqApiLogger do
defp format_request_body(%Request{} = req, hide_list) do
case Request.get_header(req, "content-type") do
["application/json" <> _] ->
req.body
|> Jason.decode!()
|> hide(hide_list)
with {:ok, decoded} <- Jason.decode("#{req.body}") do
hide(decoded, hide_list)
else
_ -> req.body
end

["application/x-www-form-urlencoded"] ->
req.body
Expand Down
28 changes: 28 additions & 0 deletions test/req_api_logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,32 @@ defmodule AntlUtilsElixir.ReqApiLoggerTest do
Logger.configure(level: :debug)
end
end

defp req_get_with_json_header(body) do
Req.new(url: url(), headers: [{"content-type", "application/json"}], body: body)
|> ReqApiLogger.attach(api_name: :test)
|> Req.get()
end

describe "ReqApiLogger regression tests : don't crash on invalid JSON request" do
setup do
TestServer.add("/")
:ok
end

test "nil body" do
assert capture_log(fn -> req_get_with_json_header(nil) end) =~
~r/body=nil/
end

test "empty body" do
assert capture_log(fn -> req_get_with_json_header("") end) =~
~r/body=""/
end

test "malformed json" do
assert capture_log(fn -> req_get_with_json_header("{") end) =~
~r/body="{"/
end
end
end