0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00
caddy/middleware/log/log_test.go
Simon Jefford 6c1f2af53a log: ensure the correct status is always logged
in the case of error (>=400) then no response may have been sent
2015-06-14 21:00:27 +01:00

48 lines
865 B
Go

package log
import (
"bytes"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type erroringMiddleware struct{}
func (erroringMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
return http.StatusNotFound, nil
}
func TestLoggedStatus(t *testing.T) {
var f bytes.Buffer
var next erroringMiddleware
rule := Rule{
PathScope: "/",
Format: DefaultLogFormat,
Log: log.New(&f, "", 0),
}
logger := Logger{
Rules: []Rule{rule},
Next: next,
}
r, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
status, err := logger.ServeHTTP(rec, r)
if status != 0 {
t.Error("Expected status to be 0 - was", status)
}
logged := f.String()
if !strings.Contains(logged, "404 13") {
t.Error("Expected 404 to be logged. Logged string -", logged)
}
}