@@ -5,14 +5,11 @@ import (
55 "fmt"
66 "net/http"
77 "net/http/httptest"
8− "net/url"
98 "strings"
109 "testing"
1110 "time"
1211
13− "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
1412 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
15− "github.com/oauth2-proxy/oauth2-proxy/pkg/validation"
1613)
1714
1815func TestLoggingHandler_ServeHTTP(t *testing.T) {
@@ -22,21 +19,20 @@ func TestLoggingHandler_ServeHTTP(t *testing.T) {
2219 Format,
2320 ExpectedLogMessage,
2421 Path string
25− ExcludePaths []string
26− SilencePingLogging bool
22+ ExcludePaths []string
2723 }{
28− {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},
29− {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},
30− {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},
31− {logger.DefaultRequestLoggingFormat, "", "/foo/bar", []string{"/foo/bar"}, false},
32− {logger.DefaultRequestLoggingFormat, "", "/ping", []string{}, true},
33− {logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/ping"}, false},
34− {logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/ping"}, true},
35− {logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/foo/bar", "/ping"}, false},
36− {"{{.RequestMethod}}", "GET\n", "/foo/bar", []string{}, true},
37− {"{{.RequestMethod}}", "GET\n", "/foo/bar", []string{"/ping"}, false},
38− {"{{.RequestMethod}}", "GET\n", "/ping", []string{}, false},
39− {"{{.RequestMethod}}", "", "/ping", []string{"/ping"}, true},
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"}},
4036 }
4137
4238 for _, test := range tests {
@@ -52,9 +48,6 @@ func TestLoggingHandler_ServeHTTP(t *testing.T) {
5248
5349 logger.SetOutput(buf)
5450 logger.SetReqTemplate(test.Format)
55− if test.SilencePingLogging {
56− test.ExcludePaths = append(test.ExcludePaths, "/ping")
57− }
5851 logger.SetExcludePaths(test.ExcludePaths)
5952 h := LoggingHandler(http.HandlerFunc(handler))
6053
@@ -70,59 +63,3 @@ func TestLoggingHandler_ServeHTTP(t *testing.T) {
7063 }
7164 }
7265}
73−
74−func TestLoggingHandler_PingUserAgent(t *testing.T) {
75− tests := []struct {
76− ExpectedLogMessage string
77− Path string
78− SilencePingLogging bool
79− WithUserAgent string
80− }{
81− {"444\n", "/foo", true, "Blah"},
82− {"444\n", "/foo", false, "Blah"},
83− {"", "/ping", true, "Blah"},
84− {"200\n", "/ping", false, "Blah"},
85− {"", "/ping", true, "PingMe!"},
86− {"", "/ping", false, "PingMe!"},
87− {"", "/foo", true, "PingMe!"},
88− {"", "/foo", false, "PingMe!"},
89− }
90−
91− for idx, test := range tests {
92− t.Run(fmt.Sprintf("%d", idx), func(t *testing.T) {
93− opts := options.NewOptions()
94− opts.PingUserAgent = "PingMe!"
95− opts.SkipAuthRegex = []string{"/foo"}
96− opts.Upstreams = []string{"static://444/foo"}
97− opts.Logging.SilencePing = test.SilencePingLogging
98− if test.SilencePingLogging {
99− opts.Logging.ExcludePaths = []string{"/ping"}
100− }
101− opts.RawRedirectURL = "localhost"
102− validation.Validate(opts)
103−
104− p := NewOAuthProxy(opts, func(email string) bool {
105− return true
106− })
107− p.provider = NewTestProvider(&url.URL{Host: "localhost"}, "")
108−
109− buf := bytes.NewBuffer(nil)
110− logger.SetOutput(buf)
111− logger.SetReqEnabled(true)
112− logger.SetReqTemplate("{{.StatusCode}}")
113−
114− r, _ := http.NewRequest("GET", test.Path, nil)
115− if test.WithUserAgent != "" {
116− r.Header.Set("User-Agent", test.WithUserAgent)
117− }
118−
119− h := LoggingHandler(p)
120− h.ServeHTTP(httptest.NewRecorder(), r)
121−
122− actual := buf.String()
123− if !strings.Contains(actual, test.ExpectedLogMessage) {
124− t.Errorf("Log message was\n%s\ninstead of matching \n%s", actual, test.ExpectedLogMessage)
125− }
126− })
127− }
128−}