9 "github.com/justinas/alice"
10 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
11 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/middleware"
12 "github.com/stretchr/testify/assert"
15const RequestLoggingFormatWithoutTime = "{{.Client}} - {{.Username}} [TIMELESS] {{.Host}} {{.RequestMethod}} {{.Upstream}} {{.RequestURI}} {{.Protocol}} {{.UserAgent}} {{.StatusCode}} {{.ResponseSize}} {{.RequestDuration}}"
17func TestLoggingHandler_ServeHTTP(t *testing.T) {
20 ExpectedLogMessage string
25 Format: RequestLoggingFormatWithoutTime,
26 ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n",
28 ExcludePaths: []string{},
31 Format: RequestLoggingFormatWithoutTime,
32 ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n",
34 ExcludePaths: []string{},
37 Format: RequestLoggingFormatWithoutTime,
38 ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n",
40 ExcludePaths: []string{"/ping"},
43 Format: RequestLoggingFormatWithoutTime,
44 ExpectedLogMessage: "",
46 ExcludePaths: []string{"/foo/bar"},
49 Format: RequestLoggingFormatWithoutTime,
50 ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/ping\" HTTP/1.1 \"\" 200 4 0.000\n",
52 ExcludePaths: []string{},
55 Format: RequestLoggingFormatWithoutTime,
56 ExpectedLogMessage: "",
58 ExcludePaths: []string{"/ping"},
61 Format: RequestLoggingFormatWithoutTime,
62 ExpectedLogMessage: "",
64 ExcludePaths: []string{"/foo/bar", "/ping"},
67 Format: "{{.RequestMethod}}",
68 ExpectedLogMessage: "GET\n",
70 ExcludePaths: []string{""},
73 Format: "{{.RequestMethod}}",
74 ExpectedLogMessage: "GET\n",
76 ExcludePaths: []string{"/ping"},
79 Format: "{{.RequestMethod}}",
80 ExpectedLogMessage: "GET\n",
82 ExcludePaths: []string{""},
85 Format: "{{.RequestMethod}}",
86 ExpectedLogMessage: "",
88 ExcludePaths: []string{"/ping"},
92 for _, test := range tests {
93 buf := bytes.NewBuffer(nil)
94 handler := func(w http.ResponseWriter, req *http.Request) {
95 _, ok := w.(http.Hijacker)
97 t.Error("http.Hijacker is not available")
100 _, err := w.Write([]byte("test"))
101 assert.NoError(t, err)
104 logger.SetOutput(buf)
105 logger.SetReqTemplate(test.Format)
106 logger.SetExcludePaths(test.ExcludePaths)
107 h := alice.New(middleware.NewScope(), LoggingHandler).Then(http.HandlerFunc(handler))
109 r, _ := http.NewRequest("GET", test.Path, nil)
110 r.RemoteAddr = "127.0.0.1"
111 r.Host = "test-server"
113 h.ServeHTTP(httptest.NewRecorder(), r)
115 actual := buf.String()
116 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