forked from argoproj/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
44 lines (37 loc) · 1.01 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
package json
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestDisallowUnknownFields tests ability to disallow unknown fields
func TestDisallowUnknownFields(t *testing.T) {
type mystruct struct {
MyField string `json:"myField"`
}
jsonWithUnknownField := []byte(`
{
"myField": "foo",
"unknown": "bar"
}
`)
var obj mystruct
err := Unmarshal(jsonWithUnknownField, &obj)
require.NoError(t, err)
assert.Equal(t, "foo", obj.MyField)
obj = mystruct{}
err = Unmarshal(jsonWithUnknownField, &obj, DisallowUnknownFields)
require.Error(t, err)
assert.Equal(t, "foo", obj.MyField)
obj = mystruct{}
err = UnmarshalStrict(jsonWithUnknownField, &obj)
require.Error(t, err)
assert.Equal(t, "foo", obj.MyField)
}
func TestIsJSON(t *testing.T) {
assert.True(t, IsJSON([]byte(`"foo"`)))
assert.True(t, IsJSON([]byte(`{"a": "b"}`)))
assert.True(t, IsJSON([]byte(`[{"a": "b"}]`)))
assert.False(t, IsJSON([]byte(`foo`)))
assert.False(t, IsJSON([]byte(`foo: bar`)))
}