github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_router_hook_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 func Test_Router_Hook_Basic(t *testing.T) { 20 p, _ := ports.PopRand() 21 s := g.Server(p) 22 s.BindHookHandlerByMap("/*", map[string]ghttp.HandlerFunc{ 23 ghttp.HookBeforeServe: func(r *ghttp.Request) { r.Response.Write("1") }, 24 ghttp.HookAfterServe: func(r *ghttp.Request) { r.Response.Write("2") }, 25 ghttp.HookBeforeOutput: func(r *ghttp.Request) { r.Response.Write("3") }, 26 ghttp.HookAfterOutput: func(r *ghttp.Request) { r.Response.Write("4") }, 27 }) 28 s.BindHandler("/test/test", func(r *ghttp.Request) { 29 r.Response.Write("test") 30 }) 31 s.SetPort(p) 32 s.SetDumpRouterMap(false) 33 s.Start() 34 defer s.Shutdown() 35 36 time.Sleep(100 * time.Millisecond) 37 gtest.C(t, func(t *gtest.T) { 38 client := g.Client() 39 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 40 41 t.Assert(client.GetContent("/"), "123") 42 t.Assert(client.GetContent("/test/test"), "1test23") 43 }) 44 } 45 46 func Test_Router_Hook_Fuzzy_Router(t *testing.T) { 47 p, _ := ports.PopRand() 48 s := g.Server(p) 49 i := 1000 50 pattern1 := "/:name/info" 51 s.BindHookHandlerByMap(pattern1, map[string]ghttp.HandlerFunc{ 52 ghttp.HookBeforeServe: func(r *ghttp.Request) { 53 r.SetParam("uid", i) 54 i++ 55 }, 56 }) 57 s.BindHandler(pattern1, func(r *ghttp.Request) { 58 r.Response.Write(r.Get("uid")) 59 }) 60 61 pattern2 := "/{object}/list/{page}.java" 62 s.BindHookHandlerByMap(pattern2, map[string]ghttp.HandlerFunc{ 63 ghttp.HookBeforeOutput: func(r *ghttp.Request) { 64 r.Response.SetBuffer([]byte( 65 fmt.Sprint(r.Get("object"), "&", r.Get("page"), "&", i), 66 )) 67 }, 68 }) 69 s.BindHandler(pattern2, func(r *ghttp.Request) { 70 r.Response.Write(r.Router.Uri) 71 }) 72 s.SetPort(p) 73 s.SetDumpRouterMap(false) 74 s.Start() 75 defer s.Shutdown() 76 77 time.Sleep(100 * time.Millisecond) 78 gtest.C(t, func(t *gtest.T) { 79 client := g.Client() 80 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 81 82 t.Assert(client.GetContent("/john"), "Not Found") 83 t.Assert(client.GetContent("/john/info"), "1000") 84 t.Assert(client.GetContent("/john/info"), "1001") 85 t.Assert(client.GetContent("/john/list/1.java"), "john&1&1002") 86 t.Assert(client.GetContent("/john/list/2.java"), "john&2&1002") 87 }) 88 } 89 90 func Test_Router_Hook_Priority(t *testing.T) { 91 p, _ := ports.PopRand() 92 s := g.Server(p) 93 s.BindHandler("/priority/show", func(r *ghttp.Request) { 94 r.Response.Write("show") 95 }) 96 97 s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc{ 98 ghttp.HookBeforeServe: func(r *ghttp.Request) { 99 r.Response.Write("1") 100 }, 101 }) 102 s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc{ 103 ghttp.HookBeforeServe: func(r *ghttp.Request) { 104 r.Response.Write("2") 105 }, 106 }) 107 s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc{ 108 ghttp.HookBeforeServe: func(r *ghttp.Request) { 109 r.Response.Write("3") 110 }, 111 }) 112 s.SetPort(p) 113 s.SetDumpRouterMap(false) 114 s.Start() 115 defer s.Shutdown() 116 117 time.Sleep(100 * time.Millisecond) 118 gtest.C(t, func(t *gtest.T) { 119 client := g.Client() 120 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 121 122 t.Assert(client.GetContent("/"), "Not Found") 123 t.Assert(client.GetContent("/priority/show"), "312show") 124 t.Assert(client.GetContent("/priority/any/any"), "2") 125 t.Assert(client.GetContent("/priority/name"), "12") 126 }) 127 } 128 129 func Test_Router_Hook_Multi(t *testing.T) { 130 p, _ := ports.PopRand() 131 s := g.Server(p) 132 s.BindHandler("/multi-hook", func(r *ghttp.Request) { 133 r.Response.Write("show") 134 }) 135 136 s.BindHookHandlerByMap("/multi-hook", map[string]ghttp.HandlerFunc{ 137 ghttp.HookBeforeServe: func(r *ghttp.Request) { 138 r.Response.Write("1") 139 }, 140 }) 141 s.BindHookHandlerByMap("/multi-hook", map[string]ghttp.HandlerFunc{ 142 ghttp.HookBeforeServe: func(r *ghttp.Request) { 143 r.Response.Write("2") 144 }, 145 }) 146 s.SetPort(p) 147 s.SetDumpRouterMap(false) 148 s.Start() 149 defer s.Shutdown() 150 151 time.Sleep(100 * time.Millisecond) 152 gtest.C(t, func(t *gtest.T) { 153 client := g.Client() 154 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 155 156 t.Assert(client.GetContent("/"), "Not Found") 157 t.Assert(client.GetContent("/multi-hook"), "12show") 158 }) 159 } 160 161 func Test_Router_Hook_ExitAll(t *testing.T) { 162 p, _ := ports.PopRand() 163 s := g.Server(p) 164 s.BindHandler("/test", func(r *ghttp.Request) { 165 r.Response.Write("test") 166 }) 167 s.Group("/hook", func(group *ghttp.RouterGroup) { 168 group.Middleware(func(r *ghttp.Request) { 169 r.Response.Write("1") 170 r.Middleware.Next() 171 }) 172 group.ALL("/test", func(r *ghttp.Request) { 173 r.Response.Write("2") 174 }) 175 }) 176 177 s.BindHookHandler("/hook/*", ghttp.HookBeforeServe, func(r *ghttp.Request) { 178 r.Response.Write("hook") 179 r.ExitAll() 180 }) 181 s.SetPort(p) 182 s.SetDumpRouterMap(false) 183 s.Start() 184 defer s.Shutdown() 185 186 time.Sleep(100 * time.Millisecond) 187 gtest.C(t, func(t *gtest.T) { 188 client := g.Client() 189 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 190 191 t.Assert(client.GetContent("/test"), "test") 192 t.Assert(client.GetContent("/hook/test"), "hook") 193 }) 194 }