@@ -2,37 +2,89 @@ package main
22
33import (
44 "bytes"
5− "fmt"
65 "net/http"
76 "net/http/httptest"
8− "strings"
97 "testing"
10− "time"
118
129 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
10+ "github.com/stretchr/testify/assert"
1311)
1412
15−func TestLoggingHandler_ServeHTTP(t *testing.T) {
16− ts := time.Now()
13+const RequestLoggingFormatWithoutTime = "{{.Client}} - {{.Username}} [TIMELESS] {{.Host}} {{.RequestMethod}} {{.Upstream}} {{.RequestURI}} {{.Protocol}} {{.UserAgent}} {{.StatusCode}} {{.ResponseSize}} {{.RequestDuration}}"
1714
15+func TestLoggingHandler_ServeHTTP(t *testing.T) {
1816 tests := []struct {
19− Format,
20− ExpectedLogMessage,
21− Path string
22− ExcludePaths []string
17+ Format string
18+ ExpectedLogMessage string
19+ Path string
20+ ExcludePaths []string
2321 }{
24− {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{}},
25− {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{}},
26− {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"}},
27− {logger.DefaultRequestLoggingFormat, "", "/foo/bar", []string{"/foo/bar"}},
28− {logger.DefaultRequestLoggingFormat, "", "/ping", []string{}},
29− {logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/ping"}},
30− {logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/ping"}},
31− {logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/foo/bar", "/ping"}},
32− {"{{.RequestMethod}}", "GET\n", "/foo/bar", []string{}},
33− {"{{.RequestMethod}}", "GET\n", "/foo/bar", []string{"/ping"}},
34− {"{{.RequestMethod}}", "GET\n", "/ping", []string{}},
35− {"{{.RequestMethod}}", "", "/ping", []string{"/ping"}},
22+ {
23+ Format: RequestLoggingFormatWithoutTime,
24+ ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n",
25+ Path: "/foo/bar",
26+ ExcludePaths: []string{},
27+ },
28+ {
29+ Format: RequestLoggingFormatWithoutTime,
30+ ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n",
31+ Path: "/foo/bar",
32+ ExcludePaths: []string{},
33+ },
34+ {
35+ Format: RequestLoggingFormatWithoutTime,
36+ ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n",
37+ Path: "/foo/bar",
38+ ExcludePaths: []string{"/ping"},
39+ },
40+ {
41+ Format: RequestLoggingFormatWithoutTime,
42+ ExpectedLogMessage: "",
43+ Path: "/foo/bar",
44+ ExcludePaths: []string{"/foo/bar"},
45+ },
46+ {
47+ Format: RequestLoggingFormatWithoutTime,
48+ ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/ping\" HTTP/1.1 \"\" 200 4 0.000\n",
49+ Path: "/ping",
50+ ExcludePaths: []string{},
51+ },
52+ {
53+ Format: RequestLoggingFormatWithoutTime,
54+ ExpectedLogMessage: "",
55+ Path: "/ping",
56+ ExcludePaths: []string{"/ping"},
57+ },
58+ {
59+ Format: RequestLoggingFormatWithoutTime,
60+ ExpectedLogMessage: "",
61+ Path: "/ping",
62+ ExcludePaths: []string{"/foo/bar", "/ping"},
63+ },
64+ {
65+ Format: "{{.RequestMethod}}",
66+ ExpectedLogMessage: "GET\n",
67+ Path: "/foo/bar",
68+ ExcludePaths: []string{""},
69+ },
70+ {
71+ Format: "{{.RequestMethod}}",
72+ ExpectedLogMessage: "GET\n",
73+ Path: "/foo/bar",
74+ ExcludePaths: []string{"/ping"},
75+ },
76+ {
77+ Format: "{{.RequestMethod}}",
78+ ExpectedLogMessage: "GET\n",
79+ Path: "/ping",
80+ ExcludePaths: []string{""},
81+ },
82+ {
83+ Format: "{{.RequestMethod}}",
84+ ExpectedLogMessage: "",
85+ Path: "/ping",
86+ ExcludePaths: []string{"/ping"},
87+ },
3688 }
3789
3890 for _, test := range tests {
@@ -43,7 +95,8 @@ func TestLoggingHandler_ServeHTTP(t *testing.T) {
4395 t.Error("http.Hijacker is not available")
4496 }
4597
46− w.Write([]byte("test"))
98+ _, err := w.Write([]byte("test"))
99+ assert.NoError(t, err)
47100 }
48101
49102 logger.SetOutput(buf)
@@ -58,8 +111,6 @@ func TestLoggingHandler_ServeHTTP(t *testing.T) {
58111 h.ServeHTTP(httptest.NewRecorder(), r)
59112
60113 actual := buf.String()
61− if !strings.Contains(actual, test.ExpectedLogMessage) {
62− t.Errorf("Log message was\n%s\ninstead of matching \n%s", actual, test.ExpectedLogMessage)
63− }
114+ assert.Equal(t, test.ExpectedLogMessage, actual)
64115 }
65116}