github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_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/gogf/gf. 6 7 package ghttp_test 8 9 import ( 10 "fmt" 11 "testing" 12 "time" 13 14 "github.com/gogf/gf/frame/g" 15 "github.com/gogf/gf/net/ghttp" 16 "github.com/gogf/gf/test/gtest" 17 ) 18 19 type NamesObject struct{} 20 21 func (o *NamesObject) ShowName(r *ghttp.Request) { 22 r.Response.Write("Object Show Name") 23 } 24 25 func Test_NameToUri_FullName(t *testing.T) { 26 p, _ := ports.PopRand() 27 s := g.Server(p) 28 s.SetNameToUriType(ghttp.URI_TYPE_FULLNAME) 29 s.BindObject("/{.struct}/{.method}", new(NamesObject)) 30 s.SetPort(p) 31 s.SetDumpRouterMap(false) 32 s.Start() 33 defer s.Shutdown() 34 35 time.Sleep(100 * time.Millisecond) 36 gtest.C(t, func(t *gtest.T) { 37 client := g.Client() 38 client.SetBrowserMode(true) 39 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 40 t.Assert(client.GetContent("/"), "Not Found") 41 t.Assert(client.GetContent("/NamesObject"), "Not Found") 42 t.Assert(client.GetContent("/NamesObject/ShowName"), "Object Show Name") 43 }) 44 } 45 46 func Test_NameToUri_AllLower(t *testing.T) { 47 p, _ := ports.PopRand() 48 s := g.Server(p) 49 s.SetNameToUriType(ghttp.URI_TYPE_ALLLOWER) 50 s.BindObject("/{.struct}/{.method}", new(NamesObject)) 51 s.SetPort(p) 52 s.SetDumpRouterMap(false) 53 s.Start() 54 defer s.Shutdown() 55 56 time.Sleep(100 * time.Millisecond) 57 gtest.C(t, func(t *gtest.T) { 58 client := g.Client() 59 client.SetBrowserMode(true) 60 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 61 t.Assert(client.GetContent("/"), "Not Found") 62 t.Assert(client.GetContent("/NamesObject"), "Not Found") 63 t.Assert(client.GetContent("/namesobject/showname"), "Object Show Name") 64 }) 65 } 66 67 func Test_NameToUri_Camel(t *testing.T) { 68 p, _ := ports.PopRand() 69 s := g.Server(p) 70 s.SetNameToUriType(ghttp.URI_TYPE_CAMEL) 71 s.BindObject("/{.struct}/{.method}", new(NamesObject)) 72 s.SetPort(p) 73 s.SetDumpRouterMap(false) 74 s.Start() 75 defer s.Shutdown() 76 77 time.Sleep(100 * time.Millisecond) 78 gtest.C(t, func(t *gtest.T) { 79 client := g.Client() 80 client.SetBrowserMode(true) 81 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 82 t.Assert(client.GetContent("/"), "Not Found") 83 t.Assert(client.GetContent("/NamesObject"), "Not Found") 84 t.Assert(client.GetContent("/namesObject/showName"), "Object Show Name") 85 }) 86 } 87 88 func Test_NameToUri_Default(t *testing.T) { 89 p, _ := ports.PopRand() 90 s := g.Server(p) 91 s.SetNameToUriType(ghttp.UriTypeDefault) 92 s.BindObject("/{.struct}/{.method}", new(NamesObject)) 93 s.SetPort(p) 94 s.SetDumpRouterMap(false) 95 s.Start() 96 defer s.Shutdown() 97 98 time.Sleep(100 * time.Millisecond) 99 gtest.C(t, func(t *gtest.T) { 100 client := g.Client() 101 client.SetBrowserMode(true) 102 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 103 t.Assert(client.GetContent("/"), "Not Found") 104 t.Assert(client.GetContent("/NamesObject"), "Not Found") 105 t.Assert(client.GetContent("/names-object/show-name"), "Object Show Name") 106 }) 107 }