github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_router_exit_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_Exit(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-start") 30 r.Exit() 31 r.Response.Write("test-end") 32 }) 33 s.SetDumpRouterMap(false) 34 s.Start() 35 defer s.Shutdown() 36 37 time.Sleep(100 * time.Millisecond) 38 gtest.C(t, func(t *gtest.T) { 39 client := g.Client() 40 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 41 42 t.Assert(client.GetContent(ctx, "/"), "123") 43 t.Assert(client.GetContent(ctx, "/test/test"), "1test-start23") 44 }) 45 } 46 47 func Test_Router_ExitHook(t *testing.T) { 48 s := g.Server(guid.S()) 49 s.BindHandler("/priority/show", func(r *ghttp.Request) { 50 r.Response.Write("show") 51 }) 52 53 s.BindHookHandlerByMap("/priority/:name", map[ghttp.HookName]ghttp.HandlerFunc{ 54 ghttp.HookBeforeServe: func(r *ghttp.Request) { 55 r.Response.Write("1") 56 }, 57 }) 58 s.BindHookHandlerByMap("/priority/*any", map[ghttp.HookName]ghttp.HandlerFunc{ 59 ghttp.HookBeforeServe: func(r *ghttp.Request) { 60 r.Response.Write("2") 61 }, 62 }) 63 s.BindHookHandlerByMap("/priority/show", map[ghttp.HookName]ghttp.HandlerFunc{ 64 ghttp.HookBeforeServe: func(r *ghttp.Request) { 65 r.Response.Write("3") 66 r.ExitHook() 67 }, 68 }) 69 s.SetDumpRouterMap(false) 70 s.Start() 71 defer s.Shutdown() 72 73 time.Sleep(100 * time.Millisecond) 74 gtest.C(t, func(t *gtest.T) { 75 client := g.Client() 76 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 77 78 t.Assert(client.GetContent(ctx, "/"), "Not Found") 79 t.Assert(client.GetContent(ctx, "/priority/show"), "3show") 80 }) 81 } 82 83 func Test_Router_ExitAll(t *testing.T) { 84 s := g.Server(guid.S()) 85 s.BindHandler("/priority/show", func(r *ghttp.Request) { 86 r.Response.Write("show") 87 }) 88 89 s.BindHookHandlerByMap("/priority/:name", map[ghttp.HookName]ghttp.HandlerFunc{ 90 ghttp.HookBeforeServe: func(r *ghttp.Request) { 91 r.Response.Write("1") 92 }, 93 }) 94 s.BindHookHandlerByMap("/priority/*any", map[ghttp.HookName]ghttp.HandlerFunc{ 95 ghttp.HookBeforeServe: func(r *ghttp.Request) { 96 r.Response.Write("2") 97 }, 98 }) 99 s.BindHookHandlerByMap("/priority/show", map[ghttp.HookName]ghttp.HandlerFunc{ 100 ghttp.HookBeforeServe: func(r *ghttp.Request) { 101 r.Response.Write("3") 102 r.ExitAll() 103 }, 104 }) 105 s.SetDumpRouterMap(false) 106 s.Start() 107 defer s.Shutdown() 108 109 time.Sleep(100 * time.Millisecond) 110 gtest.C(t, func(t *gtest.T) { 111 client := g.Client() 112 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 113 114 t.Assert(client.GetContent(ctx, "/"), "Not Found") 115 t.Assert(client.GetContent(ctx, "/priority/show"), "3") 116 }) 117 }