9 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
10 "github.com/stretchr/testify/assert"
13const RequestLoggingFormatWithoutTime = "{{.Client}} - {{.Username}} [TIMELESS] {{.Host}} {{.RequestMethod}} {{.Upstream}} {{.RequestURI}} {{.Protocol}} {{.UserAgent}} {{.StatusCode}} {{.ResponseSize}} {{.RequestDuration}}"
15func TestLoggingHandler_ServeHTTP(t *testing.T) {
18 ExpectedLogMessage string
23 Format: RequestLoggingFormatWithoutTime,
24 ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n",
26 ExcludePaths: []string{},
29 Format: RequestLoggingFormatWithoutTime,
30 ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n",
32 ExcludePaths: []string{},
35 Format: RequestLoggingFormatWithoutTime,
36 ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n",
38 ExcludePaths: []string{"/ping"},
41 Format: RequestLoggingFormatWithoutTime,
42 ExpectedLogMessage: "",
44 ExcludePaths: []string{"/foo/bar"},
47 Format: RequestLoggingFormatWithoutTime,
48 ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/ping\" HTTP/1.1 \"\" 200 4 0.000\n",
50 ExcludePaths: []string{},
53 Format: RequestLoggingFormatWithoutTime,
54 ExpectedLogMessage: "",
56 ExcludePaths: []string{"/ping"},
59 Format: RequestLoggingFormatWithoutTime,
60 ExpectedLogMessage: "",
62 ExcludePaths: []string{"/foo/bar", "/ping"},
65 Format: "{{.RequestMethod}}",
66 ExpectedLogMessage: "GET\n",
68 ExcludePaths: []string{""},
71 Format: "{{.RequestMethod}}",
72 ExpectedLogMessage: "GET\n",
74 ExcludePaths: []string{"/ping"},
77 Format: "{{.RequestMethod}}",
78 ExpectedLogMessage: "GET\n",
80 ExcludePaths: []string{""},
83 Format: "{{.RequestMethod}}",
84 ExpectedLogMessage: "",
86 ExcludePaths: []string{"/ping"},
90 for _, test := range tests {
91 buf := bytes.NewBuffer(nil)
92 handler := func(w http.ResponseWriter, req *http.Request) {
93 _, ok := w.(http.Hijacker)
95 t.Error("http.Hijacker is not available")
98 _, err := w.Write([]byte("test"))
99 assert.NoError(t, err)
102 logger.SetOutput(buf)
103 logger.SetReqTemplate(test.Format)
104 logger.SetExcludePaths(test.ExcludePaths)
105 h := LoggingHandler(http.HandlerFunc(handler))
107 r, _ := http.NewRequest("GET", test.Path, nil)
108 r.RemoteAddr = "127.0.0.1"
109 r.Host = "test-server"
111 h.ServeHTTP(httptest.NewRecorder(), r)
113 actual := buf.String()
114 assert.Equal(t, test.ExpectedLogMessage, actual)
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