1 Star 1 Fork 1

up-zero/gotool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
request_test.go 1.07 KB
一键复制 编辑 原始数据 按行查看 历史
GetcharZp 提交于 2023-10-25 11:54 +08:00 . fix request str to int64
package gotool
import (
"bytes"
"net/http"
"testing"
)
type shouldBindJSONStruct struct {
Z string `json:"z"`
Both string `json:"both"`
}
type shouldBindQueryBase struct {
A string `json:"a"`
B string `json:"b"`
}
type shouldBindQueryStruct struct {
*shouldBindQueryBase
Z string `json:"z"`
Both string `json:"both"`
Num int `json:"num"`
F float64 `json:"f"`
}
// TestShouldBindJSON json入参绑定
func TestShouldBindJSON(t *testing.T) {
r, err := http.NewRequest("POST", "/", bytes.NewBuffer([]byte(`{"z":"z","both":"both"}`)))
if err != nil {
t.Fatal(err)
}
data := new(shouldBindJSONStruct)
err = ShouldBindJson(r, data)
if err != nil {
t.Fatal(err)
}
t.Log(data)
}
// TestShouldBindQuery query入参绑定
func TestShouldBindQuery(t *testing.T) {
r, err := http.NewRequest("GET", "/test?a=a&b=b&z=z&both=both&num=1698205401000&f=100000.121", bytes.NewBuffer([]byte(`{"z":"z","both":"both"}`)))
if err != nil {
t.Fatal(err)
}
data := new(shouldBindQueryStruct)
err = ShouldBindQuery(r, data)
if err != nil {
t.Fatal(err)
}
t.Log(data)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/up-zero/gotool.git
git@gitee.com:up-zero/gotool.git
up-zero
gotool
gotool
main

搜索帮助