tracinghandler_test.go 837 B

12345678910111213141516171819202122232425262728293031
  1. package handler
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/tal-tech/go-zero/core/stringx"
  8. "github.com/tal-tech/go-zero/core/trace"
  9. "github.com/tal-tech/go-zero/core/trace/tracespec"
  10. )
  11. func TestTracingHandler(t *testing.T) {
  12. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  13. traceId := stringx.RandId()
  14. req.Header.Set(trace.TraceIdKey, traceId)
  15. handler := TracingHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  16. span, ok := r.Context().Value(tracespec.TracingKey).(tracespec.Trace)
  17. assert.True(t, ok)
  18. assert.Equal(t, traceId, span.TraceId())
  19. }))
  20. resp := httptest.NewRecorder()
  21. handler.ServeHTTP(resp, req)
  22. assert.Equal(t, http.StatusOK, resp.Code)
  23. assert.Equal(t, traceId, resp.Header().Get(trace.TraceIdKey))
  24. }