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