2020-09-17 13:01:20 -05:00
|
|
|
package caddyhttp
|
|
|
|
|
|
|
|
import (
|
2020-09-17 22:46:24 -05:00
|
|
|
"context"
|
2020-09-17 13:01:20 -05:00
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus/testutil"
|
|
|
|
)
|
|
|
|
|
2020-09-17 22:46:24 -05:00
|
|
|
func TestServerNameFromContext(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
expected := "UNKNOWN"
|
|
|
|
if actual := serverNameFromContext(ctx); actual != expected {
|
|
|
|
t.Errorf("Not equal: expected %q, but got %q", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
in := "foo"
|
|
|
|
ctx = context.WithValue(ctx, ServerCtxKey, &Server{name: in})
|
|
|
|
if actual := serverNameFromContext(ctx); actual != in {
|
|
|
|
t.Errorf("Not equal: expected %q, but got %q", in, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 13:01:20 -05:00
|
|
|
func TestMetricsInstrumentedHandler(t *testing.T) {
|
|
|
|
handlerErr := errors.New("oh noes")
|
|
|
|
response := []byte("hello world!")
|
|
|
|
h := HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
if actual := testutil.ToFloat64(httpMetrics.requestInFlight); actual != 1.0 {
|
|
|
|
t.Errorf("Not same: expected %#v, but got %#v", 1.0, actual)
|
|
|
|
}
|
|
|
|
if handlerErr == nil {
|
|
|
|
w.Write(response)
|
|
|
|
}
|
|
|
|
return handlerErr
|
|
|
|
})
|
|
|
|
|
|
|
|
mh := middlewareHandlerFunc(func(w http.ResponseWriter, r *http.Request, h Handler) error {
|
|
|
|
return h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
|
2020-09-17 22:46:24 -05:00
|
|
|
ih := newMetricsInstrumentedHandler("bar", mh)
|
2020-09-17 13:01:20 -05:00
|
|
|
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
if actual := ih.ServeHTTP(w, r, h); actual != handlerErr {
|
|
|
|
t.Errorf("Not same: expected %#v, but got %#v", handlerErr, actual)
|
|
|
|
}
|
|
|
|
if actual := testutil.ToFloat64(httpMetrics.requestInFlight); actual != 0.0 {
|
|
|
|
t.Errorf("Not same: expected %#v, but got %#v", 0.0, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
handlerErr = nil
|
|
|
|
if err := ih.ServeHTTP(w, r, h); err != nil {
|
|
|
|
t.Errorf("Received unexpected error: %w", err)
|
|
|
|
}
|
2020-09-21 14:42:47 -05:00
|
|
|
|
|
|
|
// an empty handler - no errors, no header written
|
|
|
|
mh = middlewareHandlerFunc(func(w http.ResponseWriter, r *http.Request, h Handler) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
ih = newMetricsInstrumentedHandler("empty", mh)
|
|
|
|
r = httptest.NewRequest("GET", "/", nil)
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
|
|
|
|
if err := ih.ServeHTTP(w, r, h); err != nil {
|
|
|
|
t.Errorf("Received unexpected error: %w", err)
|
|
|
|
}
|
|
|
|
if actual := w.Result().StatusCode; actual != 200 {
|
|
|
|
t.Errorf("Not same: expected status code %#v, but got %#v", 200, actual)
|
|
|
|
}
|
|
|
|
if actual := w.Result().Header; len(actual) != 0 {
|
|
|
|
t.Errorf("Not empty: expected headers to be empty, but got %#v", actual)
|
|
|
|
}
|
2020-09-17 13:01:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type middlewareHandlerFunc func(http.ResponseWriter, *http.Request, Handler) error
|
|
|
|
|
|
|
|
func (f middlewareHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request, h Handler) error {
|
|
|
|
return f(w, r, h)
|
|
|
|
}
|