2020-07-26 17:09:05 +08:00
|
|
|
package httpx
|
|
|
|
|
|
|
|
import (
|
2024-05-11 22:25:10 +08:00
|
|
|
"bytes"
|
2025-01-22 21:35:32 +08:00
|
|
|
"encoding/json"
|
2023-02-26 21:58:58 +08:00
|
|
|
"errors"
|
2020-07-26 17:09:05 +08:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2023-02-26 21:58:58 +08:00
|
|
|
"reflect"
|
2020-07-26 17:09:05 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-04-28 15:12:04 +08:00
|
|
|
"github.com/zeromicro/go-zero/rest/internal/header"
|
2022-01-04 15:51:32 +08:00
|
|
|
"github.com/zeromicro/go-zero/rest/pathvar"
|
2020-07-26 17:09:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseForm(t *testing.T) {
|
2024-04-04 20:28:54 +08:00
|
|
|
t.Run("slice", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name string `form:"name"`
|
|
|
|
Age int `form:"age"`
|
|
|
|
Percent float64 `form:"percent,optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?name=hello&age=18&percent=3.4",
|
|
|
|
http.NoBody)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, Parse(r, &v))
|
|
|
|
assert.Equal(t, "hello", v.Name)
|
|
|
|
assert.Equal(t, 18, v.Age)
|
|
|
|
assert.Equal(t, 3.4, v.Percent)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no value", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
NoValue string `form:"noValue,optional"`
|
|
|
|
}
|
2020-07-26 17:09:05 +08:00
|
|
|
|
2024-04-04 20:28:54 +08:00
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?name=hello&age=18&percent=3.4&statuses=try&statuses=done&singleValue=one",
|
|
|
|
http.NoBody)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, Parse(r, &v))
|
|
|
|
assert.Equal(t, 0, len(v.NoValue))
|
|
|
|
})
|
2025-01-13 01:03:32 +08:00
|
|
|
|
|
|
|
t.Run("slice with one value on array format", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Names string `form:"names"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?names=1,2,3",
|
|
|
|
http.NoBody)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.Equal(t, "1,2,3", v.Names)
|
|
|
|
}
|
|
|
|
})
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
|
|
|
|
2024-11-02 21:55:37 +08:00
|
|
|
func TestParseFormArray(t *testing.T) {
|
|
|
|
t.Run("slice", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name []string `form:"name"`
|
|
|
|
Age []int `form:"age"`
|
|
|
|
Percent []float64 `form:"percent,optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?name=hello&name=world&age=18&age=19&percent=3.4&percent=4.5",
|
|
|
|
http.NoBody)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.ElementsMatch(t, []string{"hello", "world"}, v.Name)
|
|
|
|
assert.ElementsMatch(t, []int{18, 19}, v.Age)
|
|
|
|
assert.ElementsMatch(t, []float64{3.4, 4.5}, v.Percent)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("slice with single value", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name []string `form:"name"`
|
|
|
|
Age []int `form:"age"`
|
|
|
|
Percent []float64 `form:"percent,optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?name=hello&age=18&percent=3.4",
|
|
|
|
http.NoBody)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.ElementsMatch(t, []string{"hello"}, v.Name)
|
|
|
|
assert.ElementsMatch(t, []int{18}, v.Age)
|
|
|
|
assert.ElementsMatch(t, []float64{3.4}, v.Percent)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-12-23 00:56:20 +08:00
|
|
|
t.Run("slice with empty", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name []string `form:"name,optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a",
|
|
|
|
http.NoBody)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.ElementsMatch(t, []string{}, v.Name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("slice with empty", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name []string `form:"name,optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?name=",
|
|
|
|
http.NoBody)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
2025-01-13 01:03:32 +08:00
|
|
|
assert.Empty(t, v.Name)
|
2024-12-23 00:56:20 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-11-02 21:55:37 +08:00
|
|
|
t.Run("slice with empty and non-empty", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name []string `form:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?name=&name=1",
|
|
|
|
http.NoBody)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
2025-01-13 01:03:32 +08:00
|
|
|
assert.ElementsMatch(t, []string{"1"}, v.Name)
|
2024-12-23 00:56:20 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("slice with one value on array format", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Names []string `form:"names"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?names=1,2,3",
|
|
|
|
http.NoBody)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.ElementsMatch(t, []string{"1", "2", "3"}, v.Names)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("slice with one value on combined array format", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Names []string `form:"names"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?names=[1,2,3]&names=4",
|
|
|
|
http.NoBody)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.ElementsMatch(t, []string{"[1,2,3]", "4"}, v.Names)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("slice with one value on integer array format", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Numbers []int `form:"numbers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?numbers=1,2,3",
|
|
|
|
http.NoBody)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.ElementsMatch(t, []int{1, 2, 3}, v.Numbers)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("slice with one value on array format brackets", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Names []string `form:"names"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?names[]=1&names[]=2&names[]=3",
|
|
|
|
http.NoBody)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.ElementsMatch(t, []string{"1", "2", "3"}, v.Names)
|
2024-11-02 21:55:37 +08:00
|
|
|
}
|
|
|
|
})
|
2025-01-13 01:03:32 +08:00
|
|
|
|
|
|
|
t.Run("slice with one empty value on integer array format", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Numbers []int `form:"numbers,optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?numbers=",
|
|
|
|
http.NoBody)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.Empty(t, v.Numbers)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("slice with one value on integer array format", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Numbers []int `form:"numbers,optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?numbers=&numbers=2",
|
|
|
|
http.NoBody)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.ElementsMatch(t, []int{2}, v.Numbers)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("slice with one empty value on float64 array format", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Numbers []float64 `form:"numbers,optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?numbers=",
|
|
|
|
http.NoBody)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.Empty(t, v.Numbers)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("slice with one value on float64 array format", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Numbers []float64 `form:"numbers,optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(
|
|
|
|
http.MethodGet,
|
|
|
|
"/a?numbers=&numbers=2",
|
|
|
|
http.NoBody)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.ElementsMatch(t, []float64{2}, v.Numbers)
|
|
|
|
}
|
|
|
|
})
|
2024-11-02 21:55:37 +08:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:10:08 +08:00
|
|
|
func TestParseForm_Error(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name string `form:"name"`
|
|
|
|
Age int `form:"age"`
|
|
|
|
}
|
|
|
|
|
2022-10-17 06:30:58 +08:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/a?name=hello;", http.NoBody)
|
2021-10-25 21:10:08 +08:00
|
|
|
assert.NotNil(t, ParseForm(r, &v))
|
|
|
|
}
|
|
|
|
|
2020-10-01 17:50:53 +08:00
|
|
|
func TestParseHeader(t *testing.T) {
|
2021-10-25 21:10:08 +08:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
expect map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
value: "",
|
|
|
|
expect: map[string]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular",
|
|
|
|
value: "key=value",
|
|
|
|
expect: map[string]string{"key": "value"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "next empty",
|
|
|
|
value: "key=value;",
|
|
|
|
expect: map[string]string{"key": "value"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "regular",
|
|
|
|
value: "key=value;foo",
|
|
|
|
expect: map[string]string{"key": "value"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
m := ParseHeader(test.value)
|
|
|
|
assert.EqualValues(t, test.expect, m)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParsePath(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name string `path:"name"`
|
|
|
|
Age int `path:"age"`
|
|
|
|
}
|
|
|
|
|
2022-10-17 06:30:58 +08:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
|
2021-10-25 21:10:08 +08:00
|
|
|
r = pathvar.WithVars(r, map[string]string{
|
|
|
|
"name": "foo",
|
|
|
|
"age": "18",
|
|
|
|
})
|
|
|
|
err := Parse(r, &v)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "foo", v.Name)
|
|
|
|
assert.Equal(t, 18, v.Age)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParsePath_Error(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name string `path:"name"`
|
|
|
|
Age int `path:"age"`
|
|
|
|
}
|
|
|
|
|
2022-10-17 06:30:58 +08:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
|
2021-10-25 21:10:08 +08:00
|
|
|
r = pathvar.WithVars(r, map[string]string{
|
|
|
|
"name": "foo",
|
|
|
|
})
|
|
|
|
assert.NotNil(t, Parse(r, &v))
|
2020-10-01 17:50:53 +08:00
|
|
|
}
|
|
|
|
|
2020-07-26 17:09:05 +08:00
|
|
|
func TestParseFormOutOfRange(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Age int `form:"age,range=[10:20)"`
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
url string
|
|
|
|
pass bool
|
|
|
|
}{
|
|
|
|
{
|
2021-10-25 21:10:08 +08:00
|
|
|
url: "/a?age=5",
|
2020-07-26 17:09:05 +08:00
|
|
|
pass: false,
|
|
|
|
},
|
|
|
|
{
|
2021-10-25 21:10:08 +08:00
|
|
|
url: "/a?age=10",
|
2020-07-26 17:09:05 +08:00
|
|
|
pass: true,
|
|
|
|
},
|
|
|
|
{
|
2021-10-25 21:10:08 +08:00
|
|
|
url: "/a?age=15",
|
2020-07-26 17:09:05 +08:00
|
|
|
pass: true,
|
|
|
|
},
|
|
|
|
{
|
2021-10-25 21:10:08 +08:00
|
|
|
url: "/a?age=20",
|
2020-07-26 17:09:05 +08:00
|
|
|
pass: false,
|
|
|
|
},
|
|
|
|
{
|
2021-10-25 21:10:08 +08:00
|
|
|
url: "/a?age=28",
|
2020-07-26 17:09:05 +08:00
|
|
|
pass: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2022-10-17 06:30:58 +08:00
|
|
|
r, err := http.NewRequest(http.MethodGet, test.url, http.NoBody)
|
2020-07-26 17:09:05 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
err = Parse(r, &v)
|
|
|
|
if test.pass {
|
|
|
|
assert.Nil(t, err)
|
|
|
|
} else {
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseMultipartForm(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name string `form:"name"`
|
|
|
|
Age int `form:"age"`
|
|
|
|
}
|
|
|
|
|
|
|
|
body := strings.Replace(`----------------------------220477612388154780019383
|
|
|
|
Content-Disposition: form-data; name="name"
|
|
|
|
|
|
|
|
kevin
|
|
|
|
----------------------------220477612388154780019383
|
|
|
|
Content-Disposition: form-data; name="age"
|
|
|
|
|
|
|
|
18
|
|
|
|
----------------------------220477612388154780019383--`, "\n", "\r\n", -1)
|
|
|
|
|
2021-10-25 21:10:08 +08:00
|
|
|
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body))
|
2020-07-26 17:09:05 +08:00
|
|
|
r.Header.Set(ContentType, "multipart/form-data; boundary=--------------------------220477612388154780019383")
|
|
|
|
|
2020-10-31 20:11:12 +08:00
|
|
|
assert.Nil(t, Parse(r, &v))
|
|
|
|
assert.Equal(t, "kevin", v.Name)
|
|
|
|
assert.Equal(t, 18, v.Age)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseMultipartFormWrongBoundary(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name string `form:"name"`
|
|
|
|
Age int `form:"age"`
|
|
|
|
}
|
|
|
|
|
|
|
|
body := strings.Replace(`----------------------------22047761238815478001938
|
|
|
|
Content-Disposition: form-data; name="name"
|
|
|
|
|
|
|
|
kevin
|
|
|
|
----------------------------22047761238815478001938
|
|
|
|
Content-Disposition: form-data; name="age"
|
|
|
|
|
|
|
|
18
|
|
|
|
----------------------------22047761238815478001938--`, "\n", "\r\n", -1)
|
|
|
|
|
2021-10-25 21:10:08 +08:00
|
|
|
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body))
|
2020-10-31 20:11:12 +08:00
|
|
|
r.Header.Set(ContentType, "multipart/form-data; boundary=--------------------------220477612388154780019383")
|
|
|
|
|
|
|
|
assert.NotNil(t, Parse(r, &v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseJsonBody(t *testing.T) {
|
2021-12-22 20:24:55 +08:00
|
|
|
t.Run("has body", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Age int `json:"age"`
|
|
|
|
}
|
|
|
|
|
|
|
|
body := `{"name":"kevin", "age": 18}`
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body))
|
2022-04-28 15:12:04 +08:00
|
|
|
r.Header.Set(ContentType, header.JsonContentType)
|
2021-12-22 20:24:55 +08:00
|
|
|
|
2023-02-26 21:58:58 +08:00
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.Equal(t, "kevin", v.Name)
|
|
|
|
assert.Equal(t, 18, v.Age)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad body", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Age int `json:"age"`
|
|
|
|
}
|
|
|
|
|
|
|
|
body := `{"name":"kevin", "ag": 18}`
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body))
|
|
|
|
r.Header.Set(ContentType, header.JsonContentType)
|
|
|
|
|
|
|
|
assert.Error(t, Parse(r, &v))
|
2021-12-22 20:24:55 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("hasn't body", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name string `json:"name,optional"`
|
|
|
|
Age int `json:"age,optional"`
|
|
|
|
}
|
|
|
|
|
2022-10-17 06:30:58 +08:00
|
|
|
r := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
|
2021-12-22 20:24:55 +08:00
|
|
|
assert.Nil(t, Parse(r, &v))
|
|
|
|
assert.Equal(t, "", v.Name)
|
|
|
|
assert.Equal(t, 0, v.Age)
|
|
|
|
})
|
2023-01-01 12:21:53 +08:00
|
|
|
|
|
|
|
t.Run("array body", func(t *testing.T) {
|
|
|
|
var v []struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Age int `json:"age"`
|
|
|
|
}
|
|
|
|
|
|
|
|
body := `[{"name":"kevin", "age": 18}]`
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body))
|
|
|
|
r.Header.Set(ContentType, header.JsonContentType)
|
|
|
|
|
2024-11-05 22:09:30 +08:00
|
|
|
assert.NoError(t, Parse(r, &v))
|
2023-01-01 12:21:53 +08:00
|
|
|
assert.Equal(t, 1, len(v))
|
|
|
|
assert.Equal(t, "kevin", v[0].Name)
|
|
|
|
assert.Equal(t, 18, v[0].Age)
|
|
|
|
})
|
2024-11-05 22:09:30 +08:00
|
|
|
|
|
|
|
t.Run("form and array body", func(t *testing.T) {
|
|
|
|
var v []struct {
|
|
|
|
// we can only ignore the form tag,
|
|
|
|
// because if the value is a slice, it should be in the body,
|
|
|
|
// but it's hard to detect when we treat it as a json body.
|
|
|
|
Product string `form:"product"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Age int `json:"age"`
|
|
|
|
}
|
|
|
|
|
|
|
|
body := `[{"name":"apple", "age": 18}]`
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/a?product=tree", strings.NewReader(body))
|
|
|
|
r.Header.Set(ContentType, header.JsonContentType)
|
|
|
|
|
|
|
|
assert.NoError(t, Parse(r, &v))
|
|
|
|
assert.Equal(t, 1, len(v))
|
|
|
|
assert.Equal(t, "apple", v[0].Name)
|
|
|
|
assert.Equal(t, 18, v[0].Age)
|
|
|
|
})
|
2025-01-22 21:35:32 +08:00
|
|
|
t.Run("bytes field", func(t *testing.T) {
|
|
|
|
type v struct {
|
|
|
|
Signature []byte `json:"signature,optional"`
|
|
|
|
}
|
|
|
|
v1 := v{
|
|
|
|
Signature: []byte{0x01, 0xff, 0x00},
|
|
|
|
}
|
|
|
|
body, _ := json.Marshal(v1)
|
|
|
|
t.Logf("body:%s", string(body))
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(body)))
|
|
|
|
r.Header.Set(ContentType, header.JsonContentType)
|
|
|
|
var v2 v
|
|
|
|
err := ParseJsonBody(r, &v2)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Greater(t, len(v2.Signature), 0)
|
|
|
|
}
|
|
|
|
t.Logf("%x", v2.Signature)
|
|
|
|
assert.EqualValues(t, v1.Signature, v2.Signature)
|
|
|
|
})
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseRequired(t *testing.T) {
|
|
|
|
v := struct {
|
|
|
|
Name string `form:"name"`
|
|
|
|
Percent float64 `form:"percent"`
|
|
|
|
}{}
|
|
|
|
|
2022-10-17 06:30:58 +08:00
|
|
|
r, err := http.NewRequest(http.MethodGet, "/a?name=hello", http.NoBody)
|
2020-07-26 17:09:05 +08:00
|
|
|
assert.Nil(t, err)
|
2020-10-31 20:11:12 +08:00
|
|
|
assert.NotNil(t, Parse(r, &v))
|
2020-09-22 17:34:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseOptions(t *testing.T) {
|
|
|
|
v := struct {
|
|
|
|
Position int8 `form:"pos,options=1|2"`
|
|
|
|
}{}
|
|
|
|
|
2022-10-17 06:30:58 +08:00
|
|
|
r, err := http.NewRequest(http.MethodGet, "/a?pos=4", http.NoBody)
|
2020-09-22 17:34:39 +08:00
|
|
|
assert.Nil(t, err)
|
2020-10-31 20:11:12 +08:00
|
|
|
assert.NotNil(t, Parse(r, &v))
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
|
|
|
|
2021-10-25 21:10:08 +08:00
|
|
|
func TestParseHeaders(t *testing.T) {
|
|
|
|
type AnonymousStruct struct {
|
|
|
|
XRealIP string `header:"x-real-ip"`
|
|
|
|
Accept string `header:"Accept,optional"`
|
|
|
|
}
|
|
|
|
v := struct {
|
|
|
|
Name string `header:"name,optional"`
|
|
|
|
Percent string `header:"percent"`
|
|
|
|
Addrs []string `header:"addrs"`
|
|
|
|
XForwardedFor string `header:"X-Forwarded-For,optional"`
|
|
|
|
AnonymousStruct
|
|
|
|
}{}
|
2022-10-17 06:30:58 +08:00
|
|
|
request, err := http.NewRequest("POST", "/", http.NoBody)
|
2021-10-25 21:10:08 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
request.Header.Set("name", "chenquan")
|
|
|
|
request.Header.Set("percent", "1")
|
|
|
|
request.Header.Add("addrs", "addr1")
|
|
|
|
request.Header.Add("addrs", "addr2")
|
|
|
|
request.Header.Add("X-Forwarded-For", "10.0.10.11")
|
|
|
|
request.Header.Add("x-real-ip", "10.0.11.10")
|
2022-04-28 15:12:04 +08:00
|
|
|
request.Header.Add("Accept", header.JsonContentType)
|
2021-10-25 21:10:08 +08:00
|
|
|
err = ParseHeaders(request, &v)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, "chenquan", v.Name)
|
|
|
|
assert.Equal(t, "1", v.Percent)
|
|
|
|
assert.Equal(t, []string{"addr1", "addr2"}, v.Addrs)
|
|
|
|
assert.Equal(t, "10.0.10.11", v.XForwardedFor)
|
|
|
|
assert.Equal(t, "10.0.11.10", v.XRealIP)
|
2022-04-28 15:12:04 +08:00
|
|
|
assert.Equal(t, header.JsonContentType, v.Accept)
|
2021-10-25 21:10:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseHeaders_Error(t *testing.T) {
|
|
|
|
v := struct {
|
|
|
|
Name string `header:"name"`
|
|
|
|
Age int `header:"age"`
|
|
|
|
}{}
|
|
|
|
|
2022-10-17 06:30:58 +08:00
|
|
|
r := httptest.NewRequest("POST", "/", http.NoBody)
|
2021-10-25 21:10:08 +08:00
|
|
|
r.Header.Set("name", "foo")
|
|
|
|
assert.NotNil(t, Parse(r, &v))
|
|
|
|
}
|
|
|
|
|
2023-02-26 21:58:58 +08:00
|
|
|
func TestParseWithValidator(t *testing.T) {
|
|
|
|
SetValidator(mockValidator{})
|
2023-08-14 22:22:22 +08:00
|
|
|
defer SetValidator(mockValidator{nop: true})
|
|
|
|
|
2023-02-26 21:58:58 +08:00
|
|
|
var v struct {
|
|
|
|
Name string `form:"name"`
|
|
|
|
Age int `form:"age"`
|
|
|
|
Percent float64 `form:"percent,optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "/a?name=hello&age=18&percent=3.4", http.NoBody)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.Equal(t, "hello", v.Name)
|
|
|
|
assert.Equal(t, 18, v.Age)
|
|
|
|
assert.Equal(t, 3.4, v.Percent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseWithValidatorWithError(t *testing.T) {
|
|
|
|
SetValidator(mockValidator{})
|
2023-08-14 22:22:22 +08:00
|
|
|
defer SetValidator(mockValidator{nop: true})
|
|
|
|
|
2023-02-26 21:58:58 +08:00
|
|
|
var v struct {
|
|
|
|
Name string `form:"name"`
|
|
|
|
Age int `form:"age"`
|
|
|
|
Percent float64 `form:"percent,optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "/a?name=world&age=18&percent=3.4", http.NoBody)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Error(t, Parse(r, &v))
|
|
|
|
}
|
|
|
|
|
2023-03-19 20:04:18 +08:00
|
|
|
func TestParseWithValidatorRequest(t *testing.T) {
|
|
|
|
SetValidator(mockValidator{})
|
2023-08-14 22:22:22 +08:00
|
|
|
defer SetValidator(mockValidator{nop: true})
|
|
|
|
|
2023-03-19 20:04:18 +08:00
|
|
|
var v mockRequest
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "/a?&age=18", http.NoBody)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Error(t, Parse(r, &v))
|
|
|
|
}
|
|
|
|
|
2023-08-14 22:22:22 +08:00
|
|
|
func TestParseFormWithDot(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Age int `form:"user.age"`
|
|
|
|
}
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "/a?user.age=18", http.NoBody)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NoError(t, Parse(r, &v))
|
|
|
|
assert.Equal(t, 18, v.Age)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParsePathWithDot(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Name string `path:"name.val"`
|
|
|
|
Age int `path:"age.val"`
|
|
|
|
}
|
|
|
|
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
|
|
|
|
r = pathvar.WithVars(r, map[string]string{
|
|
|
|
"name.val": "foo",
|
|
|
|
"age.val": "18",
|
|
|
|
})
|
|
|
|
err := Parse(r, &v)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "foo", v.Name)
|
|
|
|
assert.Equal(t, 18, v.Age)
|
|
|
|
}
|
|
|
|
|
2024-01-13 23:48:50 +08:00
|
|
|
func TestParseWithFloatPtr(t *testing.T) {
|
|
|
|
t.Run("has float32 pointer", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
WeightFloat32 *float32 `json:"weightFloat32,optional"`
|
|
|
|
}
|
|
|
|
body := `{"weightFloat32": 3.2}`
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body))
|
|
|
|
r.Header.Set(ContentType, header.JsonContentType)
|
|
|
|
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.Equal(t, float32(3.2), *v.WeightFloat32)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-03 20:33:20 +08:00
|
|
|
func TestParseWithEscapedParams(t *testing.T) {
|
|
|
|
t.Run("escaped", func(t *testing.T) {
|
|
|
|
var v struct {
|
|
|
|
Dev string `form:"dev"`
|
|
|
|
}
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "http://127.0.0.1/api/v2/dev/test?dev=se205%5fy1205%5fj109%26verRelease=v01%26iid1=863494061186673%26iid2=863494061186681%26mcc=636%26mnc=1", http.NoBody)
|
|
|
|
if assert.NoError(t, Parse(r, &v)) {
|
|
|
|
assert.Equal(t, "se205_y1205_j109&verRelease=v01&iid1=863494061186673&iid2=863494061186681&mcc=636&mnc=1", v.Dev)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-11 23:06:59 +08:00
|
|
|
func TestCustomUnmarshalerStructRequest(t *testing.T) {
|
|
|
|
reqBody := `{"name": "hello"}`
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/a", bytes.NewReader([]byte(reqBody)))
|
|
|
|
r.Header.Set(ContentType, JsonContentType)
|
|
|
|
v := struct {
|
|
|
|
Foo *mockUnmarshaler `json:"name"`
|
|
|
|
}{}
|
|
|
|
assert.Nil(t, Parse(r, &v))
|
|
|
|
assert.Equal(t, "hello", v.Foo.Name)
|
|
|
|
}
|
|
|
|
|
2024-12-23 00:56:20 +08:00
|
|
|
func TestParseJsonStringRequest(t *testing.T) {
|
|
|
|
type GoodsInfo struct {
|
|
|
|
Sku int64 `json:"sku,optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetReq struct {
|
|
|
|
GoodsList []*GoodsInfo `json:"goods_list"`
|
|
|
|
}
|
|
|
|
|
|
|
|
input := `{"goods_list":"[{\"sku\":11},{\"sku\":22}]"}`
|
|
|
|
r := httptest.NewRequest(http.MethodPost, "/a", strings.NewReader(input))
|
|
|
|
r.Header.Set(ContentType, JsonContentType)
|
|
|
|
var v GetReq
|
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
assert.NoError(t, Parse(r, &v))
|
|
|
|
assert.Equal(t, 2, len(v.GoodsList))
|
|
|
|
assert.ElementsMatch(t, []int64{11, 22}, []int64{v.GoodsList[0].Sku, v.GoodsList[1].Sku})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-26 17:09:05 +08:00
|
|
|
func BenchmarkParseRaw(b *testing.B) {
|
2022-10-17 06:30:58 +08:00
|
|
|
r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", http.NoBody)
|
2020-07-26 17:09:05 +08:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
v := struct {
|
|
|
|
Name string `form:"name"`
|
|
|
|
Age int `form:"age"`
|
|
|
|
Percent float64 `form:"percent,optional"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
v.Name = r.FormValue("name")
|
|
|
|
v.Age, err = strconv.Atoi(r.FormValue("age"))
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
v.Percent, err = strconv.ParseFloat(r.FormValue("percent"), 64)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkParseAuto(b *testing.B) {
|
2022-10-17 06:30:58 +08:00
|
|
|
r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", http.NoBody)
|
2020-07-26 17:09:05 +08:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
v := struct {
|
|
|
|
Name string `form:"name"`
|
|
|
|
Age int `form:"age"`
|
|
|
|
Percent float64 `form:"percent,optional"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
if err = Parse(r, &v); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-26 21:58:58 +08:00
|
|
|
|
2023-08-14 22:22:22 +08:00
|
|
|
type mockValidator struct {
|
|
|
|
nop bool
|
|
|
|
}
|
2023-02-26 21:58:58 +08:00
|
|
|
|
|
|
|
func (m mockValidator) Validate(r *http.Request, data any) error {
|
2023-08-14 22:22:22 +08:00
|
|
|
if m.nop {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-26 21:58:58 +08:00
|
|
|
if r.URL.Path == "/a" {
|
|
|
|
val := reflect.ValueOf(data).Elem().FieldByName("Name").String()
|
|
|
|
if val != "hello" {
|
|
|
|
return errors.New("name is not hello")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-19 20:04:18 +08:00
|
|
|
|
|
|
|
type mockRequest struct {
|
|
|
|
Name string `json:"name,optional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockRequest) Validate() error {
|
|
|
|
if m.Name != "hello" {
|
|
|
|
return errors.New("name is not hello")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-11 22:25:10 +08:00
|
|
|
|
2024-05-11 23:06:59 +08:00
|
|
|
type mockUnmarshaler struct {
|
2024-05-11 22:25:10 +08:00
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2024-05-11 23:06:59 +08:00
|
|
|
func (m *mockUnmarshaler) UnmarshalJSON(b []byte) error {
|
2024-05-11 22:25:10 +08:00
|
|
|
m.Name = string(b)
|
|
|
|
return nil
|
|
|
|
}
|