github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_router_group_test.go (about) 1 // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf. 6 7 // 分组路由测试 8 package ghttp_test 9 10 import ( 11 "fmt" 12 "github.com/zhongdalu/gf/g" 13 "github.com/zhongdalu/gf/g/frame/gmvc" 14 "github.com/zhongdalu/gf/g/net/ghttp" 15 "github.com/zhongdalu/gf/g/test/gtest" 16 "testing" 17 "time" 18 ) 19 20 // 执行对象 21 type GroupObject struct{} 22 23 func (o *GroupObject) Init(r *ghttp.Request) { 24 r.Response.Write("1") 25 } 26 27 func (o *GroupObject) Shut(r *ghttp.Request) { 28 r.Response.Write("2") 29 } 30 31 func (o *GroupObject) Index(r *ghttp.Request) { 32 r.Response.Write("Object Index") 33 } 34 35 func (o *GroupObject) Show(r *ghttp.Request) { 36 r.Response.Write("Object Show") 37 } 38 39 func (o *GroupObject) Delete(r *ghttp.Request) { 40 r.Response.Write("Object Delete") 41 } 42 43 // 控制器 44 type GroupController struct { 45 gmvc.Controller 46 } 47 48 func (c *GroupController) Init(r *ghttp.Request) { 49 c.Controller.Init(r) 50 c.Response.Write("1") 51 } 52 53 func (c *GroupController) Shut() { 54 c.Response.Write("2") 55 } 56 57 func (c *GroupController) Index() { 58 c.Response.Write("Controller Index") 59 } 60 61 func (c *GroupController) Show() { 62 c.Response.Write("Controller Show") 63 } 64 65 func (c *GroupController) Post() { 66 c.Response.Write("Controller Post") 67 } 68 69 func Handler(r *ghttp.Request) { 70 r.Response.Write("Handler") 71 } 72 73 func Test_Router_GroupBasic1(t *testing.T) { 74 p := ports.PopRand() 75 s := g.Server(p) 76 obj := new(GroupObject) 77 ctl := new(GroupController) 78 // 分组路由方法注册 79 g := s.Group("/api") 80 g.ALL("/handler", Handler) 81 g.ALL("/ctl", ctl) 82 g.GET("/ctl/my-show", ctl, "Show") 83 g.REST("/ctl/rest", ctl) 84 g.ALL("/obj", obj) 85 g.GET("/obj/my-show", obj, "Show") 86 g.REST("/obj/rest", obj) 87 s.SetPort(p) 88 s.SetDumpRouteMap(false) 89 s.Start() 90 defer s.Shutdown() 91 92 time.Sleep(time.Second) 93 gtest.Case(t, func() { 94 client := ghttp.NewClient() 95 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 96 97 gtest.Assert(client.GetContent("/api/handler"), "Handler") 98 99 gtest.Assert(client.GetContent("/api/ctl"), "1Controller Index2") 100 gtest.Assert(client.GetContent("/api/ctl/"), "1Controller Index2") 101 gtest.Assert(client.GetContent("/api/ctl/index"), "1Controller Index2") 102 gtest.Assert(client.GetContent("/api/ctl/my-show"), "1Controller Show2") 103 gtest.Assert(client.GetContent("/api/ctl/post"), "1Controller Post2") 104 gtest.Assert(client.GetContent("/api/ctl/show"), "1Controller Show2") 105 gtest.Assert(client.PostContent("/api/ctl/rest"), "1Controller Post2") 106 107 gtest.Assert(client.GetContent("/api/obj"), "1Object Index2") 108 gtest.Assert(client.GetContent("/api/obj/"), "1Object Index2") 109 gtest.Assert(client.GetContent("/api/obj/index"), "1Object Index2") 110 gtest.Assert(client.GetContent("/api/obj/delete"), "1Object Delete2") 111 gtest.Assert(client.GetContent("/api/obj/my-show"), "1Object Show2") 112 gtest.Assert(client.GetContent("/api/obj/show"), "1Object Show2") 113 gtest.Assert(client.DeleteContent("/api/obj/rest"), "1Object Delete2") 114 115 gtest.Assert(client.DeleteContent("/ThisDoesNotExist"), "Not Found") 116 gtest.Assert(client.DeleteContent("/api/ThisDoesNotExist"), "Not Found") 117 }) 118 } 119 120 func Test_Router_Basic2(t *testing.T) { 121 p := ports.PopRand() 122 s := g.Server(p) 123 obj := new(GroupObject) 124 ctl := new(GroupController) 125 // 分组路由批量注册 126 s.Group("/api").Bind([]ghttp.GroupItem{ 127 {"ALL", "/handler", Handler}, 128 {"ALL", "/ctl", ctl}, 129 {"GET", "/ctl/my-show", ctl, "Show"}, 130 {"REST", "/ctl/rest", ctl}, 131 {"ALL", "/obj", obj}, 132 {"GET", "/obj/my-show", obj, "Show"}, 133 {"REST", "/obj/rest", obj}, 134 }) 135 s.SetPort(p) 136 s.SetDumpRouteMap(false) 137 s.Start() 138 defer s.Shutdown() 139 140 time.Sleep(time.Second) 141 gtest.Case(t, func() { 142 client := ghttp.NewClient() 143 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 144 145 gtest.Assert(client.GetContent("/api/handler"), "Handler") 146 147 gtest.Assert(client.GetContent("/api/ctl/my-show"), "1Controller Show2") 148 gtest.Assert(client.GetContent("/api/ctl/post"), "1Controller Post2") 149 gtest.Assert(client.GetContent("/api/ctl/show"), "1Controller Show2") 150 gtest.Assert(client.PostContent("/api/ctl/rest"), "1Controller Post2") 151 152 gtest.Assert(client.GetContent("/api/obj/delete"), "1Object Delete2") 153 gtest.Assert(client.GetContent("/api/obj/my-show"), "1Object Show2") 154 gtest.Assert(client.GetContent("/api/obj/show"), "1Object Show2") 155 gtest.Assert(client.DeleteContent("/api/obj/rest"), "1Object Delete2") 156 157 gtest.Assert(client.DeleteContent("/ThisDoesNotExist"), "Not Found") 158 gtest.Assert(client.DeleteContent("/api/ThisDoesNotExist"), "Not Found") 159 }) 160 } 161 162 func Test_Router_GroupBuildInVar(t *testing.T) { 163 p := ports.PopRand() 164 s := g.Server(p) 165 obj := new(GroupObject) 166 ctl := new(GroupController) 167 // 分组路由方法注册 168 g := s.Group("/api") 169 g.ALL("/{.struct}/{.method}", ctl) 170 g.ALL("/{.struct}/{.method}", obj) 171 s.SetPort(p) 172 s.SetDumpRouteMap(false) 173 s.Start() 174 defer s.Shutdown() 175 176 time.Sleep(time.Second) 177 gtest.Case(t, func() { 178 client := ghttp.NewClient() 179 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 180 181 gtest.Assert(client.GetContent("/api/group-controller/index"), "1Controller Index2") 182 gtest.Assert(client.GetContent("/api/group-controller/post"), "1Controller Post2") 183 gtest.Assert(client.GetContent("/api/group-controller/show"), "1Controller Show2") 184 185 gtest.Assert(client.GetContent("/api/group-object/index"), "1Object Index2") 186 gtest.Assert(client.GetContent("/api/group-object/delete"), "1Object Delete2") 187 gtest.Assert(client.GetContent("/api/group-object/show"), "1Object Show2") 188 189 gtest.Assert(client.DeleteContent("/ThisDoesNotExist"), "Not Found") 190 gtest.Assert(client.DeleteContent("/api/ThisDoesNotExist"), "Not Found") 191 }) 192 }