MCPcopy
hub / github.com/grafana/dskit / TestEndToEnd_json

Function TestEndToEnd_json

log/sanitize_test.go:362–420  ·  view source on GitHub ↗

TestEndToEnd_json verifies the final JSON output produced by go-kit when logging values wrapped with DropUnsafeChars or EscapeUnsafeChars.

(t *testing.T)

Source from the content-addressed store, hash-verified

360// when logging values wrapped with DropUnsafeChars or
361// EscapeUnsafeChars.
362func 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 }

Callers

nothing calls this directly

Calls 5

DropUnsafeCharsFunction · 0.85
EscapeUnsafeCharsFunction · 0.85
RunMethod · 0.80
StringMethod · 0.65
LogMethod · 0.45

Tested by

no test coverage detected