0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00
caddy/caddyhttp/pprof/pprof_test.go
2016-09-05 10:20:34 -06:00

55 lines
1.2 KiB
Go

package pprof
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
func TestServeHTTP(t *testing.T) {
h := Handler{
Next: httpserver.HandlerFunc(nextHandler),
Mux: NewMux(),
}
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "/debug/pprof", nil)
if err != nil {
t.Fatal(err)
}
status, err := h.ServeHTTP(w, r)
if status != 0 {
t.Errorf("Expected status %d but got %d", 0, status)
}
if err != nil {
t.Errorf("Expected nil error, but got: %v", err)
}
if w.Body.String() == "content" {
t.Errorf("Expected pprof to handle request, but it didn't")
}
w = httptest.NewRecorder()
r, err = http.NewRequest("GET", "/foo", nil)
if err != nil {
t.Fatal(err)
}
status, err = h.ServeHTTP(w, r)
if status != http.StatusNotFound {
t.Errorf("Test two: Expected status %d but got %d", http.StatusNotFound, status)
}
if err != nil {
t.Errorf("Test two: Expected nil error, but got: %v", err)
}
if w.Body.String() != "content" {
t.Errorf("Expected pprof to pass the request through, but it didn't; got: %s", w.Body.String())
}
}
func nextHandler(w http.ResponseWriter, r *http.Request) (int, error) {
fmt.Fprintf(w, "content")
return http.StatusNotFound, nil
}