@@ -5,11 +5,31 @@ import (
55 "testing"
66
77 "github.com/stretchr/testify/assert"
8+ "github.com/stretchr/testify/require"
89)
910
10- func TestClientContextUnmarshalJSON_InvalidJSON (t * testing.T ) {
11- var cc ClientContext
12- err := json .Unmarshal ([]byte (`not valid json` ), & cc )
13- assert .Error (t , err )
14- assert .Empty (t , cc .Custom )
11+ func TestClientContextUnmarshalJSON (t * testing.T ) {
12+ t .Run ("non-string custom values are serialized to string" , func (t * testing.T ) {
13+ input := `{
14+ "Client": {"installation_id": "install1"},
15+ "custom": {
16+ "key1": "stringval",
17+ "key2": {"nested": "object"},
18+ "key3": 42
19+ }
20+ }`
21+ var cc ClientContext
22+ err := json .Unmarshal ([]byte (input ), & cc )
23+ require .NoError (t , err )
24+ assert .Equal (t , "install1" , cc .Client .InstallationID )
25+ assert .Equal (t , "stringval" , cc .Custom ["key1" ])
26+ assert .JSONEq (t , `{"nested":"object"}` , cc .Custom ["key2" ])
27+ assert .Equal (t , "42" , cc .Custom ["key3" ])
28+ })
29+
30+ t .Run ("invalid JSON returns error" , func (t * testing.T ) {
31+ var cc ClientContext
32+ err := json .Unmarshal ([]byte (`not valid json` ), & cc )
33+ assert .Error (t , err )
34+ })
1535}
0 commit comments