github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_context_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_Context(t *testing.T) {
    21  	s := g.Server(guid.S())
    22  	s.Group("/", func(group *ghttp.RouterGroup) {
    23  		group.Middleware(func(r *ghttp.Request) {
    24  			r.SetCtxVar("traceid", 123)
    25  			r.Middleware.Next()
    26  		})
    27  		group.GET("/", func(r *ghttp.Request) {
    28  			r.Response.Write(r.GetCtxVar("traceid"))
    29  		})
    30  	})
    31  	s.SetDumpRouterMap(false)
    32  	s.Start()
    33  	defer s.Shutdown()
    34  
    35  	time.Sleep(100 * time.Millisecond)
    36  	gtest.C(t, func(t *gtest.T) {
    37  		client := g.Client()
    38  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    39  
    40  		t.Assert(client.GetContent(ctx, "/"), `123`)
    41  	})
    42  }