From 6fbdefff40f247ff09b881877e6c15ef4240e0f3 Mon Sep 17 00:00:00 2001 From: Ivor Paul Date: Sat, 28 Sep 2024 00:13:09 +0200 Subject: [PATCH] Test to verify that metadata for Tesla.Middleware is kept for each slice of the response. --- config/config.exs | 2 +- test/tesla/middleware/meta_logger_test.exs | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/config/config.exs b/config/config.exs index 68a81ab..91e6b3b 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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 diff --git a/test/tesla/middleware/meta_logger_test.exs b/test/tesla/middleware/meta_logger_test.exs index 8db95d5..884087a 100644 --- a/test/tesla/middleware/meta_logger_test.exs +++ b/test/tesla/middleware/meta_logger_test.exs @@ -1,5 +1,7 @@ defmodule Tesla.Middleware.MetaLoggerTest do - use ExUnit.Case, async: true + use ExUnit.Case, async: false + + require Logger import ExUnit.CaptureLog @@ -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)