2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-12-30 22:02:46 +08:00
|
|
|
//
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-12-30 22:02:46 +08:00
|
|
|
|
|
|
|
package ghttp_test
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
2022-01-22 15:33:31 +08:00
|
|
|
"github.com/gogf/gf/v2/net/gtcp"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2022-02-22 14:12:09 +08:00
|
|
|
"github.com/gogf/gf/v2/util/guid"
|
2018-12-30 22:02:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 执行对象
|
2019-06-19 09:06:52 +08:00
|
|
|
type GroupObject struct{}
|
2018-12-30 22:02:46 +08:00
|
|
|
|
2019-03-01 23:45:55 +08:00
|
|
|
func (o *GroupObject) Init(r *ghttp.Request) {
|
2019-06-19 09:06:52 +08:00
|
|
|
r.Response.Write("1")
|
2019-02-28 23:57:20 +08:00
|
|
|
}
|
|
|
|
|
2019-03-01 23:45:55 +08:00
|
|
|
func (o *GroupObject) Shut(r *ghttp.Request) {
|
2019-06-19 09:06:52 +08:00
|
|
|
r.Response.Write("2")
|
2019-02-28 23:57:20 +08:00
|
|
|
}
|
|
|
|
|
2019-03-01 23:45:55 +08:00
|
|
|
func (o *GroupObject) Index(r *ghttp.Request) {
|
2019-06-19 09:06:52 +08:00
|
|
|
r.Response.Write("Object Index")
|
2019-02-26 22:21:57 +08:00
|
|
|
}
|
|
|
|
|
2019-03-01 23:45:55 +08:00
|
|
|
func (o *GroupObject) Show(r *ghttp.Request) {
|
2019-06-19 09:06:52 +08:00
|
|
|
r.Response.Write("Object Show")
|
2018-12-30 22:02:46 +08:00
|
|
|
}
|
|
|
|
|
2019-03-01 23:45:55 +08:00
|
|
|
func (o *GroupObject) Delete(r *ghttp.Request) {
|
2019-06-19 09:06:52 +08:00
|
|
|
r.Response.Write("Object Delete")
|
2018-12-30 22:02:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Handler(r *ghttp.Request) {
|
2019-06-19 09:06:52 +08:00
|
|
|
r.Response.Write("Handler")
|
2018-12-30 22:02:46 +08:00
|
|
|
}
|
|
|
|
|
2019-03-01 23:45:55 +08:00
|
|
|
func Test_Router_GroupBasic1(t *testing.T) {
|
2022-02-22 14:12:09 +08:00
|
|
|
s := g.Server(guid.S())
|
2019-06-19 09:06:52 +08:00
|
|
|
obj := new(GroupObject)
|
|
|
|
// 分组路由方法注册
|
2019-12-02 23:00:48 +08:00
|
|
|
group := s.Group("/api")
|
|
|
|
group.ALL("/handler", Handler)
|
|
|
|
group.ALL("/obj", obj)
|
|
|
|
group.GET("/obj/my-show", obj, "Show")
|
|
|
|
group.REST("/obj/rest", obj)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-06-19 09:06:52 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2019-06-19 09:06:52 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/api/handler"), "Handler")
|
|
|
|
|
|
|
|
t.Assert(client.GetContent(ctx, "/api/obj"), "1Object Index2")
|
|
|
|
t.Assert(client.GetContent(ctx, "/api/obj/"), "1Object Index2")
|
|
|
|
t.Assert(client.GetContent(ctx, "/api/obj/index"), "1Object Index2")
|
|
|
|
t.Assert(client.GetContent(ctx, "/api/obj/delete"), "1Object Delete2")
|
|
|
|
t.Assert(client.GetContent(ctx, "/api/obj/my-show"), "1Object Show2")
|
|
|
|
t.Assert(client.GetContent(ctx, "/api/obj/show"), "1Object Show2")
|
|
|
|
t.Assert(client.DeleteContent(ctx, "/api/obj/rest"), "1Object Delete2")
|
|
|
|
|
|
|
|
t.Assert(client.DeleteContent(ctx, "/ThisDoesNotExist"), "Not Found")
|
|
|
|
t.Assert(client.DeleteContent(ctx, "/api/ThisDoesNotExist"), "Not Found")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2018-12-30 22:02:46 +08:00
|
|
|
}
|
2019-03-01 23:45:55 +08:00
|
|
|
|
|
|
|
func Test_Router_GroupBuildInVar(t *testing.T) {
|
2022-02-22 14:12:09 +08:00
|
|
|
s := g.Server(guid.S())
|
2019-06-19 09:06:52 +08:00
|
|
|
obj := new(GroupObject)
|
|
|
|
// 分组路由方法注册
|
2019-12-02 23:00:48 +08:00
|
|
|
group := s.Group("/api")
|
|
|
|
group.ALL("/{.struct}/{.method}", obj)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-06-19 09:06:52 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2019-06-19 09:06:52 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/api/group-object/index"), "1Object Index2")
|
|
|
|
t.Assert(client.GetContent(ctx, "/api/group-object/delete"), "1Object Delete2")
|
|
|
|
t.Assert(client.GetContent(ctx, "/api/group-object/show"), "1Object Show2")
|
2019-06-19 09:06:52 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.DeleteContent(ctx, "/ThisDoesNotExist"), "Not Found")
|
|
|
|
t.Assert(client.DeleteContent(ctx, "/api/ThisDoesNotExist"), "Not Found")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
|
|
|
}
|
2020-03-11 15:51:24 +08:00
|
|
|
|
2021-07-30 15:15:44 +08:00
|
|
|
func Test_Router_Group_Methods(t *testing.T) {
|
2022-02-22 14:12:09 +08:00
|
|
|
s := g.Server(guid.S())
|
2020-03-11 15:51:24 +08:00
|
|
|
obj := new(GroupObject)
|
|
|
|
group := s.Group("/")
|
|
|
|
group.ALL("/obj", obj, "Show, Delete")
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/obj/show"), "1Object Show2")
|
|
|
|
t.Assert(client.GetContent(ctx, "/obj/delete"), "1Object Delete2")
|
2020-03-11 15:51:24 +08:00
|
|
|
})
|
|
|
|
}
|
2020-03-24 19:48:10 +08:00
|
|
|
|
|
|
|
func Test_Router_Group_MultiServer(t *testing.T) {
|
2022-01-22 15:33:31 +08:00
|
|
|
p1, _ := gtcp.GetFreePort()
|
|
|
|
p2, _ := gtcp.GetFreePort()
|
2020-03-24 19:48:10 +08:00
|
|
|
s1 := g.Server(p1)
|
|
|
|
s2 := g.Server(p2)
|
|
|
|
s1.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.POST("/post", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("post1")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
s2.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.POST("/post", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("post2")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
s1.SetPort(p1)
|
|
|
|
s2.SetPort(p2)
|
|
|
|
s1.SetDumpRouterMap(false)
|
|
|
|
s2.SetDumpRouterMap(false)
|
|
|
|
gtest.Assert(s1.Start(), nil)
|
|
|
|
gtest.Assert(s2.Start(), nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
defer s2.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
c1 := g.Client()
|
2020-03-24 19:48:10 +08:00
|
|
|
c1.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p1))
|
2020-11-25 16:40:45 +08:00
|
|
|
c2 := g.Client()
|
2020-03-24 19:48:10 +08:00
|
|
|
c2.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p2))
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(c1.PostContent(ctx, "/post"), "post1")
|
|
|
|
t.Assert(c2.PostContent(ctx, "/post"), "post2")
|
2020-03-24 19:48:10 +08:00
|
|
|
})
|
|
|
|
}
|
2021-08-19 20:59:08 +08:00
|
|
|
|
|
|
|
func Test_Router_Group_Map(t *testing.T) {
|
|
|
|
testFuncGet := func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("get")
|
|
|
|
}
|
|
|
|
testFuncPost := func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("post")
|
|
|
|
}
|
2022-02-22 14:12:09 +08:00
|
|
|
s := g.Server(guid.S())
|
2021-08-19 20:59:08 +08:00
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.Map(map[string]interface{}{
|
|
|
|
"Get: /test": testFuncGet,
|
|
|
|
"Post:/test": testFuncPost,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
//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()
|
2022-02-22 14:12:09 +08:00
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2021-08-19 20:59:08 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(c.GetContent(ctx, "/test"), "get")
|
|
|
|
t.Assert(c.PostContent(ctx, "/test"), "post")
|
2021-08-19 20:59:08 +08:00
|
|
|
})
|
|
|
|
}
|
2022-02-17 22:24:50 +08:00
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/1609
|
|
|
|
func Test_Issue1609(t *testing.T) {
|
2022-02-22 14:12:09 +08:00
|
|
|
s := g.Server(guid.S())
|
2022-02-17 22:24:50 +08:00
|
|
|
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()
|
2022-02-22 14:12:09 +08:00
|
|
|
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2022-02-17 22:24:50 +08:00
|
|
|
|
|
|
|
t.Assert(c.GetContent(ctx, "/api/get"), "get")
|
|
|
|
t.Assert(c.PostContent(ctx, "/test"), "Not Found")
|
|
|
|
})
|
|
|
|
}
|