TestEndToEnd_json verifies the final JSON output produced by go-kit when logging values wrapped with DropUnsafeChars or EscapeUnsafeChars.
(t *testing.T)
| 360 | // when logging values wrapped with DropUnsafeChars or |
| 361 | // EscapeUnsafeChars. |
| 362 | func TestEndToEnd_json(t *testing.T) { |
| 363 | testCases := map[string]struct { |
| 364 | value any |
| 365 | wantOnWire map[string]string |
| 366 | }{ |
| 367 | "clean string passes through both wrappers unchanged": { |
| 368 | value: "Mozilla/5.0", |
| 369 | wantOnWire: map[string]string{ |
| 370 | "drop": `{"user_agent":"Mozilla/5.0"}`, |
| 371 | "escape": `{"user_agent":"Mozilla/5.0"}`, |
| 372 | }, |
| 373 | }, |
| 374 | "embedded newline is sanitized before reaching the encoder": { |
| 375 | value: "Mozilla\nFAKE", |
| 376 | wantOnWire: map[string]string{ |
| 377 | "drop": `{"user_agent":"MozillaFAKE"}`, |
| 378 | "escape": `{"user_agent":"Mozilla\\x0aFAKE"}`, |
| 379 | }, |
| 380 | }, |
| 381 | "custom Stringer rendering user-controlled text is sanitized": { |
| 382 | value: testUnsafeUserInputStringer{"path=/foo\nFAKE"}, |
| 383 | wantOnWire: map[string]string{ |
| 384 | "drop": `{"user_agent":"path=/fooFAKE"}`, |
| 385 | "escape": `{"user_agent":"path=/foo\\x0aFAKE"}`, |
| 386 | }, |
| 387 | }, |
| 388 | "typed-nil error renders as JSON null, matching go-kit raw output": { |
| 389 | value: (*testNilSafeError)(nil), |
| 390 | wantOnWire: map[string]string{ |
| 391 | "drop": `{"user_agent":null}`, |
| 392 | "escape": `{"user_agent":null}`, |
| 393 | }, |
| 394 | }, |
| 395 | "nil any renders as JSON null, matching go-kit raw output": { |
| 396 | value: nil, |
| 397 | wantOnWire: map[string]string{ |
| 398 | "drop": `{"user_agent":null}`, |
| 399 | "escape": `{"user_agent":null}`, |
| 400 | }, |
| 401 | }, |
| 402 | } |
| 403 | |
| 404 | for name, tc := range testCases { |
| 405 | t.Run(name, func(t *testing.T) { |
| 406 | t.Run("drop", func(t *testing.T) { |
| 407 | buf := &bytes.Buffer{} |
| 408 | logger := log.NewJSONLogger(buf) |
| 409 | require.NoError(t, logger.Log("user_agent", DropUnsafeChars(tc.value))) |
| 410 | require.JSONEq(t, tc.wantOnWire["drop"], buf.String()) |
| 411 | }) |
| 412 | t.Run("escape", func(t *testing.T) { |
| 413 | buf := &bytes.Buffer{} |
| 414 | logger := log.NewJSONLogger(buf) |
| 415 | require.NoError(t, logger.Log("user_agent", EscapeUnsafeChars(tc.value))) |
| 416 | require.JSONEq(t, tc.wantOnWire["escape"], buf.String()) |
| 417 | }) |
| 418 | }) |
| 419 | } |
nothing calls this directly
no test coverage detected