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