...
1
2
3
4
5 package gin
6
7 import (
8 "bytes"
9 "net/http"
10 "net/http/httptest"
11 "testing"
12
13 "github.com/gin-gonic/gin/binding"
14 "github.com/stretchr/testify/assert"
15 )
16
17 func TestBindWith(t *testing.T) {
18 w := httptest.NewRecorder()
19 c, _ := CreateTestContext(w)
20
21 c.Request, _ = http.NewRequest("POST", "/?foo=bar&bar=foo", bytes.NewBufferString("foo=unused"))
22
23 var obj struct {
24 Foo string `form:"foo"`
25 Bar string `form:"bar"`
26 }
27 captureOutput(t, func() {
28 assert.NoError(t, c.BindWith(&obj, binding.Form))
29 })
30 assert.Equal(t, "foo", obj.Bar)
31 assert.Equal(t, "bar", obj.Foo)
32 assert.Equal(t, 0, w.Body.Len())
33 }
34
View as plain text