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