forked from gin-gonic/gin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relocate binding body tests (gin-gonic#2086)
* Relocate binding body tests Every test file should be related to a tested file. Remove useless tests. * Add github.com/stretchr/testify/require package
- Loading branch information
1 parent
53146f2
commit 3308338
Showing
6 changed files
with
105 additions
and
72 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2019 Gin Core Team. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package binding | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestJSONBindingBindBody(t *testing.T) { | ||
var s struct { | ||
Foo string `json:"foo"` | ||
} | ||
err := jsonBinding{}.BindBody([]byte(`{"foo": "FOO"}`), &s) | ||
require.NoError(t, err) | ||
assert.Equal(t, "FOO", s.Foo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2019 Gin Core Team. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package binding | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/ugorji/go/codec" | ||
) | ||
|
||
func TestMsgpackBindingBindBody(t *testing.T) { | ||
type teststruct struct { | ||
Foo string `msgpack:"foo"` | ||
} | ||
var s teststruct | ||
err := msgpackBinding{}.BindBody(msgpackBody(t, teststruct{"FOO"}), &s) | ||
require.NoError(t, err) | ||
assert.Equal(t, "FOO", s.Foo) | ||
} | ||
|
||
func msgpackBody(t *testing.T, obj interface{}) []byte { | ||
var bs bytes.Buffer | ||
h := &codec.MsgpackHandle{} | ||
err := codec.NewEncoder(&bs, h).Encode(obj) | ||
require.NoError(t, err) | ||
return bs.Bytes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2019 Gin Core Team. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package binding | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestXMLBindingBindBody(t *testing.T) { | ||
var s struct { | ||
Foo string `xml:"foo"` | ||
} | ||
xmlBody := `<?xml version="1.0" encoding="UTF-8"?> | ||
<root> | ||
<foo>FOO</foo> | ||
</root>` | ||
err := xmlBinding{}.BindBody([]byte(xmlBody), &s) | ||
require.NoError(t, err) | ||
assert.Equal(t, "FOO", s.Foo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2019 Gin Core Team. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package binding | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestYAMLBindingBindBody(t *testing.T) { | ||
var s struct { | ||
Foo string `yaml:"foo"` | ||
} | ||
err := yamlBinding{}.BindBody([]byte("foo: FOO"), &s) | ||
require.NoError(t, err) | ||
assert.Equal(t, "FOO", s.Foo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters