12 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
15func TestLoggingHandler_ServeHTTP(t *testing.T) {
23 SilencePingLogging bool
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{}, false},
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{}, true},
27 {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},
28 {logger.DefaultRequestLoggingFormat, "", "/foo/bar", []string{"/foo/bar"}, false},
29 {logger.DefaultRequestLoggingFormat, "", "/ping", []string{}, true},
30 {logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/ping"}, false},
31 {logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/ping"}, true},
32 {logger.DefaultRequestLoggingFormat, "", "/ping", []string{"/foo/bar", "/ping"}, false},
33 {"{{.RequestMethod}}", "GET\n", "/foo/bar", []string{}, true},
34 {"{{.RequestMethod}}", "GET\n", "/foo/bar", []string{"/ping"}, false},
35 {"{{.RequestMethod}}", "GET\n", "/ping", []string{}, false},
36 {"{{.RequestMethod}}", "", "/ping", []string{"/ping"}, true},
39 for _, test := range tests {
40 buf := bytes.NewBuffer(nil)
41 handler := func(w http.ResponseWriter, req *http.Request) {
42 _, ok := w.(http.Hijacker)
44 t.Error("http.Hijacker is not available")
47 w.Write([]byte("test"))
51 logger.SetReqTemplate(test.Format)
52 if test.SilencePingLogging {
53 test.ExcludePaths = append(test.ExcludePaths, "/ping")
55 logger.SetExcludePaths(test.ExcludePaths)
56 h := LoggingHandler(http.HandlerFunc(handler))
58 r, _ := http.NewRequest("GET", test.Path, nil)
59 r.RemoteAddr = "127.0.0.1"
60 r.Host = "test-server"
62 h.ServeHTTP(httptest.NewRecorder(), r)
64 actual := buf.String()
65 if !strings.Contains(actual, test.ExpectedLogMessage) {
66 t.Errorf("Log message was\n%s\ninstead of matching \n%s", actual, test.ExpectedLogMessage)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list