github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_router_domain_basic_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/wangyougui/gf. 6 7 package ghttp_test 8 9 import ( 10 "fmt" 11 "testing" 12 "time" 13 14 "github.com/wangyougui/gf/v2/frame/g" 15 "github.com/wangyougui/gf/v2/internal/intlog" 16 "github.com/wangyougui/gf/v2/net/ghttp" 17 "github.com/wangyougui/gf/v2/test/gtest" 18 "github.com/wangyougui/gf/v2/util/guid" 19 ) 20 21 func Test_Router_DomainBasic(t *testing.T) { 22 s := g.Server(guid.S()) 23 d := s.Domain("localhost, local") 24 d.BindHandler("/:name", func(r *ghttp.Request) { 25 r.Response.Write("/:name") 26 }) 27 d.BindHandler("/:name/update", func(r *ghttp.Request) { 28 r.Response.Write(r.Get("name")) 29 }) 30 d.BindHandler("/:name/:action", func(r *ghttp.Request) { 31 r.Response.Write(r.Get("action")) 32 }) 33 d.BindHandler("/:name/*any", func(r *ghttp.Request) { 34 r.Response.Write(r.Get("any")) 35 }) 36 d.BindHandler("/user/list/{field}.html", func(r *ghttp.Request) { 37 r.Response.Write(r.Get("field")) 38 }) 39 s.SetDumpRouterMap(false) 40 s.Start() 41 defer s.Shutdown() 42 43 time.Sleep(100 * time.Millisecond) 44 gtest.C(t, func(t *gtest.T) { 45 client := g.Client() 46 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 47 t.Assert(client.GetContent(ctx, "/john"), "Not Found") 48 t.Assert(client.GetContent(ctx, "/john/update"), "Not Found") 49 t.Assert(client.GetContent(ctx, "/john/edit"), "Not Found") 50 t.Assert(client.GetContent(ctx, "/user/list/100.html"), "Not Found") 51 }) 52 gtest.C(t, func(t *gtest.T) { 53 client := g.Client() 54 client.SetPrefix(fmt.Sprintf("http://localhost:%d", s.GetListenedPort())) 55 t.Assert(client.GetContent(ctx, "/john"), "") 56 t.Assert(client.GetContent(ctx, "/john/update"), "john") 57 t.Assert(client.GetContent(ctx, "/john/edit"), "edit") 58 t.Assert(client.GetContent(ctx, "/user/list/100.html"), "100") 59 }) 60 gtest.C(t, func(t *gtest.T) { 61 client := g.Client() 62 client.SetPrefix(fmt.Sprintf("http://local:%d", s.GetListenedPort())) 63 t.Assert(client.GetContent(ctx, "/john"), "") 64 t.Assert(client.GetContent(ctx, "/john/update"), "john") 65 t.Assert(client.GetContent(ctx, "/john/edit"), "edit") 66 t.Assert(client.GetContent(ctx, "/user/list/100.html"), "100") 67 }) 68 } 69 70 func Test_Router_DomainMethod(t *testing.T) { 71 s := g.Server(guid.S()) 72 d := s.Domain("localhost, local") 73 d.BindHandler("GET:/get", func(r *ghttp.Request) { 74 75 }) 76 d.BindHandler("POST:/post", func(r *ghttp.Request) { 77 78 }) 79 s.SetDumpRouterMap(false) 80 s.Start() 81 defer s.Shutdown() 82 83 time.Sleep(100 * time.Millisecond) 84 gtest.C(t, func(t *gtest.T) { 85 client := g.Client() 86 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 87 88 resp1, err := client.Get(ctx, "/get") 89 t.AssertNil(err) 90 defer resp1.Close() 91 t.Assert(resp1.StatusCode, 404) 92 93 resp2, err := client.Post(ctx, "/get") 94 t.AssertNil(err) 95 defer resp2.Close() 96 t.Assert(resp2.StatusCode, 404) 97 98 resp3, err := client.Get(ctx, "/post") 99 t.AssertNil(err) 100 defer resp3.Close() 101 t.Assert(resp3.StatusCode, 404) 102 103 resp4, err := client.Post(ctx, "/post") 104 t.AssertNil(err) 105 defer resp4.Close() 106 t.Assert(resp4.StatusCode, 404) 107 }) 108 109 gtest.C(t, func(t *gtest.T) { 110 client := g.Client() 111 client.SetPrefix(fmt.Sprintf("http://localhost:%d", s.GetListenedPort())) 112 113 resp1, err := client.Get(ctx, "/get") 114 t.AssertNil(err) 115 defer resp1.Close() 116 t.Assert(resp1.StatusCode, 200) 117 118 resp2, err := client.Post(ctx, "/get") 119 t.AssertNil(err) 120 defer resp2.Close() 121 t.Assert(resp2.StatusCode, 404) 122 123 resp3, err := client.Get(ctx, "/post") 124 t.AssertNil(err) 125 defer resp3.Close() 126 t.Assert(resp3.StatusCode, 404) 127 128 resp4, err := client.Post(ctx, "/post") 129 t.AssertNil(err) 130 defer resp4.Close() 131 t.Assert(resp4.StatusCode, 200) 132 }) 133 134 gtest.C(t, func(t *gtest.T) { 135 client := g.Client() 136 client.SetPrefix(fmt.Sprintf("http://local:%d", s.GetListenedPort())) 137 138 resp1, err := client.Get(ctx, "/get") 139 t.AssertNil(err) 140 defer resp1.Close() 141 t.Assert(resp1.StatusCode, 200) 142 143 resp2, err := client.Post(ctx, "/get") 144 t.AssertNil(err) 145 defer resp2.Close() 146 t.Assert(resp2.StatusCode, 404) 147 148 resp3, err := client.Get(ctx, "/post") 149 t.AssertNil(err) 150 defer resp3.Close() 151 t.Assert(resp3.StatusCode, 404) 152 153 resp4, err := client.Post(ctx, "/post") 154 t.AssertNil(err) 155 defer resp4.Close() 156 t.Assert(resp4.StatusCode, 200) 157 }) 158 } 159 160 func Test_Router_DomainStatus(t *testing.T) { 161 s := g.Server(guid.S()) 162 d := s.Domain("localhost, local") 163 d.BindHandler("/200", func(r *ghttp.Request) { 164 r.Response.WriteStatus(200) 165 }) 166 d.BindHandler("/300", func(r *ghttp.Request) { 167 r.Response.WriteStatus(300) 168 }) 169 d.BindHandler("/400", func(r *ghttp.Request) { 170 r.Response.WriteStatus(400) 171 }) 172 d.BindHandler("/500", func(r *ghttp.Request) { 173 r.Response.WriteStatus(500) 174 }) 175 s.SetDumpRouterMap(false) 176 s.Start() 177 defer s.Shutdown() 178 179 time.Sleep(100 * time.Millisecond) 180 gtest.C(t, func(t *gtest.T) { 181 client := g.Client() 182 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 183 184 resp1, err := client.Get(ctx, "/200") 185 t.AssertNil(err) 186 defer resp1.Close() 187 t.Assert(resp1.StatusCode, 404) 188 189 resp2, err := client.Get(ctx, "/300") 190 t.AssertNil(err) 191 defer resp2.Close() 192 t.Assert(resp2.StatusCode, 404) 193 194 resp3, err := client.Get(ctx, "/400") 195 t.AssertNil(err) 196 defer resp3.Close() 197 t.Assert(resp3.StatusCode, 404) 198 199 resp4, err := client.Get(ctx, "/500") 200 t.AssertNil(err) 201 defer resp4.Close() 202 t.Assert(resp4.StatusCode, 404) 203 }) 204 gtest.C(t, func(t *gtest.T) { 205 client := g.Client() 206 client.SetPrefix(fmt.Sprintf("http://localhost:%d", s.GetListenedPort())) 207 208 resp1, err := client.Get(ctx, "/200") 209 t.AssertNil(err) 210 defer resp1.Close() 211 t.Assert(resp1.StatusCode, 200) 212 213 resp2, err := client.Get(ctx, "/300") 214 t.AssertNil(err) 215 defer resp2.Close() 216 t.Assert(resp2.StatusCode, 300) 217 218 resp3, err := client.Get(ctx, "/400") 219 t.AssertNil(err) 220 defer resp3.Close() 221 t.Assert(resp3.StatusCode, 400) 222 223 resp4, err := client.Get(ctx, "/500") 224 t.AssertNil(err) 225 defer resp4.Close() 226 t.Assert(resp4.StatusCode, 500) 227 }) 228 gtest.C(t, func(t *gtest.T) { 229 client := g.Client() 230 client.SetPrefix(fmt.Sprintf("http://local:%d", s.GetListenedPort())) 231 232 resp1, err := client.Get(ctx, "/200") 233 t.AssertNil(err) 234 defer resp1.Close() 235 t.Assert(resp1.StatusCode, 200) 236 237 resp2, err := client.Get(ctx, "/300") 238 t.AssertNil(err) 239 defer resp2.Close() 240 t.Assert(resp2.StatusCode, 300) 241 242 resp3, err := client.Get(ctx, "/400") 243 t.AssertNil(err) 244 defer resp3.Close() 245 t.Assert(resp3.StatusCode, 400) 246 247 resp4, err := client.Get(ctx, "/500") 248 t.AssertNil(err) 249 defer resp4.Close() 250 t.Assert(resp4.StatusCode, 500) 251 }) 252 } 253 254 func Test_Router_DomainCustomStatusHandler(t *testing.T) { 255 s := g.Server(guid.S()) 256 d := s.Domain("localhost, local") 257 d.BindHandler("/", func(r *ghttp.Request) { 258 r.Response.Write("hello") 259 }) 260 d.BindStatusHandler(404, func(r *ghttp.Request) { 261 r.Response.Write("404 page") 262 }) 263 s.SetDumpRouterMap(false) 264 s.Start() 265 defer s.Shutdown() 266 267 time.Sleep(100 * time.Millisecond) 268 gtest.C(t, func(t *gtest.T) { 269 client := g.Client() 270 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 271 272 t.Assert(client.GetContent(ctx, "/"), "Not Found") 273 t.Assert(client.GetContent(ctx, "/ThisDoesNotExist"), "Not Found") 274 }) 275 gtest.C(t, func(t *gtest.T) { 276 client := g.Client() 277 client.SetPrefix(fmt.Sprintf("http://localhost:%d", s.GetListenedPort())) 278 279 t.Assert(client.GetContent(ctx, "/"), "hello") 280 t.Assert(client.GetContent(ctx, "/ThisDoesNotExist"), "404 page") 281 }) 282 gtest.C(t, func(t *gtest.T) { 283 client := g.Client() 284 client.SetPrefix(fmt.Sprintf("http://local:%d", s.GetListenedPort())) 285 286 t.Assert(client.GetContent(ctx, "/"), "hello") 287 t.Assert(client.GetContent(ctx, "/ThisDoesNotExist"), "404 page") 288 }) 289 } 290 291 func Test_Router_Domain404(t *testing.T) { 292 s := g.Server(guid.S()) 293 d := s.Domain("localhost, local") 294 d.BindHandler("/", func(r *ghttp.Request) { 295 r.Response.Write("hello") 296 }) 297 s.SetDumpRouterMap(false) 298 s.Start() 299 defer s.Shutdown() 300 301 time.Sleep(100 * time.Millisecond) 302 gtest.C(t, func(t *gtest.T) { 303 client := g.Client() 304 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 305 306 t.Assert(client.GetContent(ctx, "/"), "Not Found") 307 }) 308 gtest.C(t, func(t *gtest.T) { 309 client := g.Client() 310 client.SetPrefix(fmt.Sprintf("http://localhost:%d", s.GetListenedPort())) 311 312 t.Assert(client.GetContent(ctx, "/"), "hello") 313 }) 314 gtest.C(t, func(t *gtest.T) { 315 client := g.Client() 316 client.SetPrefix(fmt.Sprintf("http://local:%d", s.GetListenedPort())) 317 318 t.Assert(client.GetContent(ctx, "/"), "hello") 319 }) 320 } 321 322 func Test_Router_DomainGroup(t *testing.T) { 323 s := g.Server(guid.S()) 324 d := s.Domain("localhost, local") 325 d.Group("/", func(group *ghttp.RouterGroup) { 326 group.Group("/app", func(group *ghttp.RouterGroup) { 327 group.GET("/{table}/list/{page}.html", func(r *ghttp.Request) { 328 intlog.Print(r.Context(), "/{table}/list/{page}.html") 329 r.Response.Write(r.Get("table"), "&", r.Get("page")) 330 }) 331 group.GET("/order/info/{order_id}", func(r *ghttp.Request) { 332 intlog.Print(r.Context(), "/order/info/{order_id}") 333 r.Response.Write(r.Get("order_id")) 334 }) 335 group.DELETE("/comment/{id}", func(r *ghttp.Request) { 336 intlog.Print(r.Context(), "/comment/{id}") 337 r.Response.Write(r.Get("id")) 338 }) 339 }) 340 }) 341 s.SetDumpRouterMap(false) 342 s.Start() 343 defer s.Shutdown() 344 345 time.Sleep(100 * time.Millisecond) 346 gtest.C(t, func(t *gtest.T) { 347 client1 := g.Client() 348 client1.SetPrefix(fmt.Sprintf("http://local:%d", s.GetListenedPort())) 349 350 client2 := g.Client() 351 client2.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 352 353 t.Assert(client1.GetContent(ctx, "/app/t/list/2.html"), "t&2") 354 t.Assert(client2.GetContent(ctx, "/app/t/list/2.html"), "Not Found") 355 356 t.Assert(client1.GetContent(ctx, "/app/order/info/2"), "2") 357 t.Assert(client2.GetContent(ctx, "/app/order/info/2"), "Not Found") 358 359 t.Assert(client1.GetContent(ctx, "/app/comment/20"), "Not Found") 360 t.Assert(client2.GetContent(ctx, "/app/comment/20"), "Not Found") 361 362 t.Assert(client1.DeleteContent(ctx, "/app/comment/20"), "20") 363 t.Assert(client2.DeleteContent(ctx, "/app/comment/20"), "Not Found") 364 }) 365 }