package main import ( "fmt" "net/http" "net/http/httptest" "strings" ) func health(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/health" { http.NotFound(w, r) return } if r.Method != http.MethodGet { w.Header().Set("Allow", "GET") http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } w.Header().Set("Content-Type", "text/plain; charset=utf-8") fmt.Fprintln(w, "ready") } func main() { for _, method := range []string{"GET", "POST"} { request := httptest.NewRequest(method, "/health", nil) recorder := httptest.NewRecorder() health(recorder, request) fmt.Println(recorder.Code, strings.TrimSpace(recorder.Body.String())) } }