forked from valyala/quicktemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonstring_test.go
45 lines (39 loc) · 1.23 KB
/
jsonstring_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package quicktemplate
import (
"encoding/json"
"strings"
"testing"
)
func TestWriteJSONString(t *testing.T) {
testWriteJSONString(t, ``)
testWriteJSONString(t, `f`)
testWriteJSONString(t, `"`)
testWriteJSONString(t, `<`)
testWriteJSONString(t, "\x00\n\r\t\b\f"+`"\`)
testWriteJSONString(t, `"foobar`)
testWriteJSONString(t, `foobar"`)
testWriteJSONString(t, `foo "bar"
baz`)
testWriteJSONString(t, `this is a "тест"`)
testWriteJSONString(t, `привет test ыва`)
testWriteJSONString(t, `</script><script>alert('evil')</script>`)
}
func testWriteJSONString(t *testing.T, s string) {
expectedResult, err := json.Marshal(s)
if err != nil {
t.Fatalf("unexpected error when encoding string %q: %s", s, err)
}
expectedResult = expectedResult[1 : len(expectedResult)-1]
bb := AcquireByteBuffer()
writeJSONString(bb, s)
result := string(bb.B)
ReleaseByteBuffer(bb)
if strings.Contains(result, "'") {
t.Fatalf("json string shouldn't contain single quote: %q, src %q", result, s)
}
result = strings.Replace(result, `\u0027`, "'", -1)
result = strings.Replace(result, ">", `\u003e`, -1)
if result != string(expectedResult) {
t.Fatalf("unexpected result %q. Expecting %q. original string %q", result, expectedResult, s)
}
}