github.com/lingyao2333/mo-zero@v1.4.1/rest/handler/prometheushandler_test.go (about)

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