-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
54 lines (40 loc) · 1.4 KB
/
json_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
46
47
48
49
50
51
52
53
54
package x_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/tozd/go/x"
)
func TestUnmarshalWithoutUnknownFields(t *testing.T) {
t.Parallel()
type Test struct {
Field string
}
var v Test
err := x.UnmarshalWithoutUnknownFields([]byte(`{}`), &v)
assert.NoError(t, err, "% -+#.1v", err) //nolint:testifylint
err = x.UnmarshalWithoutUnknownFields([]byte(`{"field": "abc"}`), &v)
assert.NoError(t, err, "% -+#.1v", err) //nolint:testifylint
err = x.UnmarshalWithoutUnknownFields([]byte(`{"field2": "abc"}`), &v)
assert.Error(t, err)
// Extra payload should error.
// See: https://github.com/golang/go/issues/36225
err = x.UnmarshalWithoutUnknownFields([]byte(`{"field": "abc"} xxx`), &v)
assert.Error(t, err)
err = x.UnmarshalWithoutUnknownFields([]byte(`{"field": "abc"} ]`), &v)
assert.Error(t, err)
err = x.UnmarshalWithoutUnknownFields([]byte(`{"field": "abc"} {`), &v)
assert.Error(t, err)
// Extra whitespace should not error.
err = x.UnmarshalWithoutUnknownFields([]byte(`{"field": "abc"} `), &v)
assert.NoError(t, err, "% -+#.1v", err)
}
func TestMarshalWithoutEscapeHTML(t *testing.T) {
t.Parallel()
type Test struct {
Field string `json:"field"`
}
data, err := x.MarshalWithoutEscapeHTML(&Test{Field: "<body></body>"})
require.NoError(t, err, "% -+#.1v", err)
assert.Equal(t, `{"field":"<body></body>"}`, string(data))
}