This repository was archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmiddleware_test.go
More file actions
130 lines (120 loc) · 5.66 KB
/
middleware_test.go
File metadata and controls
130 lines (120 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package http_logrus_test
import (
"fmt"
"net/http"
"runtime"
"strings"
"testing"
"github.com/improbable-eng/go-httpwares"
"github.com/improbable-eng/go-httpwares/logging/logrus"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
func customMiddlewareCodeToLevel(statusCode int) logrus.Level {
if statusCode == testCodeImATeapot {
// Make this a special case for tests, and an error.
return logrus.ErrorLevel
}
level := http_logrus.DefaultMiddlewareCodeToLevel(statusCode)
return level
}
func TestLogrusMiddlewareSuite(t *testing.T) {
if strings.HasPrefix(runtime.Version(), "go1.7") {
t.Skipf("Skipping due to json.RawMessage incompatibility with go1.7")
return
}
s := &logrusMiddlewareTestSuite{newLogrusBaseTestSuite(t)}
// In this suite we have all the Middleware, but no Tripperware.
s.WaresTestSuite.ServerMiddleware = []httpwares.Middleware{
http_logrus.Middleware(
logrus.NewEntry(s.logrusBaseTestSuite.logger).WithField("http.handler.group", "my_service"),
http_logrus.WithDecider(func(w httpwares.WrappedResponseWriter, r *http.Request) bool {
return r.URL.Path != "/blah"
}),
http_logrus.WithRequestFieldExtractor(func(req *http.Request) map[string]interface{} {
return map[string]interface{}{
"http.request.custom": req.Header.Get("x-test-data"),
}
}),
http_logrus.WithResponseFieldExtractor(func(res httpwares.WrappedResponseWriter) map[string]interface{} {
return map[string]interface{}{
"http.response.custom": 1234,
}
}),
http_logrus.WithLevels(customMiddlewareCodeToLevel),
http_logrus.WithRequestBodyCapture(requestCaptureDeciderForTest),
http_logrus.WithResponseBodyCapture(responseCaptureDeciderForTest),
),
}
suite.Run(t, s)
}
type logrusMiddlewareTestSuite struct {
*logrusBaseTestSuite
}
func (s *logrusMiddlewareTestSuite) TestPing_WithCustomFields() {
req, _ := http.NewRequest("GET", "https://something.local/someurl", nil)
req.Header.Set("User-Agent", "testagent")
req.Header.Set("referer", "http://improbable.io/")
req.Header.Set("x-test-data", "test")
msgs := s.makeSuccessfulRequestWithAssertions(req, 2, "server")
for _, m := range msgs {
assert.Contains(s.T(), m, `"http.request.custom": "test"`, "all lines must contain fields added in using request field extractor")
assert.Contains(s.T(), m, `"custom_tags.string": "something"`, "all lines must contain `custom_tags.string` set by AddFields")
assert.Contains(s.T(), m, `"custom_tags.int": 1337`, "all lines must contain `custom_tags.int` set by AddFields")
assert.Contains(s.T(), m, `"http.request.method": "GET"`, "all lines must contain the http verb")
assert.Contains(s.T(), m, `"http.request.referer": "http://improbable.io/"`, "all lines must contain the referer if present")
assert.Contains(s.T(), m, `"http.request.user_agent": "testagent"`, "all lines must contain the user agent")
}
assert.Contains(s.T(), msgs[0], `"level": "warning"`, "warningf handler myst be logged as this..")
assert.Contains(s.T(), msgs[0], `"msg": "handler_log"`, "handler's message must contain user message")
assert.Contains(s.T(), msgs[1], `"msg": "finished HTTP call with code 201 Created"`, "interceptor message must contain string")
assert.Contains(s.T(), msgs[1], `"level": "info"`, "~200 status codes must be logged as info by default.")
assert.Contains(s.T(), msgs[1], `"http.time_ms":`, "interceptor log statement should contain execution time")
assert.Contains(s.T(), msgs[1], `"http.response.length_bytes":`, "interceptor log statement should contain response size")
assert.Contains(s.T(), msgs[1], `"http.response.custom": 1234`, "all lines must contain fields added in using response field extractor")
}
func (s *logrusMiddlewareTestSuite) TestPingError_WithCustomLevels() {
for _, tcase := range []struct {
code int
level logrus.Level
msg string
}{
{
code: http.StatusInternalServerError,
level: logrus.ErrorLevel,
msg: "Internal (500) must remap to ErrorLevel in DefaultMiddlewareCodeToLevel",
},
{
code: http.StatusNotFound,
level: logrus.InfoLevel,
msg: "NotFound (404) must remap to InfoLevel in DefaultMiddlewareCodeToLevel",
},
{
code: http.StatusBadRequest,
level: logrus.WarnLevel,
msg: "BadRequest (400) must remap to WarnLevel in DefaultMiddlewareCodeToLevel",
},
{
code: http.StatusTeapot,
level: logrus.ErrorLevel,
msg: "ImATeapot is overwritten to ErrorLevel with customMiddlewareCodeToLevel override, which probably didn't work",
},
} {
s.SetupTest()
req, _ := http.NewRequest("GET", fmt.Sprintf("https://something.local/someurl?code=%d", tcase.code), nil)
msgs := s.makeSuccessfulRequestWithAssertions(req, 2, "server")
m := msgs[1]
assert.Contains(s.T(), m, fmt.Sprintf(`"http.response.status": %d`, tcase.code), "all lines must contain method name")
assert.Contains(s.T(), m, fmt.Sprintf(`"level": "%s"`, tcase.level.String()), tcase.msg)
assert.Contains(s.T(), m, `"http.response.length_bytes":`, "interceptor log statement should contain response size")
}
}
func (s *logrusMiddlewareTestSuite) TestPing_WithNoEndLogging() {
req, _ := http.NewRequest("GET", "https://something.local/blah", nil)
msgs := s.makeSuccessfulRequestWithAssertions(req, 1, "server")
assert.Contains(s.T(), msgs[0], `"custom_tags.string": "something"`, "all lines must contain `custom_tags.string` set by AddFields")
assert.Contains(s.T(), msgs[0], `"custom_tags.int": 1337`, "all lines must contain `custom_tags.int` set by AddFields")
assert.Contains(s.T(), msgs[0], `"level": "warning"`, "warningf handler myst be logged as this..")
assert.Contains(s.T(), msgs[0], `"msg": "handler_log"`, "handler's message must contain user message")
}