github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_status_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 // static service testing. 8 9 package ghttp_test 10 11 import ( 12 "fmt" 13 "testing" 14 "time" 15 16 "github.com/wangyougui/gf/v2/frame/g" 17 "github.com/wangyougui/gf/v2/net/ghttp" 18 "github.com/wangyougui/gf/v2/test/gtest" 19 "github.com/wangyougui/gf/v2/util/guid" 20 ) 21 22 func Test_StatusHandler(t *testing.T) { 23 gtest.C(t, func(t *gtest.T) { 24 s := g.Server(guid.S()) 25 s.BindStatusHandlerByMap(map[int]ghttp.HandlerFunc{ 26 404: func(r *ghttp.Request) { r.Response.WriteOver("404") }, 27 502: func(r *ghttp.Request) { r.Response.WriteOver("502") }, 28 }) 29 s.BindHandler("/502", func(r *ghttp.Request) { 30 r.Response.WriteStatusExit(502) 31 }) 32 s.SetDumpRouterMap(false) 33 s.Start() 34 defer s.Shutdown() 35 time.Sleep(100 * time.Millisecond) 36 client := g.Client() 37 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 38 39 t.Assert(client.GetContent(ctx, "/404"), "404") 40 t.Assert(client.GetContent(ctx, "/502"), "502") 41 }) 42 } 43 44 func Test_StatusHandler_Multi(t *testing.T) { 45 gtest.C(t, func(t *gtest.T) { 46 s := g.Server(guid.S()) 47 s.BindStatusHandler(502, func(r *ghttp.Request) { 48 r.Response.WriteOver("1") 49 }) 50 s.BindStatusHandler(502, func(r *ghttp.Request) { 51 r.Response.Write("2") 52 }) 53 s.BindHandler("/502", func(r *ghttp.Request) { 54 r.Response.WriteStatusExit(502) 55 }) 56 s.SetDumpRouterMap(false) 57 s.Start() 58 defer s.Shutdown() 59 time.Sleep(100 * time.Millisecond) 60 client := g.Client() 61 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 62 63 t.Assert(client.GetContent(ctx, "/502"), "12") 64 }) 65 }