git.iko.soy/typeable/oauth2-proxy
on remove-gap-headers help sign in
typeable/oauth2-proxy/logging_handler_test.go
118 lines 3.5 kB history raw
1package main
3import (
4 "bytes"
5 "net/http"
6 "net/http/httptest"
7 "testing"
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) {
18 tests := []struct {
19 Format string
20 ExpectedLogMessage string
21 Path string
22 ExcludePaths []string
23 }{
24 {
25 Format: RequestLoggingFormatWithoutTime,
26 ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n",
27 Path: "/foo/bar",
28 ExcludePaths: []string{},
29 },
30 {
31 Format: RequestLoggingFormatWithoutTime,
32 ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n",
33 Path: "/foo/bar",
34 ExcludePaths: []string{},
35 },
36 {
37 Format: RequestLoggingFormatWithoutTime,
38 ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n",
39 Path: "/foo/bar",
40 ExcludePaths: []string{"/ping"},
41 },
42 {
43 Format: RequestLoggingFormatWithoutTime,
44 ExpectedLogMessage: "",
45 Path: "/foo/bar",
46 ExcludePaths: []string{"/foo/bar"},
47 },
48 {
49 Format: RequestLoggingFormatWithoutTime,
50 ExpectedLogMessage: "127.0.0.1 - - [TIMELESS] test-server GET - \"/ping\" HTTP/1.1 \"\" 200 4 0.000\n",
51 Path: "/ping",
52 ExcludePaths: []string{},
53 },
54 {
55 Format: RequestLoggingFormatWithoutTime,
56 ExpectedLogMessage: "",
57 Path: "/ping",
58 ExcludePaths: []string{"/ping"},
59 },
60 {
61 Format: RequestLoggingFormatWithoutTime,
62 ExpectedLogMessage: "",
63 Path: "/ping",
64 ExcludePaths: []string{"/foo/bar", "/ping"},
65 },
66 {
67 Format: "{{.RequestMethod}}",
68 ExpectedLogMessage: "GET\n",
69 Path: "/foo/bar",
70 ExcludePaths: []string{""},
71 },
72 {
73 Format: "{{.RequestMethod}}",
74 ExpectedLogMessage: "GET\n",
75 Path: "/foo/bar",
76 ExcludePaths: []string{"/ping"},
77 },
78 {
79 Format: "{{.RequestMethod}}",
80 ExpectedLogMessage: "GET\n",
81 Path: "/ping",
82 ExcludePaths: []string{""},
83 },
84 {
85 Format: "{{.RequestMethod}}",
86 ExpectedLogMessage: "",
87 Path: "/ping",
88 ExcludePaths: []string{"/ping"},
89 },
90 }
92 for _, test := range tests {
93 buf := bytes.NewBuffer(nil)
94 handler := func(w http.ResponseWriter, req *http.Request) {
95 _, ok := w.(http.Hijacker)
96 if !ok {
97 t.Error("http.Hijacker is not available")
98 }
100 _, err := w.Write([]byte("test"))
101 assert.NoError(t, err)
102 }
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)
117 }