Skip to content

Commit b9e2b61

Browse files
committed
added checking the logger fields in the test
1 parent 81e1527 commit b9e2b61

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

coderd/httpmw/logger_internal_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func TestLoggerMiddleware_SingleRequest(t *testing.T) {
5252
logger := slog.Make(sink)
5353
logger = logger.Leveled(slog.LevelDebug)
5454

55+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
56+
defer cancel()
57+
5558
// Create a test handler to simulate an HTTP request
5659
testHandler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
5760
rw.WriteHeader(http.StatusOK)
@@ -63,7 +66,7 @@ func TestLoggerMiddleware_SingleRequest(t *testing.T) {
6366
wrappedHandler := loggerMiddleware(testHandler)
6467

6568
// Create a test HTTP request
66-
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/test-path", nil)
69+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/test-path", nil)
6770
if err != nil {
6871
t.Fatalf("failed to create request: %v", err)
6972
}
@@ -76,6 +79,24 @@ func TestLoggerMiddleware_SingleRequest(t *testing.T) {
7679
require.Len(t, sink.entries, 1, "log was written twice")
7780

7881
require.Equal(t, sink.entries[0].Message, "GET", "log message should be GET")
82+
83+
fieldsMap := make(map[string]interface{})
84+
for _, field := range sink.entries[0].Fields {
85+
fieldsMap[field.Name] = field.Value
86+
}
87+
88+
// Check that the log contains the expected fields
89+
requiredFields := []string{"host", "path", "proto", "remote_addr", "start", "took", "status_code", "latency_ms"}
90+
for _, field := range requiredFields {
91+
_, exists := fieldsMap[field]
92+
require.True(t, exists, "field %q is missing in log fields", field)
93+
}
94+
95+
require.Len(t, sink.entries[0].Fields, len(requiredFields), "log should contain only the required fields")
96+
97+
// Check value of the status code
98+
require.Equal(t, fieldsMap["status_code"], http.StatusOK, "status_code should be 200")
99+
79100
}
80101

81102
func TestLoggerMiddleware_WebSocket(t *testing.T) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy