github.com/gogf/gf/v2@v2.7.4/net/gtrace/gtrace_z_unit_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 "net/http" 12 "strings" 13 "testing" 14 15 "github.com/gogf/gf/v2/encoding/gcompress" 16 17 "github.com/gogf/gf/v2/net/gtrace" 18 "github.com/gogf/gf/v2/test/gtest" 19 "github.com/gogf/gf/v2/text/gstr" 20 ) 21 22 func TestWithTraceID(t *testing.T) { 23 var ( 24 ctx = context.Background() 25 uuid = `a323f910-f690-11ec-963d-79c0b7fcf119` 26 ) 27 gtest.C(t, func(t *gtest.T) { 28 newCtx, err := gtrace.WithTraceID(ctx, uuid) 29 t.AssertNE(err, nil) 30 t.Assert(newCtx, ctx) 31 }) 32 gtest.C(t, func(t *gtest.T) { 33 var traceId = gstr.Replace(uuid, "-", "") 34 newCtx, err := gtrace.WithTraceID(ctx, traceId) 35 t.AssertNil(err) 36 t.AssertNE(newCtx, ctx) 37 t.Assert(gtrace.GetTraceID(ctx), "") 38 t.Assert(gtrace.GetTraceID(newCtx), traceId) 39 }) 40 } 41 42 func TestWithUUID(t *testing.T) { 43 var ( 44 ctx = context.Background() 45 uuid = `a323f910-f690-11ec-963d-79c0b7fcf119` 46 ) 47 gtest.C(t, func(t *gtest.T) { 48 newCtx, err := gtrace.WithTraceID(ctx, uuid) 49 t.AssertNE(err, nil) 50 t.Assert(newCtx, ctx) 51 }) 52 gtest.C(t, func(t *gtest.T) { 53 newCtx, err := gtrace.WithUUID(ctx, uuid) 54 t.AssertNil(err) 55 t.AssertNE(newCtx, ctx) 56 t.Assert(gtrace.GetTraceID(ctx), "") 57 t.Assert(gtrace.GetTraceID(newCtx), gstr.Replace(uuid, "-", "")) 58 }) 59 } 60 61 func TestSafeContent(t *testing.T) { 62 var ( 63 defText = "δΈ" 64 shortData = strings.Repeat(defText, gtrace.MaxContentLogSize()-1) 65 standData = strings.Repeat(defText, gtrace.MaxContentLogSize()) 66 longData = strings.Repeat(defText, gtrace.MaxContentLogSize()+1) 67 header = http.Header{} 68 gzipHeader = http.Header{ 69 "Content-Encoding": []string{"gzip"}, 70 } 71 ) 72 73 // safe content 74 gtest.C(t, func(t *gtest.T) { 75 76 t1, err := gtrace.SafeContentForHttp([]byte(shortData), header) 77 t.AssertNil(err) 78 t.Assert(t1, shortData) 79 t.Assert(gtrace.SafeContent([]byte(shortData)), shortData) 80 81 t2, err := gtrace.SafeContentForHttp([]byte(standData), header) 82 t.AssertNil(err) 83 t.Assert(t2, standData) 84 t.Assert(gtrace.SafeContent([]byte(standData)), standData) 85 86 t3, err := gtrace.SafeContentForHttp([]byte(longData), header) 87 t.AssertNil(err) 88 t.Assert(t3, standData+"...") 89 t.Assert(gtrace.SafeContent([]byte(longData)), standData+"...") 90 }) 91 92 // compress content 93 var ( 94 compressShortData, _ = gcompress.Gzip([]byte(shortData)) 95 compressStandData, _ = gcompress.Gzip([]byte(standData)) 96 compressLongData, _ = gcompress.Gzip([]byte(longData)) 97 ) 98 gtest.C(t, func(t *gtest.T) { 99 100 t1, err := gtrace.SafeContentForHttp(compressShortData, gzipHeader) 101 t.AssertNil(err) 102 t.Assert(t1, shortData) 103 104 t2, err := gtrace.SafeContentForHttp(compressStandData, gzipHeader) 105 t.AssertNil(err) 106 t.Assert(t2, standData) 107 108 t3, err := gtrace.SafeContentForHttp(compressLongData, gzipHeader) 109 t.AssertNil(err) 110 t.Assert(t3, standData+"...") 111 }) 112 }