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 thru, 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
}