You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/rest/handler/tracinghandler_test.go

32 lines
837 B
Go

4 years ago
package handler
4 years ago
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
3 years ago
"github.com/tal-tech/go-zero/core/stringx"
"github.com/tal-tech/go-zero/core/trace"
"github.com/tal-tech/go-zero/core/trace/tracespec"
4 years ago
)
func TestTracingHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
traceId := stringx.RandId()
req.Header.Set(trace.TraceIdKey, traceId)
4 years ago
handler := TracingHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
span, ok := r.Context().Value(tracespec.TracingKey).(tracespec.Trace)
assert.True(t, ok)
assert.Equal(t, traceId, span.TraceId())
4 years ago
}))
resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
4 years ago
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, traceId, resp.Header().Get(trace.TraceIdKey))
4 years ago
}