github.com/gogf/gf/v2@v2.7.4/net/gtrace/gtrace_z_unit_feature_http_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 package gtrace_test 8 9 import ( 10 "context" 11 "fmt" 12 "testing" 13 "time" 14 15 "github.com/gogf/gf/v2/frame/g" 16 "github.com/gogf/gf/v2/net/ghttp" 17 "github.com/gogf/gf/v2/net/gtrace" 18 "github.com/gogf/gf/v2/os/gctx" 19 "github.com/gogf/gf/v2/test/gtest" 20 ) 21 22 func Test_Client_Server_Tracing(t *testing.T) { 23 gtest.C(t, func(t *gtest.T) { 24 p := 8888 25 s := g.Server(p) 26 s.BindHandler("/", func(r *ghttp.Request) { 27 ctx := r.Context() 28 g.Log().Print(ctx, "GetTraceID:", gtrace.GetTraceID(ctx)) 29 r.Response.Write(gtrace.GetTraceID(ctx)) 30 }) 31 s.SetPort(p) 32 s.SetDumpRouterMap(false) 33 t.AssertNil(s.Start()) 34 defer s.Shutdown() 35 36 time.Sleep(100 * time.Millisecond) 37 38 ctx := gctx.New() 39 40 prefix := fmt.Sprintf("http://127.0.0.1:%d", p) 41 client := g.Client() 42 client.SetPrefix(prefix) 43 t.Assert(gtrace.IsUsingDefaultProvider(), true) 44 t.Assert(client.GetContent(ctx, "/"), gtrace.GetTraceID(ctx)) 45 t.Assert(client.GetContent(ctx, "/"), gctx.CtxId(ctx)) 46 }) 47 } 48 49 func Test_WithTraceID(t *testing.T) { 50 gtest.C(t, func(t *gtest.T) { 51 p := 8889 52 s := g.Server(p) 53 s.BindHandler("/", func(r *ghttp.Request) { 54 ctx := r.Context() 55 r.Response.Write(gtrace.GetTraceID(ctx)) 56 }) 57 s.SetPort(p) 58 s.SetDumpRouterMap(false) 59 t.AssertNil(s.Start()) 60 defer s.Shutdown() 61 62 time.Sleep(100 * time.Millisecond) 63 64 ctx, err := gtrace.WithTraceID(context.TODO(), traceID.String()) 65 t.AssertNil(err) 66 67 prefix := fmt.Sprintf("http://127.0.0.1:%d", p) 68 client := g.Client() 69 client.SetPrefix(prefix) 70 t.Assert(gtrace.IsUsingDefaultProvider(), true) 71 t.Assert(client.GetContent(ctx, "/"), gtrace.GetTraceID(ctx)) 72 t.Assert(client.GetContent(ctx, "/"), traceIDStr) 73 }) 74 }