|
1 | 1 | package remotelogger |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "fmt" |
5 | 6 | "net/http" |
6 | 7 | "net/http/httptest" |
@@ -357,3 +358,67 @@ func TestLogLevelChangeToFatal_NoExit(t *testing.T) { |
357 | 358 | // Verify the log contains a warning about the level change |
358 | 359 | assert.Contains(t, log, "LOG_LEVEL updated from INFO to FATAL") |
359 | 360 | } |
| 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