header_test.go 768 字节
Newer Older
O
ob-robot 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
package trace

import (
	"net/http"
	"testing"

	"github.com/stretchr/testify/assert"

	agentlog "github.com/oceanbase/obagent/log"
)

func TestGetTraceId_WithTraceId(t *testing.T) {
	req := &http.Request{
		Header: http.Header{},
	}
	traceId := "abcdefg"
	req.Header.Add(TraceIdHeader, traceId)
	if got := GetTraceId(req); got != traceId {
		t.Errorf("GetTraceId() = %v, want %v", got, traceId)
	}

	ctx := ContextWithTraceId(req)
	val := ctx.Value(agentlog.TraceIdKey{})
	assert.NotNil(t, val)
	ctxTraceId, _ := val.(string)
	assert.Equal(t, traceId, ctxTraceId)
}

func TestGetTraceId_GetRandomTraceId(t *testing.T) {
	req := &http.Request{
		Header: http.Header{},
	}
	if got := GetTraceId(req); got == "" {
		t.Errorf("GetTraceId() = %v, want not nil", got)
	}
}