github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_router_names_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/net/ghttp" 16 "github.com/wangyougui/gf/v2/test/gtest" 17 "github.com/wangyougui/gf/v2/util/guid" 18 ) 19 20 type NamesObject struct{} 21 22 func (o *NamesObject) ShowName(r *ghttp.Request) { 23 r.Response.Write("Object Show Name") 24 } 25 26 func Test_NameToUri_FullName(t *testing.T) { 27 s := g.Server(guid.S()) 28 s.SetNameToUriType(ghttp.UriTypeFullName) 29 s.BindObject("/{.struct}/{.method}", new(NamesObject)) 30 s.SetDumpRouterMap(false) 31 s.Start() 32 defer s.Shutdown() 33 34 time.Sleep(100 * time.Millisecond) 35 gtest.C(t, func(t *gtest.T) { 36 client := g.Client() 37 client.SetBrowserMode(true) 38 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 39 t.Assert(client.GetContent(ctx, "/"), "Not Found") 40 t.Assert(client.GetContent(ctx, "/NamesObject"), "Not Found") 41 t.Assert(client.GetContent(ctx, "/NamesObject/ShowName"), "Object Show Name") 42 }) 43 } 44 45 func Test_NameToUri_AllLower(t *testing.T) { 46 s := g.Server(guid.S()) 47 s.SetNameToUriType(ghttp.UriTypeAllLower) 48 s.BindObject("/{.struct}/{.method}", new(NamesObject)) 49 s.SetDumpRouterMap(false) 50 s.Start() 51 defer s.Shutdown() 52 53 time.Sleep(100 * time.Millisecond) 54 gtest.C(t, func(t *gtest.T) { 55 client := g.Client() 56 client.SetBrowserMode(true) 57 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 58 t.Assert(client.GetContent(ctx, "/"), "Not Found") 59 t.Assert(client.GetContent(ctx, "/NamesObject"), "Not Found") 60 t.Assert(client.GetContent(ctx, "/namesobject/showname"), "Object Show Name") 61 }) 62 } 63 64 func Test_NameToUri_Camel(t *testing.T) { 65 s := g.Server(guid.S()) 66 s.SetNameToUriType(ghttp.UriTypeCamel) 67 s.BindObject("/{.struct}/{.method}", new(NamesObject)) 68 s.SetDumpRouterMap(false) 69 s.Start() 70 defer s.Shutdown() 71 72 time.Sleep(100 * time.Millisecond) 73 gtest.C(t, func(t *gtest.T) { 74 client := g.Client() 75 client.SetBrowserMode(true) 76 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 77 t.Assert(client.GetContent(ctx, "/"), "Not Found") 78 t.Assert(client.GetContent(ctx, "/NamesObject"), "Not Found") 79 t.Assert(client.GetContent(ctx, "/namesObject/showName"), "Object Show Name") 80 }) 81 } 82 83 func Test_NameToUri_Default(t *testing.T) { 84 s := g.Server(guid.S()) 85 s.SetNameToUriType(ghttp.UriTypeDefault) 86 s.BindObject("/{.struct}/{.method}", new(NamesObject)) 87 s.SetDumpRouterMap(false) 88 s.Start() 89 defer s.Shutdown() 90 91 time.Sleep(100 * time.Millisecond) 92 gtest.C(t, func(t *gtest.T) { 93 client := g.Client() 94 client.SetBrowserMode(true) 95 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 96 t.Assert(client.GetContent(ctx, "/"), "Not Found") 97 t.Assert(client.GetContent(ctx, "/NamesObject"), "Not Found") 98 t.Assert(client.GetContent(ctx, "/names-object/show-name"), "Object Show Name") 99 }) 100 }