github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_z_unit_feature_error_code_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 // static service testing. 8 9 package ghttp_test 10 11 import ( 12 "fmt" 13 "testing" 14 "time" 15 16 "github.com/gogf/gf/v2/errors/gcode" 17 "github.com/gogf/gf/v2/errors/gerror" 18 "github.com/gogf/gf/v2/frame/g" 19 "github.com/gogf/gf/v2/net/ghttp" 20 "github.com/gogf/gf/v2/test/gtest" 21 "github.com/gogf/gf/v2/util/guid" 22 ) 23 24 func Test_Error_Code(t *testing.T) { 25 gtest.C(t, func(t *gtest.T) { 26 s := g.Server(guid.S()) 27 s.Group("/", func(group *ghttp.RouterGroup) { 28 group.Middleware(func(r *ghttp.Request) { 29 r.Middleware.Next() 30 r.Response.ClearBuffer() 31 r.Response.Write(gerror.Code(r.GetError())) 32 }) 33 group.ALL("/", func(r *ghttp.Request) { 34 panic(gerror.NewCode(gcode.New(10000, "", nil), "test error")) 35 }) 36 }) 37 s.SetDumpRouterMap(false) 38 s.Start() 39 defer s.Shutdown() 40 time.Sleep(100 * time.Millisecond) 41 c := g.Client() 42 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 43 44 t.Assert(c.GetContent(ctx, "/"), "10000") 45 }) 46 }