2022-03-10 09:22:50 +08:00
|
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
|
|
|
|
package ghttp_test
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"fmt"
|
2025-01-14 09:28:19 +08:00
|
|
|
|
"net/http"
|
2022-03-10 09:22:50 +08:00
|
|
|
|
"testing"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2022-10-08 11:46:38 +08:00
|
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
2023-10-19 19:58:19 +08:00
|
|
|
|
"github.com/gogf/gf/v2/encoding/gurl"
|
2022-03-10 09:22:50 +08:00
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
|
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
2023-10-09 20:00:08 +08:00
|
|
|
|
"github.com/gogf/gf/v2/util/gtag"
|
2022-03-10 09:22:50 +08:00
|
|
|
|
"github.com/gogf/gf/v2/util/guid"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/1609
|
|
|
|
|
|
func Test_Issue1609(t *testing.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
group := s.Group("/api/get")
|
|
|
|
|
|
group.GET("/", func(r *ghttp.Request) {
|
|
|
|
|
|
r.Response.Write("get")
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
gtest.Assert(s.Start(), nil)
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
c := g.Client()
|
|
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
|
|
|
|
|
|
|
|
|
|
|
t.Assert(c.GetContent(ctx, "/api/get"), "get")
|
|
|
|
|
|
t.Assert(c.PostContent(ctx, "/test"), "Not Found")
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Test_Issue1611(t *testing.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
v := g.View(guid.S())
|
|
|
|
|
|
content := "This is header"
|
2022-03-17 16:58:04 +08:00
|
|
|
|
gtest.AssertNil(v.SetPath(gtest.DataPath("issue1611")))
|
2022-03-10 09:22:50 +08:00
|
|
|
|
s.SetView(v)
|
|
|
|
|
|
s.BindHandler("/", func(r *ghttp.Request) {
|
|
|
|
|
|
gtest.AssertNil(r.Response.WriteTpl("index/layout.html", g.Map{
|
|
|
|
|
|
"header": content,
|
|
|
|
|
|
}))
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
c := g.Client()
|
|
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
|
|
|
|
|
|
|
|
|
|
|
t.Assert(gstr.Contains(c.GetContent(ctx, "/"), content), true)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/1626
|
|
|
|
|
|
func Test_Issue1626(t *testing.T) {
|
|
|
|
|
|
type TestReq struct {
|
|
|
|
|
|
Name string `v:"required"`
|
|
|
|
|
|
}
|
|
|
|
|
|
type TestRes struct {
|
|
|
|
|
|
Name string
|
|
|
|
|
|
}
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.Use(
|
|
|
|
|
|
ghttp.MiddlewareHandlerResponse,
|
|
|
|
|
|
func(r *ghttp.Request) {
|
|
|
|
|
|
r.Middleware.Next()
|
|
|
|
|
|
if err := r.GetError(); err != nil {
|
|
|
|
|
|
r.Response.ClearBuffer()
|
|
|
|
|
|
r.Response.Write(err.Error())
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
)
|
|
|
|
|
|
s.BindHandler("/test", func(ctx context.Context, req *TestReq) (res *TestRes, err error) {
|
|
|
|
|
|
return &TestRes{Name: req.Name}, nil
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
c := g.Client()
|
|
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
|
|
|
|
|
|
|
|
|
|
|
t.Assert(c.GetContent(ctx, "/test"), `The Name field is required`)
|
|
|
|
|
|
t.Assert(
|
|
|
|
|
|
gstr.Contains(c.GetContent(ctx, "/test?name=john"), `{"Name":"john"}`),
|
|
|
|
|
|
true,
|
|
|
|
|
|
)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Issue1653TestReq struct {
|
|
|
|
|
|
g.Meta `path:"/test" method:"post" summary:"执行报表查询" tags:""`
|
|
|
|
|
|
UUID string `json:"uuid" v:"required#菜单唯一码不可为空" dc:""`
|
|
|
|
|
|
Limit int `json:"limit"`
|
|
|
|
|
|
Filter []g.Map `json:"filter"`
|
|
|
|
|
|
FilterMap g.Map `json:"filter_map"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Issue1653TestRes struct {
|
refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395)
This pull request standardizes the use of the Go 1.18+ `any` type alias
instead of `interface{}` throughout the codebase. The change improves
code readability and aligns with modern Go best practices. The update
touches many files, including core data structures, code generation
templates, logging utilities, and test data, ensuring consistency across
all usages.
**Type alias migration to `any`:**
* Replaced all instances of `interface{}` with `any` in core data
structures such as `garray` and in generated model structs (e.g.,
`TableUser`, `User1`, `User2`) to modernize type usage.
[[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31)
[[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19)
[[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18)
[[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19)
[[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19)
[[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19)
* Updated function signatures, method parameters, and return types from
`interface{}` to `any` in various parts of the codebase, including code
generation, service logic, and logging utilities (e.g., `mlog`).
[[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55)
[[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74)
[[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73)
[[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41)
[[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107)
[[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121)
[[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112)
[[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36)
[[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74)
[[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96)
**Generated code and templates:**
* Adjusted generated files and code generation templates to output `any`
instead of `interface{}` for relevant struct fields and function
signatures, ensuring that new code generation aligns with the updated
convention.
[[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19)
[[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18)
[[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19)
[[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19)
[[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19)
[[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55)
[[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73)
[[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41)
**Container and utility updates:**
* Refactored the `garray` container implementation and related
constructors/methods to use `[]any` instead of `[]interface{}`, along
with corresponding function signatures.
[[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31)
[[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52)
[[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62)
[[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86)
[[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97)
[[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114)
[[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124)
[[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143)
[[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167)
These changes collectively modernize the codebase and prepare it for
future Go developments by using the idiomatic `any` type.
2025-08-28 16:53:19 +08:00
|
|
|
|
UUID string `json:"uuid"`
|
|
|
|
|
|
FeedBack any `json:"feed_back"`
|
2022-03-10 09:22:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type cIssue1653Foo struct{}
|
|
|
|
|
|
|
|
|
|
|
|
var Issue1653Foo = new(cIssue1653Foo)
|
|
|
|
|
|
|
|
|
|
|
|
func (r cIssue1653Foo) PostTest(ctx context.Context, req *Issue1653TestReq) (*Issue1653TestRes, error) {
|
|
|
|
|
|
return &Issue1653TestRes{UUID: req.UUID, FeedBack: req.Filter[0]["code"]}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Test_Issue1653(t *testing.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.Use(ghttp.MiddlewareHandlerResponse)
|
|
|
|
|
|
s.Group("/boot", func(grp *ghttp.RouterGroup) {
|
|
|
|
|
|
grp.Bind(Issue1653Foo)
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
|
|
// g.Client()测试:
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
c := g.Client()
|
|
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
|
|
|
|
|
dataReq := `
|
|
|
|
|
|
{"uuid":"28ee701c-7daf-4cdc-9a62-6d6704e6112b","limit":0,"filter":
|
|
|
|
|
|
[
|
|
|
|
|
|
{
|
|
|
|
|
|
"code":"P00001","constraint":"",
|
|
|
|
|
|
"created_at":"2022-03-08 04:56:15","created_by":"3ed72aba-1622-4262-a61e-83581e020763","default_value":"MonthStart()",
|
|
|
|
|
|
"expression":"AND A.DLVDAT_0>='%v'","force":false,"frequent":true,"name":"发货日期起",
|
|
|
|
|
|
"parent":"13109602-0da3-49b9-827f-2f44183ab756","read_only":false,"reference":null,"type":"date",
|
|
|
|
|
|
"updated_at":"2022-03-08 04:56:15","updated_by":"3ed72aba-1622-4262-a61e-83581e020763","updated_tick":1,
|
|
|
|
|
|
"uuid":"e6cd3268-1d75-42e0-83f9-f1f7b29976e8"
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"code":"P00002","constraint":"","created_at":"2022-03-08 04:56:15","created_by":
|
|
|
|
|
|
"3ed72aba-1622-4262-a61e-83581e020763","default_value":"MonthEnd()","expression":"AND A.DLVDAT_0<='%v'","force":false,"frequent":true,
|
|
|
|
|
|
"name":"发货日期止","parent":"13109602-0da3-49b9-827f-2f44183ab756","read_only":false,"reference":null,"type":"date","updated_at":
|
|
|
|
|
|
"2022-03-08 04:56:15","updated_by":"3ed72aba-1622-4262-a61e-83581e020763","updated_tick":1,"uuid":"dba005b5-655e-4ac4-8b22-898aa3ad2294"
|
|
|
|
|
|
}
|
|
|
|
|
|
],
|
|
|
|
|
|
"filter_map":{"P00001":1646064000000,"P00002":1648742399999},
|
|
|
|
|
|
"selector_template":""
|
|
|
|
|
|
}
|
|
|
|
|
|
`
|
|
|
|
|
|
resContent := c.PostContent(ctx, "/boot/test", dataReq)
|
2025-02-27 11:59:26 +08:00
|
|
|
|
t.Assert(resContent, `{"code":0,"message":"OK","data":{"uuid":"28ee701c-7daf-4cdc-9a62-6d6704e6112b","feed_back":"P00001"}}`)
|
2022-03-10 09:22:50 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2022-03-11 10:24:42 +08:00
|
|
|
|
|
|
|
|
|
|
type LbseMasterHead struct {
|
|
|
|
|
|
Code string `json:"code" v:"code@required|min-length:1#The code is required"`
|
|
|
|
|
|
Active bool `json:"active"`
|
|
|
|
|
|
Preset bool `json:"preset"`
|
|
|
|
|
|
Superior string `json:"superior"`
|
|
|
|
|
|
Path []string `json:"path"`
|
|
|
|
|
|
Sort int `json:"sort"`
|
|
|
|
|
|
Folder bool `json:"folder"`
|
|
|
|
|
|
Test string `json:"test" v:"required"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Template struct {
|
|
|
|
|
|
LbseMasterHead
|
|
|
|
|
|
Datasource string `json:"datasource" v:"required|length:32,32#The datasource is required"`
|
|
|
|
|
|
SQLText string `json:"sql_text"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type TemplateCreateReq struct {
|
|
|
|
|
|
g.Meta `path:"/test" method:"post" summary:"Create template" tags:"Template"`
|
|
|
|
|
|
Master Template `json:"master"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type TemplateCreateRes struct{}
|
|
|
|
|
|
|
|
|
|
|
|
type cFoo1 struct{}
|
|
|
|
|
|
|
|
|
|
|
|
var Foo1 = new(cFoo1)
|
|
|
|
|
|
|
|
|
|
|
|
func (r cFoo1) PostTest1(ctx context.Context, req *TemplateCreateReq) (res *TemplateCreateRes, err error) {
|
|
|
|
|
|
g.Dump(req)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/1662
|
|
|
|
|
|
func Test_Issue662(t *testing.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.Use(ghttp.MiddlewareHandlerResponse)
|
|
|
|
|
|
s.Group("/boot", func(grp *ghttp.RouterGroup) {
|
|
|
|
|
|
grp.Bind(Foo1)
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
// g.Client()测试:
|
|
|
|
|
|
// code字段传入空字符串时,校验没有提示
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
c := g.Client()
|
|
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
|
|
|
|
|
dataReq := `
|
|
|
|
|
|
{"master":{"active":true,"code":"","created_at":"","created_by":"","created_by_text":"","datasource":"38b6f170-a584-43fc-8912-cc1e9bf1b1a9","description":"币种","folder":false,"path":"[\"XCUR\"]","preset":false,"sort":1000,"sql_text":"SELECT!!!!","superior":null,"updated_at":"","updated_by":"","updated_by_text":"","updated_tick":0,"uuid":""},"translation":[{"code":"zh_CN","text":"币种"},{"code":"en_US","text":"币种"}],"filters":null,"fields":[{"code":"F001","created_at":"2022-01-18 23:37:38","created_by":"3ed72aba-1622-4262-a61e-83581e020763","field":"value","hide":false,"min_width":120,"name":"value","parent":"296154bf-b718-4e8f-8b70-efb969b831ec","updated_at":"2022-01-18 23:37:38","updated_by":"3ed72aba-1622-4262-a61e-83581e020763","updated_tick":1,"uuid":"f2140b7a-044c-41c3-b70e-852e6160b21b"},{"code":"F002","created_at":"2022-01-18 23:37:38","created_by":"3ed72aba-1622-4262-a61e-83581e020763","field":"label","hide":false,"min_width":120,"name":"label","parent":"296154bf-b718-4e8f-8b70-efb969b831ec","updated_at":"2022-01-18 23:37:38","updated_by":"3ed72aba-1622-4262-a61e-83581e020763","updated_tick":1,"uuid":"2d3bba5d-308b-4dba-bcac-f093e6556eca"}],"limit":0}
|
|
|
|
|
|
`
|
|
|
|
|
|
t.Assert(c.PostContent(ctx, "/boot/test", dataReq), `{"code":51,"message":"The code is required","data":null}`)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2022-10-08 11:46:38 +08:00
|
|
|
|
|
|
|
|
|
|
type DemoReq struct {
|
|
|
|
|
|
g.Meta `path:"/demo" method:"post"`
|
|
|
|
|
|
Data *gjson.Json
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type DemoRes struct {
|
|
|
|
|
|
Content string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Api struct{}
|
|
|
|
|
|
|
|
|
|
|
|
func (a *Api) Demo(ctx context.Context, req *DemoReq) (res *DemoRes, err error) {
|
|
|
|
|
|
return &DemoRes{
|
|
|
|
|
|
Content: req.Data.MustToJsonString(),
|
|
|
|
|
|
}, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var api = Api{}
|
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/2172
|
|
|
|
|
|
func Test_Issue2172(t *testing.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.Use(ghttp.MiddlewareHandlerResponse)
|
|
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
|
|
group.Bind(api)
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
c := g.Client()
|
|
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
|
|
|
|
|
dataReq := `{"data":{"asd":1}}`
|
2025-02-27 11:59:26 +08:00
|
|
|
|
t.Assert(c.PostContent(ctx, "/demo", dataReq), `{"code":0,"message":"OK","data":{"Content":"{\"asd\":1}"}}`)
|
2022-10-08 11:46:38 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2022-12-22 10:25:30 +08:00
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/2334
|
|
|
|
|
|
func Test_Issue2334(t *testing.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.SetServerRoot(gtest.DataPath("static1"))
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
c := g.Client()
|
|
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
|
|
|
|
|
t.Assert(c.GetContent(ctx, "/index.html"), "index")
|
|
|
|
|
|
|
|
|
|
|
|
c.SetHeader("If-Modified-Since", "Mon, 12 Dec 2040 05:53:35 GMT")
|
|
|
|
|
|
res, _ := c.Get(ctx, "/index.html")
|
|
|
|
|
|
t.Assert(res.StatusCode, 304)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-07-25 20:28:50 +08:00
|
|
|
|
|
|
|
|
|
|
type CreateOrderReq struct {
|
|
|
|
|
|
g.Meta `path:"/order" tags:"订单" method:"put" summary:"创建订单"`
|
|
|
|
|
|
Details []*OrderDetail `p:"detail" v:"required#请输入订单详情" dc:"订单详情"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type OrderDetail struct {
|
|
|
|
|
|
Name string `p:"name" v:"required#请输入物料名称" dc:"物料名称"`
|
|
|
|
|
|
Sn string `p:"sn" v:"required#请输入客户编号" dc:"客户编号"`
|
|
|
|
|
|
Images string `p:"images" dc:"图片"`
|
|
|
|
|
|
Desc string `p:"desc" dc:"备注"`
|
|
|
|
|
|
Number int `p:"number" v:"required#请输入数量" dc:"数量"`
|
|
|
|
|
|
Price float64 `p:"price" v:"required" dc:"单价"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type CreateOrderRes struct{}
|
|
|
|
|
|
type OrderController struct{}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *OrderController) CreateOrder(ctx context.Context, req *CreateOrderReq) (res *CreateOrderRes, err error) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/2482
|
|
|
|
|
|
func Test_Issue2482(t *testing.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.Group("/api/v2", func(group *ghttp.RouterGroup) {
|
|
|
|
|
|
group.Middleware(ghttp.MiddlewareHandlerResponse)
|
|
|
|
|
|
group.Bind(OrderController{})
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
c := g.Client()
|
|
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
content := `
|
|
|
|
|
|
{
|
|
|
|
|
|
"detail": [
|
|
|
|
|
|
{
|
|
|
|
|
|
"images": "string",
|
|
|
|
|
|
"desc": "string",
|
|
|
|
|
|
"number": 0,
|
|
|
|
|
|
"price": 0
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
`
|
|
|
|
|
|
t.Assert(c.PutContent(ctx, "/api/v2/order", content), `{"code":51,"message":"请输入物料名称","data":null}`)
|
|
|
|
|
|
})
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
content := `
|
|
|
|
|
|
{
|
|
|
|
|
|
"detail": [
|
|
|
|
|
|
{
|
|
|
|
|
|
"images": "string",
|
|
|
|
|
|
"desc": "string",
|
|
|
|
|
|
"number": 0,
|
|
|
|
|
|
"name": "string",
|
|
|
|
|
|
"price": 0
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
`
|
|
|
|
|
|
t.Assert(c.PutContent(ctx, "/api/v2/order", content), `{"code":51,"message":"请输入客户编号","data":null}`)
|
|
|
|
|
|
})
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
content := `
|
|
|
|
|
|
{
|
|
|
|
|
|
"detail": [
|
|
|
|
|
|
{
|
|
|
|
|
|
"images": "string",
|
|
|
|
|
|
"desc": "string",
|
|
|
|
|
|
"number": 0,
|
|
|
|
|
|
"name": "string",
|
|
|
|
|
|
"sn": "string",
|
|
|
|
|
|
"price": 0
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
`
|
2025-02-27 11:59:26 +08:00
|
|
|
|
t.Assert(c.PutContent(ctx, "/api/v2/order", content), `{"code":0,"message":"OK","data":null}`)
|
2023-07-25 20:28:50 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-10-09 20:00:08 +08:00
|
|
|
|
|
|
|
|
|
|
type Issue2890Enum string
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
Issue2890EnumA Issue2890Enum = "a"
|
|
|
|
|
|
Issue2890EnumB Issue2890Enum = "b"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Issue2890Req struct {
|
|
|
|
|
|
g.Meta `path:"/issue2890" method:"post"`
|
|
|
|
|
|
Id int
|
|
|
|
|
|
Enums Issue2890Enum `v:"required|enums"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Issue2890Res struct{}
|
|
|
|
|
|
type Issue2890Controller struct{}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *Issue2890Controller) Post(ctx context.Context, req *Issue2890Req) (res *Issue2890Res, err error) {
|
|
|
|
|
|
g.RequestFromCtx(ctx).Response.Write(req.Enums)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/2890
|
|
|
|
|
|
func Test_Issue2890(t *testing.T) {
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
oldEnumsJson, err := gtag.GetGlobalEnums()
|
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
defer t.AssertNil(gtag.SetGlobalEnums(oldEnumsJson))
|
|
|
|
|
|
|
|
|
|
|
|
err = gtag.SetGlobalEnums(`{"github.com/gogf/gf/v2/net/ghttp_test.Issue2890Enum": ["a","b"]}`)
|
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.Group("/api/v2", func(group *ghttp.RouterGroup) {
|
|
|
|
|
|
group.Middleware(ghttp.MiddlewareHandlerResponse)
|
|
|
|
|
|
group.Bind(Issue2890Controller{})
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
c := g.Client()
|
|
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
|
|
|
|
|
t.Assert(
|
|
|
|
|
|
c.PostContent(ctx, "/api/v2/issue2890", ``),
|
|
|
|
|
|
`{"code":51,"message":"The Enums field is required","data":null}`,
|
|
|
|
|
|
)
|
|
|
|
|
|
t.Assert(
|
|
|
|
|
|
c.PostContent(ctx, "/api/v2/issue2890", `{"Enums":"c"}`),
|
2023-10-11 21:33:51 +08:00
|
|
|
|
"{\"code\":51,\"message\":\"The Enums value `c` should be in enums of: [\\\"a\\\",\\\"b\\\"]\",\"data\":null}",
|
|
|
|
|
|
)
|
2023-10-09 20:00:08 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-10-19 19:58:19 +08:00
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/2963
|
|
|
|
|
|
func Test_Issue2963(t *testing.T) {
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.SetServerRoot(gtest.DataPath("issue2963"))
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
c := g.Client()
|
|
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
|
|
|
|
|
t.Assert(c.GetContent(ctx, "/1.txt"), `1`)
|
|
|
|
|
|
t.Assert(c.GetContent(ctx, "/中文G146(1)-icon.txt"), `中文G146(1)-icon`)
|
|
|
|
|
|
t.Assert(c.GetContent(ctx, "/"+gurl.Encode("中文G146(1)-icon.txt")), `中文G146(1)-icon`)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-10-24 10:02:04 +08:00
|
|
|
|
|
|
|
|
|
|
type Issue3077Req struct {
|
|
|
|
|
|
g.Meta `path:"/echo" method:"get"`
|
|
|
|
|
|
A string `default:"a"`
|
|
|
|
|
|
B string `default:""`
|
|
|
|
|
|
}
|
|
|
|
|
|
type Issue3077Res struct {
|
|
|
|
|
|
g.Meta `mime:"text/html"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Issue3077V1 struct{}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *Issue3077V1) Hello(ctx context.Context, req *Issue3077Req) (res *Issue3077Res, err error) {
|
|
|
|
|
|
g.RequestFromCtx(ctx).Response.Write(fmt.Sprintf("%v", req))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/3077
|
|
|
|
|
|
func Test_Issue3077(t *testing.T) {
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
|
|
group.Bind(Issue3077V1{})
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
c := g.Client()
|
|
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
|
|
|
|
|
t.Assert(c.GetContent(ctx, "/echo?a=1&b=2"), `&{{} 1 2}`)
|
|
|
|
|
|
t.Assert(c.GetContent(ctx, "/echo?"), `&{{} a }`)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2024-02-02 10:57:24 +08:00
|
|
|
|
|
|
|
|
|
|
type ListMessageReq struct {
|
|
|
|
|
|
g.Meta `path:"/list" method:"get"`
|
|
|
|
|
|
StartTime int64
|
|
|
|
|
|
EndTime int64
|
|
|
|
|
|
}
|
|
|
|
|
|
type ListMessageRes struct {
|
|
|
|
|
|
g.Meta
|
|
|
|
|
|
Title string
|
|
|
|
|
|
Content string
|
|
|
|
|
|
}
|
|
|
|
|
|
type BaseRes[T any] struct {
|
|
|
|
|
|
g.Meta
|
|
|
|
|
|
Code int
|
|
|
|
|
|
Data T
|
|
|
|
|
|
Msg string
|
|
|
|
|
|
}
|
|
|
|
|
|
type cMessage struct{}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *cMessage) List(ctx context.Context, req *ListMessageReq) (res *BaseRes[*ListMessageRes], err error) {
|
|
|
|
|
|
res = &BaseRes[*ListMessageRes]{
|
|
|
|
|
|
Code: 100,
|
|
|
|
|
|
Data: &ListMessageRes{
|
|
|
|
|
|
Title: "title",
|
|
|
|
|
|
Content: "hello",
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
return res, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/2457
|
|
|
|
|
|
func Test_Issue2457(t *testing.T) {
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.Use(ghttp.MiddlewareHandlerResponse)
|
|
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
|
|
group.Bind(
|
|
|
|
|
|
new(cMessage),
|
|
|
|
|
|
)
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
c := g.Client()
|
|
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2025-02-27 11:59:26 +08:00
|
|
|
|
t.Assert(c.GetContent(ctx, "/list"), `{"code":0,"message":"OK","data":{"Code":100,"Data":{"Title":"title","Content":"hello"},"Msg":""}}`)
|
2024-02-02 10:57:24 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2024-02-06 11:47:25 +08:00
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/3245
|
|
|
|
|
|
type Issue3245Req struct {
|
|
|
|
|
|
g.Meta `path:"/hello" method:"get"`
|
|
|
|
|
|
Name string `p:"nickname" json:"name"`
|
|
|
|
|
|
XHeaderName string `p:"Header-Name" in:"header" json:"X-Header-Name"`
|
|
|
|
|
|
XHeaderAge uint8 `p:"Header-Age" in:"cookie" json:"X-Header-Age"`
|
|
|
|
|
|
}
|
|
|
|
|
|
type Issue3245Res struct {
|
|
|
|
|
|
Reply any
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Issue3245V1 struct{}
|
|
|
|
|
|
|
|
|
|
|
|
func (Issue3245V1) Hello(ctx context.Context, req *Issue3245Req) (res *Issue3245Res, err error) {
|
|
|
|
|
|
res = &Issue3245Res{
|
|
|
|
|
|
Reply: req,
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func Test_Issue3245(t *testing.T) {
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.Use(ghttp.MiddlewareHandlerResponse)
|
|
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
|
|
group.Bind(
|
|
|
|
|
|
new(Issue3245V1),
|
|
|
|
|
|
)
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
c := g.Client()
|
|
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
|
|
|
|
|
c.SetHeader("Header-Name", "oldme")
|
|
|
|
|
|
c.SetCookie("Header-Age", "25")
|
|
|
|
|
|
|
2025-02-27 11:59:26 +08:00
|
|
|
|
expect := `{"code":0,"message":"OK","data":{"Reply":{"name":"oldme","X-Header-Name":"oldme","X-Header-Age":25}}}`
|
2024-02-06 11:47:25 +08:00
|
|
|
|
t.Assert(c.GetContent(ctx, "/hello?nickname=oldme"), expect)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2024-09-23 19:05:32 +08:00
|
|
|
|
|
|
|
|
|
|
type ItemSecondThird struct {
|
|
|
|
|
|
SecondID uint64 `json:"secondId,string"`
|
|
|
|
|
|
ThirdID uint64 `json:"thirdId,string"`
|
|
|
|
|
|
}
|
|
|
|
|
|
type ItemFirst struct {
|
|
|
|
|
|
ID uint64 `json:"id,string"`
|
|
|
|
|
|
ItemSecondThird
|
|
|
|
|
|
}
|
|
|
|
|
|
type ItemInput struct {
|
|
|
|
|
|
ItemFirst
|
|
|
|
|
|
}
|
|
|
|
|
|
type Issue3789Req struct {
|
|
|
|
|
|
g.Meta `path:"/hello" method:"GET"`
|
|
|
|
|
|
ItemInput
|
|
|
|
|
|
}
|
|
|
|
|
|
type Issue3789Res struct {
|
|
|
|
|
|
ItemInput
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Issue3789 struct{}
|
|
|
|
|
|
|
|
|
|
|
|
func (Issue3789) Say(ctx context.Context, req *Issue3789Req) (res *Issue3789Res, err error) {
|
|
|
|
|
|
res = &Issue3789Res{
|
|
|
|
|
|
ItemInput: req.ItemInput,
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/3789
|
2024-09-24 11:51:53 +08:00
|
|
|
|
func Test_Issue3789(t *testing.T) {
|
2024-09-23 19:05:32 +08:00
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
s := g.Server()
|
|
|
|
|
|
s.Use(ghttp.MiddlewareHandlerResponse)
|
|
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
|
|
group.Bind(
|
|
|
|
|
|
new(Issue3789),
|
|
|
|
|
|
)
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
c := g.Client()
|
|
|
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2025-02-27 11:59:26 +08:00
|
|
|
|
expect := `{"code":0,"message":"OK","data":{"id":"0","secondId":"2","thirdId":"3"}}`
|
2024-09-23 19:05:32 +08:00
|
|
|
|
t.Assert(c.GetContent(ctx, "/hello?id=&secondId=2&thirdId=3"), expect)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2024-12-26 10:18:47 +08:00
|
|
|
|
|
2025-01-14 09:28:19 +08:00
|
|
|
|
// https://github.com/gogf/gf/issues/4108
|
|
|
|
|
|
func Test_Issue4108(t *testing.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
|
|
group.Middleware(ghttp.MiddlewareHandlerResponse)
|
|
|
|
|
|
group.GET("/", func(r *ghttp.Request) {
|
|
|
|
|
|
r.Response.Writer.Write([]byte("hello"))
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
client := g.Client()
|
|
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
|
|
|
|
|
|
|
|
|
|
|
rsp, err := client.Get(ctx, "/")
|
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
t.Assert(rsp.StatusCode, http.StatusOK)
|
|
|
|
|
|
t.Assert(rsp.ReadAllString(), "hello")
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2025-01-22 09:28:06 +08:00
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/4115
|
|
|
|
|
|
func Test_Issue4115(t *testing.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.Use(func(r *ghttp.Request) {
|
|
|
|
|
|
r.Response.Writer.Write([]byte("hello"))
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
client := g.Client()
|
|
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
|
|
|
|
|
|
|
|
|
|
|
rsp, err := client.Get(ctx, "/")
|
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
t.Assert(rsp.StatusCode, http.StatusOK)
|
|
|
|
|
|
t.Assert(rsp.ReadAllString(), "hello")
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2025-01-22 19:17:16 +08:00
|
|
|
|
|
2024-12-26 10:18:47 +08:00
|
|
|
|
// https://github.com/gogf/gf/issues/4047
|
|
|
|
|
|
func Test_Issue4047(t *testing.T) {
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
err := s.SetConfigWithMap(g.Map{
|
|
|
|
|
|
"logger": nil,
|
|
|
|
|
|
})
|
|
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
t.Assert(s.Logger(), nil)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2025-03-08 20:56:27 +08:00
|
|
|
|
|
|
|
|
|
|
// Issue4093Req
|
|
|
|
|
|
type Issue4093Req struct {
|
|
|
|
|
|
g.Meta `path:"/test" method:"post"`
|
|
|
|
|
|
Page int `json:"page" example:"10" d:"1" v:"min:1#页码最小值不能低于1" dc:"当前页码"`
|
|
|
|
|
|
PerPage int `json:"pageSize" example:"1" d:"10" v:"min:1|max:200#每页数量最小值不能低于1|最大值不能大于200" dc:"每页数量"`
|
|
|
|
|
|
Pagination bool `json:"pagination" d:"true" dc:"是否需要进行分页"`
|
|
|
|
|
|
Name string `json:"name" d:"john"`
|
|
|
|
|
|
Number int `json:"number" d:"1"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Issue4093Res struct {
|
|
|
|
|
|
g.Meta `mime:"text/html" example:"string"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
Issue4093 = cIssue4093{}
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type cIssue4093 struct{}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *cIssue4093) User(ctx context.Context, req *Issue4093Req) (res *Issue4093Res, err error) {
|
|
|
|
|
|
g.RequestFromCtx(ctx).Response.WriteJson(req)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/4093
|
|
|
|
|
|
func Test_Issue4093(t *testing.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
|
|
group.Middleware(ghttp.MiddlewareHandlerResponse)
|
|
|
|
|
|
group.Bind(Issue4093)
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
|
|
|
|
|
|
client := g.Client().ContentJson()
|
|
|
|
|
|
client.SetPrefix(prefix)
|
|
|
|
|
|
|
|
|
|
|
|
t.Assert(client.PostContent(ctx, "/test", `{"pagination":false,"name":"","number":0}`), `{"page":1,"pageSize":10,"pagination":false,"name":"","number":0}`)
|
|
|
|
|
|
t.Assert(client.PostContent(ctx, "/test"), `{"page":1,"pageSize":10,"pagination":true,"name":"john","number":1}`)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2025-08-29 09:46:48 +08:00
|
|
|
|
|
|
|
|
|
|
// Issue4227Req
|
|
|
|
|
|
type Issue4227Req struct {
|
|
|
|
|
|
g.Meta `path:"/hello/:path_param" method:"post"`
|
|
|
|
|
|
HeaderParam string `json:"Authorization" in:"header" default:"Bearer token123"`
|
|
|
|
|
|
QueryParam bool `json:"query_param" in:"query" default:"false"`
|
|
|
|
|
|
PathParam int `json:"path_param" in:"path" default:"123" v:"required"`
|
|
|
|
|
|
CookieParam bool `json:"cookie_param" in:"cookie" default:"false"`
|
|
|
|
|
|
BodyParam bool `json:"body_param" default:"false"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Issue4227Res struct {
|
|
|
|
|
|
g.Meta `mime:"application/json"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
Issue4227 = cIssue4227{}
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type cIssue4227 struct{}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *cIssue4227) Feature(ctx context.Context, req *Issue4227Req) (res *Issue4227Res, err error) {
|
|
|
|
|
|
g.RequestFromCtx(ctx).Response.WriteJson(req)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/4227
|
|
|
|
|
|
func Test_Issue4227(t *testing.T) {
|
|
|
|
|
|
s := g.Server(guid.S())
|
|
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
|
|
group.Middleware(ghttp.MiddlewareHandlerResponse)
|
|
|
|
|
|
group.Bind(Issue4227)
|
|
|
|
|
|
})
|
|
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
|
|
s.Start()
|
|
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
|
|
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
|
|
|
|
|
|
client := g.Client().ContentJson()
|
|
|
|
|
|
client.SetPrefix(prefix)
|
|
|
|
|
|
|
|
|
|
|
|
resp1 := client.PostContent(ctx, "/hello/123", `{}`)
|
|
|
|
|
|
t.Assert(resp1, `{"Authorization":"Bearer token123","query_param":false,"path_param":123,"cookie_param":false,"body_param":false}`)
|
|
|
|
|
|
|
|
|
|
|
|
client.SetHeader("Authorization", "Bearer token123")
|
|
|
|
|
|
|
|
|
|
|
|
resp2 := client.PostContent(ctx, "/hello/123", `{"body_param":"true"}`)
|
|
|
|
|
|
t.Assert(resp2, `{"Authorization":"Bearer token123","query_param":false,"path_param":123,"cookie_param":false,"body_param":true}`)
|
|
|
|
|
|
|
|
|
|
|
|
client.SetCookie("cookie_param", "true")
|
|
|
|
|
|
resp3 := client.PostContent(ctx, "/hello/123", `{}`)
|
|
|
|
|
|
t.Assert(resp3, `{"Authorization":"Bearer token123","query_param":false,"path_param":123,"cookie_param":true,"body_param":false}`)
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|