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
Matthew Holt f23d8cb37f Add {upstream} placeholder when proxy middleware is used (closes #531)
Middlewares can now make their own placeholders that may be useful in
logging, on a per-request basis. Proxy is the first one to do this.
2016-03-20 21:56:13 -06:00

62 lines
1.3 KiB
Go

package log
import (
"bytes"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/mholt/caddy/middleware"
)
type erroringMiddleware struct{}
func (erroringMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if rr, ok := w.(*middleware.ResponseRecorder); ok {
rr.Replacer.Set("testval", "foobar")
}
return http.StatusNotFound, nil
}
func TestLoggedStatus(t *testing.T) {
var f bytes.Buffer
var next erroringMiddleware
rule := Rule{
PathScope: "/",
Format: DefaultLogFormat + " {testval}",
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.Errorf("Expected status to be 0, but was %d", status)
}
if err != nil {
t.Errorf("Expected error to be nil, instead got: %v", err)
}
logged := f.String()
if !strings.Contains(logged, "404 13") {
t.Errorf("Expected log entry to contain '404 13', but it didn't: %s", logged)
}
// check custom placeholder
if !strings.Contains(logged, "foobar") {
t.Errorf("Expected the log entry to contain 'foobar' (custom placeholder), but it didn't: %s", logged)
}
}