github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_router_object_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 Object struct{} 21 22 func (o *Object) Init(r *ghttp.Request) { 23 r.Response.Write("1") 24 } 25 26 func (o *Object) Shut(r *ghttp.Request) { 27 r.Response.Write("2") 28 } 29 30 func (o *Object) Index(r *ghttp.Request) { 31 r.Response.Write("Object Index") 32 } 33 34 func (o *Object) Show(r *ghttp.Request) { 35 r.Response.Write("Object Show") 36 } 37 38 func (o *Object) Info(r *ghttp.Request) { 39 r.Response.Write("Object Info") 40 } 41 42 func Test_Router_Object1(t *testing.T) { 43 s := g.Server(guid.S()) 44 s.BindObject("/", new(Object)) 45 s.BindObject("/{.struct}/{.method}", new(Object)) 46 s.SetDumpRouterMap(false) 47 s.Start() 48 defer s.Shutdown() 49 50 time.Sleep(100 * time.Millisecond) 51 gtest.C(t, func(t *gtest.T) { 52 client := g.Client() 53 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 54 55 t.Assert(client.GetContent(ctx, "/"), "1Object Index2") 56 t.Assert(client.GetContent(ctx, "/init"), "Not Found") 57 t.Assert(client.GetContent(ctx, "/shut"), "Not Found") 58 t.Assert(client.GetContent(ctx, "/index"), "1Object Index2") 59 t.Assert(client.GetContent(ctx, "/show"), "1Object Show2") 60 61 t.Assert(client.GetContent(ctx, "/object"), "Not Found") 62 t.Assert(client.GetContent(ctx, "/object/init"), "Not Found") 63 t.Assert(client.GetContent(ctx, "/object/shut"), "Not Found") 64 t.Assert(client.GetContent(ctx, "/object/index"), "1Object Index2") 65 t.Assert(client.GetContent(ctx, "/object/show"), "1Object Show2") 66 67 t.Assert(client.GetContent(ctx, "/none-exist"), "Not Found") 68 }) 69 } 70 71 func Test_Router_Object2(t *testing.T) { 72 s := g.Server(guid.S()) 73 s.BindObject("/object", new(Object), "Show, Info") 74 s.SetDumpRouterMap(false) 75 s.Start() 76 defer s.Shutdown() 77 78 time.Sleep(100 * time.Millisecond) 79 gtest.C(t, func(t *gtest.T) { 80 client := g.Client() 81 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 82 83 t.Assert(client.GetContent(ctx, "/"), "Not Found") 84 t.Assert(client.GetContent(ctx, "/object"), "Not Found") 85 t.Assert(client.GetContent(ctx, "/object/init"), "Not Found") 86 t.Assert(client.GetContent(ctx, "/object/shut"), "Not Found") 87 t.Assert(client.GetContent(ctx, "/object/index"), "Not Found") 88 t.Assert(client.GetContent(ctx, "/object/show"), "1Object Show2") 89 t.Assert(client.GetContent(ctx, "/object/info"), "1Object Info2") 90 91 t.Assert(client.GetContent(ctx, "/none-exist"), "Not Found") 92 }) 93 } 94 95 func Test_Router_ObjectMethod(t *testing.T) { 96 s := g.Server(guid.S()) 97 s.BindObjectMethod("/object-info", new(Object), "Info") 98 s.SetDumpRouterMap(false) 99 s.Start() 100 defer s.Shutdown() 101 102 time.Sleep(100 * time.Millisecond) 103 gtest.C(t, func(t *gtest.T) { 104 client := g.Client() 105 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 106 107 t.Assert(client.GetContent(ctx, "/"), "Not Found") 108 t.Assert(client.GetContent(ctx, "/object"), "Not Found") 109 t.Assert(client.GetContent(ctx, "/object/init"), "Not Found") 110 t.Assert(client.GetContent(ctx, "/object/shut"), "Not Found") 111 t.Assert(client.GetContent(ctx, "/object/index"), "Not Found") 112 t.Assert(client.GetContent(ctx, "/object/show"), "Not Found") 113 t.Assert(client.GetContent(ctx, "/object/info"), "Not Found") 114 t.Assert(client.GetContent(ctx, "/object-info"), "1Object Info2") 115 116 t.Assert(client.GetContent(ctx, "/none-exist"), "Not Found") 117 }) 118 }