github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/gin/binding/json_test.go (about) 1 // Copyright 2019 Gin Core Team. All rights reserved. 2 // Use of this source code is governed by a MIT style 3 // license that can be found in the LICENSE file. 4 5 package binding 6 7 import ( 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestJSONBindingBindBody(t *testing.T) { 15 var s struct { 16 Foo string `json:"foo"` 17 } 18 err := jsonBinding{}.BindBody([]byte(`{"foo": "FOO"}`), &s) 19 require.NoError(t, err) 20 assert.Equal(t, "FOO", s.Foo) 21 } 22 23 func TestJSONBindingBindBodyMap(t *testing.T) { 24 s := make(map[string]string) 25 err := jsonBinding{}.BindBody([]byte(`{"foo": "FOO","hello":"world"}`), &s) 26 require.NoError(t, err) 27 assert.Len(t, s, 2) 28 assert.Equal(t, "FOO", s["foo"]) 29 assert.Equal(t, "world", s["hello"]) 30 }