tracinghandler_test.go 649 B

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