github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_router_exit_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 func Test_Router_Exit(t *testing.T) { 19 p := ports.PopRand() 20 s := g.Server(p) 21 s.BindHookHandlerByMap("/*", map[string]ghttp.HandlerFunc{ 22 "BeforeServe": func(r *ghttp.Request) { r.Response.Write("1") }, 23 "AfterServe": func(r *ghttp.Request) { r.Response.Write("2") }, 24 "BeforeOutput": func(r *ghttp.Request) { r.Response.Write("3") }, 25 "AfterOutput": func(r *ghttp.Request) { r.Response.Write("4") }, 26 "BeforeClose": func(r *ghttp.Request) { r.Response.Write("5") }, 27 "AfterClose": func(r *ghttp.Request) { r.Response.Write("6") }, 28 }) 29 s.BindHandler("/test/test", func(r *ghttp.Request) { 30 r.Response.Write("test-start") 31 r.Exit() 32 r.Response.Write("test-end") 33 }) 34 s.SetPort(p) 35 s.SetDumpRouteMap(false) 36 s.Start() 37 defer s.Shutdown() 38 39 // 等待启动完成 40 time.Sleep(time.Second) 41 gtest.Case(t, func() { 42 client := ghttp.NewClient() 43 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 44 45 gtest.Assert(client.GetContent("/"), "123") 46 gtest.Assert(client.GetContent("/test/test"), "1test-start23") 47 }) 48 } 49 50 func Test_Router_ExitHook(t *testing.T) { 51 p := ports.PopRand() 52 s := g.Server(p) 53 s.BindHandler("/priority/show", func(r *ghttp.Request) { 54 r.Response.Write("show") 55 }) 56 57 s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc{ 58 "BeforeServe": func(r *ghttp.Request) { 59 r.Response.Write("1") 60 }, 61 }) 62 s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc{ 63 "BeforeServe": func(r *ghttp.Request) { 64 r.Response.Write("2") 65 }, 66 }) 67 s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc{ 68 "BeforeServe": func(r *ghttp.Request) { 69 r.Response.Write("3") 70 r.ExitHook() 71 }, 72 }) 73 s.SetPort(p) 74 s.SetDumpRouteMap(false) 75 s.Start() 76 defer s.Shutdown() 77 78 // 等待启动完成 79 time.Sleep(time.Second) 80 gtest.Case(t, func() { 81 client := ghttp.NewClient() 82 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 83 84 gtest.Assert(client.GetContent("/"), "Not Found") 85 gtest.Assert(client.GetContent("/priority/show"), "3show") 86 }) 87 } 88 89 func Test_Router_ExitAll(t *testing.T) { 90 p := ports.PopRand() 91 s := g.Server(p) 92 s.BindHandler("/priority/show", func(r *ghttp.Request) { 93 r.Response.Write("show") 94 }) 95 96 s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc{ 97 "BeforeServe": func(r *ghttp.Request) { 98 r.Response.Write("1") 99 }, 100 }) 101 s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc{ 102 "BeforeServe": func(r *ghttp.Request) { 103 r.Response.Write("2") 104 }, 105 }) 106 s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc{ 107 "BeforeServe": func(r *ghttp.Request) { 108 r.Response.Write("3") 109 r.ExitAll() 110 }, 111 }) 112 s.SetPort(p) 113 s.SetDumpRouteMap(false) 114 s.Start() 115 defer s.Shutdown() 116 117 // 等待启动完成 118 time.Sleep(time.Second) 119 gtest.Case(t, func() { 120 client := ghttp.NewClient() 121 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 122 123 gtest.Assert(client.GetContent("/"), "Not Found") 124 gtest.Assert(client.GetContent("/priority/show"), "3") 125 }) 126 }