2017-07-14 11:51:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2019-03-20 13:44:51 +00:00
|
|
|
"strings"
|
2017-07-14 11:51:16 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2019-02-10 16:37:45 +00:00
|
|
|
|
2019-05-24 16:08:48 +00:00
|
|
|
"github.com/pusher/oauth2_proxy/pkg/logger"
|
2017-07-14 11:51:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoggingHandler_ServeHTTP(t *testing.T) {
|
|
|
|
ts := time.Now()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
Format,
|
2019-05-31 08:11:28 +00:00
|
|
|
ExpectedLogMessage,
|
|
|
|
Path string
|
2019-06-21 21:39:46 +00:00
|
|
|
ExcludePaths []string
|
|
|
|
SilencePingLogging bool
|
2017-07-14 11:51:16 +00:00
|
|
|
}{
|
2019-06-21 21:39:46 +00:00
|
|
|
{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)), "/foo/bar", []string{}, false},
|
|
|
|
{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)), "/foo/bar", []string{}, true},
|
|
|
|
{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)), "/foo/bar", []string{"/ping"}, false},
|
|
|
|
{logger.DefaultRequestLoggingFormat, "", "/foo/bar", []string{"/foo/bar"}, false},
|
|
|
|
{logger.DefaultRequestLoggingFormat, "", "/ping", []string{}, true},
|
|
|
|
{logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/ping"}, false},
|
|
|
|
{logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/ping"}, true},
|
|
|
|
{logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/foo/bar", "/ping"}, false},
|
|
|
|
{"{{.RequestMethod}}", "GET\n", "/foo/bar", []string{}, true},
|
|
|
|
{"{{.RequestMethod}}", "GET\n", "/foo/bar", []string{"/ping"}, false},
|
|
|
|
{"{{.RequestMethod}}", "GET\n", "/ping", []string{}, false},
|
|
|
|
{"{{.RequestMethod}}", "", "/ping", []string{"/ping"}, true},
|
2017-07-14 11:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
handler := func(w http.ResponseWriter, req *http.Request) {
|
2019-03-22 21:19:38 +00:00
|
|
|
_, ok := w.(http.Hijacker)
|
|
|
|
if !ok {
|
|
|
|
t.Error("http.Hijacker is not available")
|
|
|
|
}
|
|
|
|
|
2017-07-14 11:51:16 +00:00
|
|
|
w.Write([]byte("test"))
|
|
|
|
}
|
|
|
|
|
2019-02-10 16:37:45 +00:00
|
|
|
logger.SetOutput(buf)
|
|
|
|
logger.SetReqTemplate(test.Format)
|
2019-06-21 21:39:46 +00:00
|
|
|
if test.SilencePingLogging {
|
|
|
|
test.ExcludePaths = append(test.ExcludePaths, "/ping")
|
|
|
|
}
|
|
|
|
logger.SetExcludePaths(test.ExcludePaths)
|
2019-02-10 16:37:45 +00:00
|
|
|
h := LoggingHandler(http.HandlerFunc(handler))
|
2017-07-14 11:51:16 +00:00
|
|
|
|
2019-05-31 08:11:28 +00:00
|
|
|
r, _ := http.NewRequest("GET", test.Path, nil)
|
2017-07-14 11:51:16 +00:00
|
|
|
r.RemoteAddr = "127.0.0.1"
|
|
|
|
r.Host = "test-server"
|
|
|
|
|
|
|
|
h.ServeHTTP(httptest.NewRecorder(), r)
|
|
|
|
|
|
|
|
actual := buf.String()
|
2019-03-20 13:44:51 +00:00
|
|
|
if !strings.Contains(actual, test.ExpectedLogMessage) {
|
|
|
|
t.Errorf("Log message was\n%s\ninstead of matching \n%s", actual, test.ExpectedLogMessage)
|
2017-07-14 11:51:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|