@@ -205,6 +205,99 @@ func TestRequestLogger_HTTPRouteParams(t *testing.T) {
205
205
}
206
206
}
207
207
208
+ func TestRequestLogger_RouteParamsLogging (t * testing.T ) {
209
+ t .Parallel ()
210
+
211
+ tests := []struct {
212
+ name string
213
+ params map [string ]string
214
+ expectedFields []string
215
+ }{
216
+ {
217
+ name : "EmptyParams" ,
218
+ params : map [string ]string {},
219
+ expectedFields : []string {},
220
+ },
221
+ {
222
+ name : "SingleParam" ,
223
+ params : map [string ]string {
224
+ "workspace" : "test-workspace" ,
225
+ },
226
+ expectedFields : []string {"workspace" },
227
+ },
228
+ {
229
+ name : "MultipleParams" ,
230
+ params : map [string ]string {
231
+ "workspace" : "test-workspace" ,
232
+ "agent" : "test-agent" ,
233
+ "user" : "test-user" ,
234
+ },
235
+ expectedFields : []string {"workspace" , "agent" , "user" },
236
+ },
237
+ {
238
+ name : "EmptyValueParam" ,
239
+ params : map [string ]string {
240
+ "workspace" : "test-workspace" ,
241
+ "agent" : "" ,
242
+ },
243
+ expectedFields : []string {"workspace" },
244
+ },
245
+ }
246
+
247
+ for _ , tt := range tests {
248
+ tt := tt
249
+ t .Run (tt .name , func (t * testing.T ) {
250
+ t .Parallel ()
251
+
252
+ sink := & fakeSink {}
253
+ logger := slog .Make (sink )
254
+ logger = logger .Leveled (slog .LevelDebug )
255
+
256
+ // Create a route context with the test parameters
257
+ chiCtx := chi .NewRouteContext ()
258
+ for key , value := range tt .params {
259
+ chiCtx .URLParams .Add (key , value )
260
+ }
261
+
262
+ ctx := context .WithValue (context .Background (), chi .RouteCtxKey , chiCtx )
263
+ logCtx := NewRequestLogger (logger , "GET" , time .Now ())
264
+
265
+ // Write the log
266
+ logCtx .WriteLog (ctx , http .StatusOK )
267
+
268
+ require .Len (t , sink .entries , 1 , "expected exactly one log entry" )
269
+
270
+ // Convert fields to map for easier checking
271
+ fieldsMap := make (map [string ]any )
272
+ for _ , field := range sink .entries [0 ].Fields {
273
+ fieldsMap [field .Name ] = field .Value
274
+ }
275
+
276
+ // Verify expected fields are present
277
+ for _ , field := range tt .expectedFields {
278
+ value , exists := fieldsMap [field ]
279
+ require .True (t , exists , "field %q should be present in log" , field )
280
+ require .Equal (t , tt .params [field ], value , "field %q has incorrect value" , field )
281
+ }
282
+
283
+ // Verify no unexpected fields are present
284
+ for field := range fieldsMap {
285
+ if field == "took" || field == "status_code" || field == "latency_ms" {
286
+ continue // Skip standard fields
287
+ }
288
+ found := false
289
+ for _ , expected := range tt .expectedFields {
290
+ if field == expected {
291
+ found = true
292
+ break
293
+ }
294
+ }
295
+ require .True (t , found , "unexpected field %q in log" , field )
296
+ }
297
+ })
298
+ }
299
+ }
300
+
208
301
type fakeSink struct {
209
302
entries []slog.SinkEntry
210
303
newEntries chan slog.SinkEntry
0 commit comments