Skip to content

Commit 074cc71

Browse files
committed
add test for Prettyprint method
1 parent 993703c commit 074cc71

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

pkg/gofr/logging/remotelogger/dynamic_level_logger_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package remotelogger
22

33
import (
4+
"bytes"
45
"fmt"
56
"net/http"
67
"net/http/httptest"
@@ -357,3 +358,67 @@ func TestLogLevelChangeToFatal_NoExit(t *testing.T) {
357358
// Verify the log contains a warning about the level change
358359
assert.Contains(t, log, "LOG_LEVEL updated from INFO to FATAL")
359360
}
361+
362+
func TestHTTPDebugMsg_PrettyPrint(t *testing.T) {
363+
cases := []struct {
364+
name string
365+
msg httpDebugMsg
366+
wantColorSeq string
367+
}{
368+
{
369+
name: "2xx uses blue",
370+
msg: httpDebugMsg{
371+
CorrelationID: "corr-200",
372+
ResponseCode: 200,
373+
ResponseTime: 123,
374+
HTTPMethod: "GET",
375+
URI: "/ok",
376+
},
377+
wantColorSeq: fmt.Sprintf("\u001B[38;5;%dm", colorBlue),
378+
},
379+
{
380+
name: "4xx uses yellow",
381+
msg: httpDebugMsg{
382+
CorrelationID: "corr-404",
383+
ResponseCode: 404,
384+
ResponseTime: 456,
385+
HTTPMethod: "POST",
386+
URI: "/not-found",
387+
},
388+
wantColorSeq: fmt.Sprintf("\u001B[38;5;%dm", colorYellow),
389+
},
390+
{
391+
name: "5xx uses red",
392+
msg: httpDebugMsg{
393+
CorrelationID: "corr-500",
394+
ResponseCode: 500,
395+
ResponseTime: 789,
396+
HTTPMethod: "PUT",
397+
URI: "/err",
398+
},
399+
wantColorSeq: fmt.Sprintf("\u001B[38;5;%dm", colorRed),
400+
},
401+
}
402+
403+
for _, tc := range cases {
404+
t.Run(tc.name, func(t *testing.T) {
405+
var buf bytes.Buffer
406+
tc.msg.PrettyPrint(&buf)
407+
out := buf.String()
408+
409+
// basic content checks
410+
assert.Contains(t, out, tc.msg.CorrelationID)
411+
assert.Contains(t, out, tc.msg.HTTPMethod)
412+
assert.Contains(t, out, tc.msg.URI)
413+
assert.Contains(t, out, fmt.Sprintf("%d", tc.msg.ResponseCode))
414+
// response time should include the microsecond suffix
415+
assert.Contains(t, out, fmt.Sprintf("%dμs", tc.msg.ResponseTime))
416+
417+
// color sequence must be present
418+
assert.Contains(t, out, tc.wantColorSeq, "expected color sequence %q in output: %q", tc.wantColorSeq, out)
419+
420+
// ensure reset code present
421+
assert.Contains(t, out, "\u001B[0m")
422+
})
423+
}
424+
}

0 commit comments

Comments
 (0)