MCPcopy
hub / github.com/uber-go/zap / TestJSONEscaping

Function TestJSONEscaping

zapcore/json_encoder_impl_test.go:60–110  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

58}
59
60func TestJSONEscaping(t *testing.T) {
61 enc := &jsonEncoder{buf: bufferpool.Get()}
62 // Test all the edge cases of JSON escaping directly.
63 cases := map[string]string{
64 // ASCII.
65 `foo`: `foo`,
66 // Special-cased characters.
67 `"`: `\"`,
68 `\`: `\\`,
69 // Special-cased characters within everyday ASCII.
70 `foo"foo`: `foo\"foo`,
71 "foo\n": `foo\n`,
72 // Special-cased control characters.
73 "\n": `\n`,
74 "\r": `\r`,
75 "\t": `\t`,
76 // \b and \f are sometimes backslash-escaped, but this representation is also
77 // conformant.
78 "\b": `\u0008`,
79 "\f": `\u000c`,
80 // The standard lib special-cases angle brackets and ampersands by default,
81 // because it wants to protect users from browser exploits. In a logging
82 // context, we shouldn't special-case these characters.
83 "<": "<",
84 ">": ">",
85 "&": "&",
86 // ASCII bell - not special-cased.
87 string(byte(0x07)): `\u0007`,
88 // Astral-plane unicode.
89 `☃`: `☃`,
90 // Decodes to (RuneError, 1)
91 "\xed\xa0\x80": `\ufffd\ufffd\ufffd`,
92 "foo\xed\xa0\x80": `foo\ufffd\ufffd\ufffd`,
93 }
94
95 t.Run("String", func(t *testing.T) {
96 for input, output := range cases {
97 enc.truncate()
98 enc.safeAddString(input)
99 assertJSON(t, output, enc)
100 }
101 })
102
103 t.Run("ByteString", func(t *testing.T) {
104 for input, output := range cases {
105 enc.truncate()
106 enc.safeAddByteString([]byte(input))
107 assertJSON(t, output, enc)
108 }
109 })
110}
111
112func TestJSONEncoderObjectFields(t *testing.T) {
113 tests := []struct {

Callers

nothing calls this directly

Calls 5

truncateMethod · 0.95
safeAddStringMethod · 0.95
safeAddByteStringMethod · 0.95
assertJSONFunction · 0.85
GetMethod · 0.45

Tested by

no test coverage detected