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