oauth2_proxy/logging_handler_test.go

47 lines
1.0 KiB
Go
Raw Normal View History

2017-07-14 11:51:16 +00:00
package main
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/pusher/oauth2_proxy/logger"
2017-07-14 11:51:16 +00:00
)
func TestLoggingHandler_ServeHTTP(t *testing.T) {
ts := time.Now()
tests := []struct {
Format,
ExpectedLogMessage string
}{
{logger.DefaultRequestLoggingFormat, fmt.Sprintf("127.0.0.1 - - [%s] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n", logger.FormatTimestamp(ts))},
2017-07-14 11:51:16 +00:00
{"{{.RequestMethod}}", "GET\n"},
}
for _, test := range tests {
buf := bytes.NewBuffer(nil)
handler := func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("test"))
}
logger.SetOutput(buf)
logger.SetReqTemplate(test.Format)
h := LoggingHandler(http.HandlerFunc(handler))
2017-07-14 11:51:16 +00:00
r, _ := http.NewRequest("GET", "/foo/bar", nil)
r.RemoteAddr = "127.0.0.1"
r.Host = "test-server"
h.ServeHTTP(httptest.NewRecorder(), r)
actual := buf.String()
if actual != test.ExpectedLogMessage {
t.Errorf("Log message was\n%s\ninstead of expected \n%s", actual, test.ExpectedLogMessage)
}
}
}