Skip to content

Commit 784754f

Browse files
committed
Add more comprehensive test case for Plug with DataDog formatter
1 parent 9cde3aa commit 784754f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

test/logger_json/plug_test.exs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,48 @@ defmodule LoggerJSON.PlugTest do
183183
} = decode_or_print_error(log)
184184
end
185185

186+
test "logs requests" do
187+
conn =
188+
Plug.Test.conn(:get, "/foo/bar?baz=qux#frag")
189+
|> Plug.Conn.put_req_header("user-agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)")
190+
|> Plug.Conn.put_req_header("referer", "http://www.example.com/")
191+
|> Plug.Conn.put_status(200)
192+
193+
log =
194+
capture_log(fn ->
195+
Logger.metadata(request_id: "123")
196+
197+
telemetry_logging_handler(
198+
[:phoenix, :endpoint, :stop],
199+
%{duration: 500_000},
200+
%{conn: conn},
201+
:info
202+
)
203+
204+
Logger.flush()
205+
end)
206+
207+
assert %{
208+
"http" => %{
209+
"method" => "GET",
210+
"referer" => "http://www.example.com/",
211+
"request_id" => "123",
212+
"status_code" => 200,
213+
"url" => "http://www.example.com/foo/bar?baz=qux",
214+
"url_details" => %{
215+
"host" => "www.example.com",
216+
"path" => "/foo/bar",
217+
"port" => 80,
218+
"queryString" => "baz=qux",
219+
"scheme" => "http"
220+
},
221+
"useragent" => "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
222+
},
223+
"message" => "GET /foo/bar [Sent 200 in 500µs]",
224+
"network" => %{"client" => %{"ip" => "127.0.0.1"}}
225+
} = decode_or_print_error(log)
226+
end
227+
186228
test "logs unsent connections" do
187229
conn = Plug.Test.conn(:get, "/")
188230

0 commit comments

Comments
 (0)