github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_router_domain_controller_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 package ghttp_test 8 9 import ( 10 "fmt" 11 "github.com/zhongdalu/gf/g" 12 "github.com/zhongdalu/gf/g/frame/gmvc" 13 "github.com/zhongdalu/gf/g/net/ghttp" 14 "github.com/zhongdalu/gf/g/test/gtest" 15 "testing" 16 "time" 17 ) 18 19 type DomainController struct { 20 gmvc.Controller 21 } 22 23 func (c *DomainController) Init(r *ghttp.Request) { 24 c.Controller.Init(r) 25 c.Response.Write("1") 26 } 27 28 func (c *DomainController) Shut() { 29 c.Response.Write("2") 30 } 31 32 func (c *DomainController) Index() { 33 c.Response.Write("Controller Index") 34 } 35 36 func (c *DomainController) Show() { 37 c.Response.Write("Controller Show") 38 } 39 40 func (c *DomainController) Info() { 41 c.Response.Write("Controller Info") 42 } 43 44 func Test_Router_DomainController1(t *testing.T) { 45 p := ports.PopRand() 46 s := g.Server(p) 47 s.Domain("localhost, local").BindController("/", new(DomainController)) 48 s.SetPort(p) 49 s.SetDumpRouteMap(false) 50 s.Start() 51 defer s.Shutdown() 52 53 // 等待启动完成 54 time.Sleep(time.Second) 55 gtest.Case(t, func() { 56 client := ghttp.NewClient() 57 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 58 59 gtest.Assert(client.GetContent("/"), "Not Found") 60 gtest.Assert(client.GetContent("/init"), "Not Found") 61 gtest.Assert(client.GetContent("/shut"), "Not Found") 62 gtest.Assert(client.GetContent("/index"), "Not Found") 63 gtest.Assert(client.GetContent("/show"), "Not Found") 64 gtest.Assert(client.GetContent("/info"), "Not Found") 65 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 66 }) 67 68 gtest.Case(t, func() { 69 client := ghttp.NewClient() 70 client.SetPrefix(fmt.Sprintf("http://localhost:%d", p)) 71 72 gtest.Assert(client.GetContent("/"), "1Controller Index2") 73 gtest.Assert(client.GetContent("/init"), "Not Found") 74 gtest.Assert(client.GetContent("/shut"), "Not Found") 75 gtest.Assert(client.GetContent("/index"), "1Controller Index2") 76 gtest.Assert(client.GetContent("/show"), "1Controller Show2") 77 gtest.Assert(client.GetContent("/info"), "1Controller Info2") 78 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 79 }) 80 81 gtest.Case(t, func() { 82 client := ghttp.NewClient() 83 client.SetPrefix(fmt.Sprintf("http://local:%d", p)) 84 85 gtest.Assert(client.GetContent("/"), "1Controller Index2") 86 gtest.Assert(client.GetContent("/init"), "Not Found") 87 gtest.Assert(client.GetContent("/shut"), "Not Found") 88 gtest.Assert(client.GetContent("/index"), "1Controller Index2") 89 gtest.Assert(client.GetContent("/show"), "1Controller Show2") 90 gtest.Assert(client.GetContent("/info"), "1Controller Info2") 91 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 92 }) 93 } 94 95 func Test_Router_DomainController2(t *testing.T) { 96 p := ports.PopRand() 97 s := g.Server(p) 98 s.Domain("localhost, local").BindController("/controller", new(DomainController), "Show, Info") 99 s.SetPort(p) 100 s.SetDumpRouteMap(false) 101 s.Start() 102 defer s.Shutdown() 103 104 // 等待启动完成 105 time.Sleep(time.Second) 106 gtest.Case(t, func() { 107 client := ghttp.NewClient() 108 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 109 110 gtest.Assert(client.GetContent("/"), "Not Found") 111 gtest.Assert(client.GetContent("/controller"), "Not Found") 112 gtest.Assert(client.GetContent("/controller/init"), "Not Found") 113 gtest.Assert(client.GetContent("/controller/shut"), "Not Found") 114 gtest.Assert(client.GetContent("/controller/index"), "Not Found") 115 gtest.Assert(client.GetContent("/controller/show"), "Not Found") 116 gtest.Assert(client.GetContent("/controller/info"), "Not Found") 117 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 118 }) 119 120 gtest.Case(t, func() { 121 client := ghttp.NewClient() 122 client.SetPrefix(fmt.Sprintf("http://localhost:%d", p)) 123 124 gtest.Assert(client.GetContent("/"), "Not Found") 125 gtest.Assert(client.GetContent("/controller"), "Not Found") 126 gtest.Assert(client.GetContent("/controller/init"), "Not Found") 127 gtest.Assert(client.GetContent("/controller/shut"), "Not Found") 128 gtest.Assert(client.GetContent("/controller/index"), "Not Found") 129 gtest.Assert(client.GetContent("/controller/show"), "1Controller Show2") 130 gtest.Assert(client.GetContent("/controller/info"), "1Controller Info2") 131 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 132 }) 133 134 gtest.Case(t, func() { 135 client := ghttp.NewClient() 136 client.SetPrefix(fmt.Sprintf("http://local:%d", p)) 137 138 gtest.Assert(client.GetContent("/"), "Not Found") 139 gtest.Assert(client.GetContent("/controller"), "Not Found") 140 gtest.Assert(client.GetContent("/controller/init"), "Not Found") 141 gtest.Assert(client.GetContent("/controller/shut"), "Not Found") 142 gtest.Assert(client.GetContent("/controller/index"), "Not Found") 143 gtest.Assert(client.GetContent("/controller/show"), "1Controller Show2") 144 gtest.Assert(client.GetContent("/controller/info"), "1Controller Info2") 145 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 146 }) 147 } 148 149 func Test_Router_DomainControllerMethod(t *testing.T) { 150 p := ports.PopRand() 151 s := g.Server(p) 152 s.Domain("localhost, local").BindControllerMethod("/controller-info", new(DomainController), "Info") 153 s.SetPort(p) 154 s.SetDumpRouteMap(false) 155 s.Start() 156 defer s.Shutdown() 157 158 // 等待启动完成 159 time.Sleep(time.Second) 160 gtest.Case(t, func() { 161 client := ghttp.NewClient() 162 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 163 164 gtest.Assert(client.GetContent("/"), "Not Found") 165 gtest.Assert(client.GetContent("/controller"), "Not Found") 166 gtest.Assert(client.GetContent("/controller/init"), "Not Found") 167 gtest.Assert(client.GetContent("/controller/shut"), "Not Found") 168 gtest.Assert(client.GetContent("/controller/index"), "Not Found") 169 gtest.Assert(client.GetContent("/controller/show"), "Not Found") 170 gtest.Assert(client.GetContent("/controller/info"), "Not Found") 171 gtest.Assert(client.GetContent("/controller-info"), "Not Found") 172 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 173 }) 174 gtest.Case(t, func() { 175 client := ghttp.NewClient() 176 client.SetPrefix(fmt.Sprintf("http://localhost:%d", p)) 177 178 gtest.Assert(client.GetContent("/"), "Not Found") 179 gtest.Assert(client.GetContent("/controller"), "Not Found") 180 gtest.Assert(client.GetContent("/controller/init"), "Not Found") 181 gtest.Assert(client.GetContent("/controller/shut"), "Not Found") 182 gtest.Assert(client.GetContent("/controller/index"), "Not Found") 183 gtest.Assert(client.GetContent("/controller/show"), "Not Found") 184 gtest.Assert(client.GetContent("/controller/info"), "Not Found") 185 gtest.Assert(client.GetContent("/controller-info"), "1Controller Info2") 186 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 187 }) 188 gtest.Case(t, func() { 189 client := ghttp.NewClient() 190 client.SetPrefix(fmt.Sprintf("http://local:%d", p)) 191 192 gtest.Assert(client.GetContent("/"), "Not Found") 193 gtest.Assert(client.GetContent("/controller"), "Not Found") 194 gtest.Assert(client.GetContent("/controller/init"), "Not Found") 195 gtest.Assert(client.GetContent("/controller/shut"), "Not Found") 196 gtest.Assert(client.GetContent("/controller/index"), "Not Found") 197 gtest.Assert(client.GetContent("/controller/show"), "Not Found") 198 gtest.Assert(client.GetContent("/controller/info"), "Not Found") 199 gtest.Assert(client.GetContent("/controller-info"), "1Controller Info2") 200 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 201 }) 202 }