2022-05-28 05:42:28 +03:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
2017-11-29 11:42:51 +03:00
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBindWith(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
c, _ := CreateTestContext(w)
|
|
|
|
|
2024-11-15 18:52:16 +03:00
|
|
|
c.Request, _ = http.NewRequest(http.MethodPost, "/?foo=bar&bar=foo", bytes.NewBufferString("foo=unused"))
|
2017-11-29 11:42:51 +03:00
|
|
|
|
|
|
|
var obj struct {
|
|
|
|
Foo string `form:"foo"`
|
|
|
|
Bar string `form:"bar"`
|
|
|
|
}
|
2019-02-18 05:10:45 +03:00
|
|
|
captureOutput(t, func() {
|
|
|
|
assert.NoError(t, c.BindWith(&obj, binding.Form))
|
|
|
|
})
|
2017-11-29 11:42:51 +03:00
|
|
|
assert.Equal(t, "foo", obj.Bar)
|
|
|
|
assert.Equal(t, "bar", obj.Foo)
|
|
|
|
assert.Equal(t, 0, w.Body.Len())
|
|
|
|
}
|