gin/binding/query_test.go
copilot-swe-agent[bot] 33581e60a4 fix(binding): support query tag in query binding while maintaining form tag backward compatibility
Co-authored-by: appleboy <21979+appleboy@users.noreply.github.com>
2025-09-21 09:54:59 +00:00

55 lines
1.1 KiB
Go

package binding
import (
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestQueryBindingWithQueryTag(t *testing.T) {
var s struct {
Foo string `query:"foo"`
Bar string `query:"bar"`
}
request := &http.Request{URL: &url.URL{RawQuery: "foo=HELLO&bar=WORLD"}}
err := queryBinding{}.Bind(request, &s)
require.NoError(t, err)
assert.Equal(t, "HELLO", s.Foo)
assert.Equal(t, "WORLD", s.Bar)
}
func TestQueryBindingWithFormTag(t *testing.T) {
var s struct {
Foo string `form:"foo"`
Bar string `form:"bar"`
}
request := &http.Request{URL: &url.URL{RawQuery: "foo=HELLO&bar=WORLD"}}
err := queryBinding{}.Bind(request, &s)
require.NoError(t, err)
assert.Equal(t, "HELLO", s.Foo)
assert.Equal(t, "WORLD", s.Bar)
}
func TestQueryBindingMixedTags(t *testing.T) {
var s struct {
Foo string `query:"foo"`
Bar string `form:"bar"`
}
request := &http.Request{URL: &url.URL{RawQuery: "foo=HELLO&bar=WORLD"}}
err := queryBinding{}.Bind(request, &s)
require.NoError(t, err)
assert.Equal(t, "HELLO", s.Foo)
assert.Equal(t, "WORLD", s.Bar)
}