prometheushandler_test.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package handler
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "git.i2edu.net/i2/go-zero/core/prometheus"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestPromMetricHandler_Disabled(t *testing.T) {
  10. promMetricHandler := PrometheusHandler("/user/login")
  11. handler := promMetricHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  12. w.WriteHeader(http.StatusOK)
  13. }))
  14. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  15. resp := httptest.NewRecorder()
  16. handler.ServeHTTP(resp, req)
  17. assert.Equal(t, http.StatusOK, resp.Code)
  18. }
  19. func TestPromMetricHandler_Enabled(t *testing.T) {
  20. prometheus.StartAgent(prometheus.Config{
  21. Host: "localhost",
  22. Path: "/",
  23. })
  24. promMetricHandler := PrometheusHandler("/user/login")
  25. handler := promMetricHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  26. w.WriteHeader(http.StatusOK)
  27. }))
  28. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  29. resp := httptest.NewRecorder()
  30. handler.ServeHTTP(resp, req)
  31. assert.Equal(t, http.StatusOK, resp.Code)
  32. }