Skip to content

Commit

Permalink
Test to verify that metadata for Tesla.Middleware is kept for each sl…
Browse files Browse the repository at this point in the history
…ice of the response.
  • Loading branch information
Ivor committed Sep 27, 2024
1 parent 24377d1 commit 6fbdeff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import Config

config :logger, :default_formatter, metadata: ~w(bar baz foo)a
config :logger, :default_formatter, metadata: ~w(bar baz foo request_id)a
21 changes: 20 additions & 1 deletion test/tesla/middleware/meta_logger_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule Tesla.Middleware.MetaLoggerTest do
use ExUnit.Case, async: true
use ExUnit.Case, async: false

require Logger

import ExUnit.CaptureLog

Expand Down Expand Up @@ -227,6 +229,23 @@ defmodule Tesla.Middleware.MetaLoggerTest do
assert logs =~ "[debug] [#{inspect(Subject)}] #{String.duplicate("b", 100)}\n"
end

test "when the max entry length is given and the logs are split, the metadata is captured with each line" do
Logger.metadata(request_id: "123123123")

body = String.duplicate("x", 100)

log_lines =
capture_log(fn ->
FakeClient.post("/huge-response", body, opts: [max_entry_length: 10])
end)
|> String.split("\n\n")

log_lines
|> Enum.each(fn log_line ->
assert log_line =~ "request_id=123123123"
end)
end

test "when response is an error, logs the response with error log level" do
logs = capture_log(fn -> FakeClient.get("/error") end)

Expand Down

0 comments on commit 6fbdeff

Please sign in to comment.