github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_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  	"github.com/gogf/gf/errors/gcode"
    14  	"github.com/gogf/gf/errors/gerror"
    15  	"github.com/gogf/gf/net/ghttp"
    16  	"testing"
    17  	"time"
    18  
    19  	"github.com/gogf/gf/frame/g"
    20  	"github.com/gogf/gf/test/gtest"
    21  )
    22  
    23  func Test_Error_Code(t *testing.T) {
    24  	gtest.C(t, func(t *gtest.T) {
    25  		p, _ := ports.PopRand()
    26  		s := g.Server(p)
    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.SetPort(p)
    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", p))
    43  
    44  		t.Assert(c.GetContent("/"), "10000")
    45  	})
    46  }