github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_z_unit_feature_router_group_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package ghttp_test 8 9 import ( 10 "bytes" 11 "fmt" 12 "sync" 13 "testing" 14 "time" 15 16 "github.com/gogf/gf/v2/frame/g" 17 "github.com/gogf/gf/v2/net/ghttp" 18 "github.com/gogf/gf/v2/os/glog" 19 "github.com/gogf/gf/v2/test/gtest" 20 "github.com/gogf/gf/v2/text/gstr" 21 "github.com/gogf/gf/v2/util/guid" 22 ) 23 24 // 执行对象 25 type GroupObject struct{} 26 27 func (o *GroupObject) Init(r *ghttp.Request) { 28 r.Response.Write("1") 29 } 30 31 func (o *GroupObject) Shut(r *ghttp.Request) { 32 r.Response.Write("2") 33 } 34 35 func (o *GroupObject) Index(r *ghttp.Request) { 36 r.Response.Write("Object Index") 37 } 38 39 func (o *GroupObject) Show(r *ghttp.Request) { 40 r.Response.Write("Object Show") 41 } 42 43 func (o *GroupObject) Delete(r *ghttp.Request) { 44 r.Response.Write("Object Delete") 45 } 46 47 func Handler(r *ghttp.Request) { 48 r.Response.Write("Handler") 49 } 50 51 func Test_Router_GroupBasic1(t *testing.T) { 52 s := g.Server(guid.S()) 53 obj := new(GroupObject) 54 // 分组路由方法注册 55 group := s.Group("/api") 56 group.ALL("/handler", Handler) 57 group.ALL("/obj", obj) 58 group.GET("/obj/my-show", obj, "Show") 59 group.REST("/obj/rest", obj) 60 s.SetDumpRouterMap(false) 61 s.Start() 62 defer s.Shutdown() 63 64 time.Sleep(100 * time.Millisecond) 65 gtest.C(t, func(t *gtest.T) { 66 client := g.Client() 67 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 68 69 t.Assert(client.GetContent(ctx, "/api/handler"), "Handler") 70 71 t.Assert(client.GetContent(ctx, "/api/obj"), "1Object Index2") 72 t.Assert(client.GetContent(ctx, "/api/obj/"), "1Object Index2") 73 t.Assert(client.GetContent(ctx, "/api/obj/index"), "1Object Index2") 74 t.Assert(client.GetContent(ctx, "/api/obj/delete"), "1Object Delete2") 75 t.Assert(client.GetContent(ctx, "/api/obj/my-show"), "1Object Show2") 76 t.Assert(client.GetContent(ctx, "/api/obj/show"), "1Object Show2") 77 t.Assert(client.DeleteContent(ctx, "/api/obj/rest"), "1Object Delete2") 78 79 t.Assert(client.DeleteContent(ctx, "/ThisDoesNotExist"), "Not Found") 80 t.Assert(client.DeleteContent(ctx, "/api/ThisDoesNotExist"), "Not Found") 81 }) 82 } 83 84 func Test_Router_GroupBuildInVar(t *testing.T) { 85 s := g.Server(guid.S()) 86 obj := new(GroupObject) 87 // 分组路由方法注册 88 group := s.Group("/api") 89 group.ALL("/{.struct}/{.method}", obj) 90 s.SetDumpRouterMap(false) 91 s.Start() 92 defer s.Shutdown() 93 94 time.Sleep(100 * time.Millisecond) 95 gtest.C(t, func(t *gtest.T) { 96 client := g.Client() 97 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 98 99 t.Assert(client.GetContent(ctx, "/api/group-object/index"), "1Object Index2") 100 t.Assert(client.GetContent(ctx, "/api/group-object/delete"), "1Object Delete2") 101 t.Assert(client.GetContent(ctx, "/api/group-object/show"), "1Object Show2") 102 103 t.Assert(client.DeleteContent(ctx, "/ThisDoesNotExist"), "Not Found") 104 t.Assert(client.DeleteContent(ctx, "/api/ThisDoesNotExist"), "Not Found") 105 }) 106 } 107 108 func Test_Router_Group_Methods(t *testing.T) { 109 s := g.Server(guid.S()) 110 obj := new(GroupObject) 111 group := s.Group("/") 112 group.ALL("/obj", obj, "Show, Delete") 113 s.SetDumpRouterMap(false) 114 s.Start() 115 defer s.Shutdown() 116 117 time.Sleep(100 * time.Millisecond) 118 gtest.C(t, func(t *gtest.T) { 119 client := g.Client() 120 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 121 t.Assert(client.GetContent(ctx, "/obj/show"), "1Object Show2") 122 t.Assert(client.GetContent(ctx, "/obj/delete"), "1Object Delete2") 123 }) 124 } 125 126 func Test_Router_Group_MultiServer(t *testing.T) { 127 s1 := g.Server(guid.S()) 128 s2 := g.Server(guid.S()) 129 s1.Group("/", func(group *ghttp.RouterGroup) { 130 group.POST("/post", func(r *ghttp.Request) { 131 r.Response.Write("post1") 132 }) 133 }) 134 s2.Group("/", func(group *ghttp.RouterGroup) { 135 group.POST("/post", func(r *ghttp.Request) { 136 r.Response.Write("post2") 137 }) 138 }) 139 s1.SetDumpRouterMap(false) 140 s2.SetDumpRouterMap(false) 141 gtest.Assert(s1.Start(), nil) 142 gtest.Assert(s2.Start(), nil) 143 defer s1.Shutdown() 144 defer s2.Shutdown() 145 146 time.Sleep(100 * time.Millisecond) 147 gtest.C(t, func(t *gtest.T) { 148 c1 := g.Client() 149 c1.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s1.GetListenedPort())) 150 c2 := g.Client() 151 c2.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s2.GetListenedPort())) 152 t.Assert(c1.PostContent(ctx, "/post"), "post1") 153 t.Assert(c2.PostContent(ctx, "/post"), "post2") 154 }) 155 } 156 157 func Test_Router_Group_Map(t *testing.T) { 158 testFuncGet := func(r *ghttp.Request) { 159 r.Response.Write("get") 160 } 161 testFuncPost := func(r *ghttp.Request) { 162 r.Response.Write("post") 163 } 164 s := g.Server(guid.S()) 165 s.Group("/", func(group *ghttp.RouterGroup) { 166 group.Map(map[string]interface{}{ 167 "Get: /test": testFuncGet, 168 "Post:/test": testFuncPost, 169 }) 170 }) 171 //s.SetDumpRouterMap(false) 172 gtest.Assert(s.Start(), nil) 173 defer s.Shutdown() 174 175 time.Sleep(100 * time.Millisecond) 176 gtest.C(t, func(t *gtest.T) { 177 c := g.Client() 178 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 179 180 t.Assert(c.GetContent(ctx, "/test"), "get") 181 t.Assert(c.PostContent(ctx, "/test"), "post") 182 }) 183 } 184 185 type SafeBuffer struct { 186 buffer *bytes.Buffer 187 mu sync.Mutex 188 } 189 190 func (b *SafeBuffer) Write(p []byte) (n int, err error) { 191 b.mu.Lock() 192 defer b.mu.Unlock() 193 return b.buffer.Write(p) 194 } 195 196 func (b *SafeBuffer) String() string { 197 b.mu.Lock() 198 defer b.mu.Unlock() 199 return b.buffer.String() 200 } 201 202 func Test_Router_OverWritten(t *testing.T) { 203 var ( 204 s = g.Server(guid.S()) 205 obj = new(GroupObject) 206 buf = &SafeBuffer{ 207 buffer: bytes.NewBuffer(nil), 208 mu: sync.Mutex{}, 209 } 210 logger = glog.NewWithWriter(buf) 211 ) 212 logger.SetStdoutPrint(false) 213 s.SetLogger(logger) 214 s.SetRouteOverWrite(true) 215 s.Group("/api", func(group *ghttp.RouterGroup) { 216 group.ALLMap(g.Map{ 217 "/obj": obj, 218 }) 219 group.ALLMap(g.Map{ 220 "/obj": obj, 221 }) 222 }) 223 s.Start() 224 defer s.Shutdown() 225 226 dumpContent := buf.String() 227 228 time.Sleep(100 * time.Millisecond) 229 gtest.C(t, func(t *gtest.T) { 230 t.Assert(gstr.Count(dumpContent, `/api/obj `), 1) 231 t.Assert(gstr.Count(dumpContent, `/api/obj/index`), 1) 232 t.Assert(gstr.Count(dumpContent, `/api/obj/show`), 1) 233 t.Assert(gstr.Count(dumpContent, `/api/obj/delete`), 1) 234 }) 235 }