-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_test.go
221 lines (180 loc) · 7.56 KB
/
route_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package rmhttp
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// ------------------------------------------------------------------------------------------------
// ROUTE TESTS
// ------------------------------------------------------------------------------------------------
// Test_Route_WithMiddleware checks that middleware can be added to a Route.
func Test_Route_WithMiddleware(t *testing.T) {
handler := createTestHandlerFunc(http.StatusOK, "test body", nil)
m1 := createTestMiddlewareHandler("x-m1", "m1")
m2 := createTestMiddlewareHandler("x-m2", "m2")
route := NewRoute(http.MethodGet, "/route", HandlerFunc(handler))
route.WithMiddleware(m1, m2)
assert.Len(t, route.Middleware, 2, "they should be equal")
}
// Test_Route_WithHeader checks that headers can be added to a Route.
func Test_Route_WithHeader(t *testing.T) {
handler := createTestHandlerFunc(http.StatusOK, "test body", nil)
route := NewRoute(http.MethodGet, "/route", HandlerFunc(handler))
route.WithHeader("x-h1", "h1")
route.WithHeader("x-h2", "h2")
assert.Len(t, route.Headers, 2, "they should be equal")
}
// Test_Route_WithTimeout checks that a timeout can be added to a Route.
func Test_Route_WithTimeout(t *testing.T) {
handler := createTestHandlerFunc(http.StatusOK, "test body", nil)
route := NewRoute(http.MethodGet, "/route", HandlerFunc(handler))
timeout := NewTimeout(5*time.Second, "Timeout!")
route.WithTimeout(timeout.Duration, timeout.Message)
assert.Equal(t, timeout.Duration, route.Timeout.Duration, "they should be equal")
assert.Equal(t, timeout.Message, route.Timeout.Message, "they should be equal")
assert.True(t, timeout.Enabled, "they should be equal")
}
// Test_Route_ComputedTimeout checks that a parent Group timeout is used if a Timeout is not set
// directly on the Route.
func Test_Route_ComputedTimeout(t *testing.T) {
t.Run("group timeout is used when route timeout has not been set", func(t *testing.T) {
handler := createTestHandlerFunc(http.StatusOK, "test body", nil)
group := NewGroup("")
group.WithTimeout(2, "group timeout")
route := NewRoute(http.MethodGet, "/route", HandlerFunc(handler))
group.Route(route)
assert.Equal(
t,
group.Timeout.Duration,
route.ComputedTimeout().Duration,
"they should be equal",
)
assert.Equal(
t,
group.Timeout.Message,
route.ComputedTimeout().Message,
"they should be equal",
)
})
t.Run("timeout can be found with multiple groups", func(t *testing.T) {
handler := createTestHandlerFunc(http.StatusOK, "test body", nil)
rootGroup := NewGroup("")
rootGroup.WithTimeout(2, "root group timeout")
group := NewGroup("/test")
rootGroup.Group(group)
route := NewRoute(http.MethodGet, "/route", HandlerFunc(handler))
group.Route(route)
assert.Equal(
t,
rootGroup.Timeout.Duration,
route.ComputedTimeout().Duration,
"they should be equal",
)
assert.Equal(
t,
rootGroup.Timeout.Message,
route.ComputedTimeout().Message,
"they should be equal",
)
})
}
// Test_Route_ComputedPattern checks that the route pattern can be built with optional Group
// patterns prefixed.
func Test_Route_ComputedPattern(t *testing.T) {
t.Run("route pattern is used when no group patterns are set", func(t *testing.T) {
handler := createTestHandlerFunc(http.StatusOK, "test body", nil)
route := NewRoute(http.MethodGet, "/route", HandlerFunc(handler))
assert.Equal(t, "/route", route.ComputedPattern(), "they should be equal")
})
t.Run("group pattern is prepended to route pattern", func(t *testing.T) {
group := NewGroup("/group")
handler := createTestHandlerFunc(http.StatusOK, "test body", nil)
route := NewRoute(http.MethodGet, "/route", HandlerFunc(handler))
group.Route(route)
assert.Equal(t, "/group/route", route.ComputedPattern(), "they should be equal")
})
t.Run("multiple group patterns are prepended to route pattern", func(t *testing.T) {
handler := createTestHandlerFunc(http.StatusOK, "test body", nil)
rootGroup := NewGroup("/root")
group := NewGroup("/group")
rootGroup.Group(group)
route := NewRoute(http.MethodGet, "/route", HandlerFunc(handler))
group.Route(route)
assert.Equal(t, "/root/group/route", route.ComputedPattern(), "they should be equal")
})
}
// Test_Route_ComputedHeaders checks that the correct headers are returned for a route,
// including any set on a parent Group
func Test_Route_ComputedHeaders(t *testing.T) {
t.Run("route headers are used when no group headers are set", func(t *testing.T) {
handler := createTestHandlerFunc(http.StatusOK, "test body", nil)
route := NewRoute(http.MethodGet, "/route", HandlerFunc(handler))
route.WithHeader("x-h1", "h1")
route.WithHeader("x-h2", "h2")
headers := route.ComputedHeaders()
assert.Len(t, headers, 2, "they should be equal")
assert.Equal(t, "h1", headers["x-h1"], "they should be equal")
assert.Equal(t, "h2", headers["x-h2"], "they should be equal")
})
t.Run("group headers are used when no route headers are set", func(t *testing.T) {
handler := createTestHandlerFunc(http.StatusOK, "test body", nil)
route := NewRoute(http.MethodGet, "/route", HandlerFunc(handler))
group := NewGroup("")
group.Route(route)
group.WithHeader("x-h1", "h1")
group.WithHeader("x-h2", "h2")
headers := route.ComputedHeaders()
assert.Len(t, headers, 2, "they should be equal")
assert.Equal(t, "h1", headers["x-h1"], "they should be equal")
assert.Equal(t, "h2", headers["x-h2"], "they should be equal")
})
t.Run("group headers are added to route headers", func(t *testing.T) {
handler := createTestHandlerFunc(http.StatusOK, "test body", nil)
group := NewGroup("")
group.WithHeader("x-g1", "g1")
group.WithHeader("x-g2", "g2")
route := NewRoute(http.MethodGet, "/route", HandlerFunc(handler))
route.WithHeader("x-h1", "h1")
route.WithHeader("x-h2", "h2")
group.Route(route)
headers := route.ComputedHeaders()
assert.Len(t, headers, 4, "they should be equal")
assert.Equal(t, "h1", headers["x-h1"], "they should be equal")
assert.Equal(t, "h2", headers["x-h2"], "they should be equal")
assert.Equal(t, "g1", headers["x-g1"], "they should be equal")
assert.Equal(t, "g2", headers["x-g2"], "they should be equal")
})
t.Run("multiple group headers are added to route headers", func(t *testing.T) {
handler := createTestHandlerFunc(http.StatusOK, "test body", nil)
rootGroup := NewGroup("/root")
rootGroup.WithHeader("x-rg1", "rg1")
rootGroup.WithHeader("x-rg2", "rg2")
group := NewGroup("/group")
group.WithHeader("x-g1", "g1")
group.WithHeader("x-g2", "g2")
rootGroup.Group(group)
route := NewRoute(http.MethodGet, "/route", HandlerFunc(handler))
route.WithHeader("x-h1", "h1")
route.WithHeader("x-h2", "h2")
group.Route(route)
headers := route.ComputedHeaders()
assert.Len(t, headers, 6, "they should be equal")
assert.Equal(t, "h1", headers["x-h1"], "they should be equal")
assert.Equal(t, "h2", headers["x-h2"], "they should be equal")
assert.Equal(t, "rg1", headers["x-rg1"], "they should be equal")
assert.Equal(t, "rg2", headers["x-rg2"], "they should be equal")
assert.Equal(t, "g1", headers["x-g1"], "they should be equal")
assert.Equal(t, "g2", headers["x-g2"], "they should be equal")
})
t.Run("group headers do not overwrite route headers", func(t *testing.T) {
handler := createTestHandlerFunc(http.StatusOK, "test body", nil)
group := NewGroup("")
group.WithHeader("x-h1", "g1")
route := NewRoute(http.MethodGet, "/route", HandlerFunc(handler))
route.WithHeader("x-h1", "h1")
group.Route(route)
headers := route.ComputedHeaders()
assert.Equal(t, "h1", headers["x-h1"], "they should be equal")
})
}