-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
147 lines (125 loc) · 4.37 KB
/
middleware_test.go
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package servicefoundation_test
import (
"crypto/tls"
"net/http"
"testing"
sf "github.com/Travix-International/go-servicefoundation"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestMiddlewareWrapperImpl_Wrap(t *testing.T) {
scenarios := []sf.Middleware{
sf.CORS,
sf.NoCaching,
sf.Counter,
sf.Histogram,
sf.RequestLogging,
sf.RequestLogging,
sf.RequestMetrics,
sf.PanicTo500,
}
useTls := false
for i, scenario := range scenarios {
const subSystem = "my-sub"
const name = "my-name"
logFactory := &mockLogFactory{}
log := &mockLogger{}
m := &mockMetrics{}
corsOptions := &sf.CORSOptions{}
handleCalled := false
handle := func(sf.WrappedResponseWriter, *http.Request, sf.RouterParams) {
handleCalled = true
}
metaFunc := func(*http.Request, sf.RouterParams) map[string]string {
return make(map[string]string)
}
rdr := &mockReader{}
r, _ := http.NewRequest("GET", "https://www.sf.com/some/url", rdr)
if useTls {
r.URL.Scheme = ""
r.TLS = &tls.ConnectionState{}
}
useTls = !useTls
w := &mockResponseWriter{}
h := &mockHistogramVec{}
s := &mockSummaryVec{}
p := sf.RouterParams{}
header := http.Header{}
header.Set("foo", "bar")
w.On("Header").Return(header)
w.On("Status").Return(http.StatusOK)
h.On("RecordTimeElapsed", mock.Anything)
h.On("RecordDuration", mock.Anything, mock.Anything)
s.On("RecordDuration", mock.Anything, mock.Anything)
m.On("CountLabels", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
m.On("AddHistogramVec", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(h)
m.On("AddSummaryVec", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(s)
logFactory.On("NewLogger", mock.Anything).Return(log)
log.On("Info", mock.Anything, mock.Anything, mock.Anything).Return(nil)
sut := sf.NewMiddlewareWrapper(logFactory, m, corsOptions, sf.ServiceGlobals{})
// Act
actual := sut.Wrap(subSystem, name, scenario, handle, metaFunc)
assert.NotNil(t, actual, "Scenario %n", i)
assert.NotEqual(t, &handle, &actual, "Scenario %n", i)
actual(w, r, p)
assert.True(t, handleCalled, "Scenario %n", i)
}
}
func TestMiddlewareWrapperImpl_Wrap_UnknownMiddleware_ReturnsUnwrappedHandler(t *testing.T) {
const subSystem = "my-sub"
const name = "my-name"
logFactory := &mockLogFactory{}
log := &mockLogger{}
m := &mockMetrics{}
corsOptions := &sf.CORSOptions{}
metaFunc := func(*http.Request, sf.RouterParams) map[string]string {
return make(map[string]string)
}
handle := func(sf.WrappedResponseWriter, *http.Request, sf.RouterParams) {
}
logFactory.On("NewLogger", mock.Anything).Return(log)
log.On("Warn", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
sut := sf.NewMiddlewareWrapper(logFactory, m, corsOptions, sf.ServiceGlobals{})
// Act
actual := sut.Wrap(subSystem, name, 0, handle, metaFunc)
assert.NotNil(t, actual)
log.AssertExpectations(t)
}
func TestMiddlewareWrapperImpl_Wrap_PanicsAreHandled(t *testing.T) {
scenarios := []sf.Middleware{sf.PanicTo500}
for i, scenario := range scenarios {
const subSystem = "my-sub"
const name = "my-name"
logFactory := &mockLogFactory{}
log := &mockLogger{}
m := &mockMetrics{}
corsOptions := &sf.CORSOptions{}
metaCalled := false
metaFunc := func(*http.Request, sf.RouterParams) map[string]string {
metaCalled = true
return make(map[string]string)
}
handle := func(sf.WrappedResponseWriter, *http.Request, sf.RouterParams) {
panic("whoa")
}
rdr := &mockReader{}
r, _ := http.NewRequest("GET", "https://www.sf.com/some/url", rdr)
w := &mockResponseWriter{}
p := sf.RouterParams{}
logFactory.On("NewLogger", mock.Anything).Return(log)
log.On("Error", mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
w.On("WriteHeader", http.StatusInternalServerError).Once()
w.On("Header").Return(http.Header{}).Once()
w.On("Status").Return(http.StatusOK).Once()
sut := sf.NewMiddlewareWrapper(logFactory, m, corsOptions, sf.ServiceGlobals{})
// Act
actual := sut.Wrap(subSystem, name, scenario, handle, metaFunc)
assert.NotNil(t, actual, "Scenario %n", i)
assert.NotEqual(t, &handle, &actual, "Scenario %n", i)
assert.False(t, metaCalled, "Scenario %n", i)
actual(w, r, p)
assert.True(t, metaCalled, "Scenario %n", i)
log.AssertExpectations(t)
w.AssertExpectations(t)
}
}