-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_test.go
More file actions
289 lines (251 loc) · 6.8 KB
/
common_test.go
File metadata and controls
289 lines (251 loc) · 6.8 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package platform
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"sync/atomic"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestCreateUuid(t *testing.T) {
Convey("Creating a uuid should produce a non empty string", t, func() {
uuid := CreateUUID()
So(uuid, ShouldNotEqual, "")
})
Convey("Multiple uuids should be unique", t, func() {
uuid1 := CreateUUID()
So(uuid1, ShouldNotEqual, "")
uuid2 := CreateUUID()
So(uuid2, ShouldNotEqual, "")
So(uuid1, ShouldNotEqual, uuid2)
})
}
func TestGenerateResponse(t *testing.T) {
Convey("Generating a response from an empty request object should populate empty routing", t, func() {
response := generateResponse(&Request{}, &Request{})
So(response, ShouldResemble, &Request{
Routing: &Routing{
RouteTo: []*Route{},
RouteFrom: []*Route{},
},
})
})
Convey("Generating a response from an should place the route from of the request onto the route to of the response", t, func() {
response := generateResponse(&Request{
Routing: &Routing{
RouteFrom: []*Route{
&Route{
Uri: String("testing-route-from"),
},
},
},
}, &Request{})
So(response, ShouldResemble, &Request{
Routing: &Routing{
RouteTo: []*Route{
&Route{
Uri: String("testing-route-from"),
},
},
RouteFrom: []*Route{},
},
})
})
Convey("Generating a response from an should place the route from of the request onto the route to of the response", t, func() {
response := generateResponse(&Request{
Routing: &Routing{
RouteFrom: []*Route{
&Route{
Uri: String("testing-route-from"),
},
},
},
}, &Request{
Routing: &Routing{
RouteTo: []*Route{
&Route{
Uri: String("testing-route-to"),
},
},
},
})
So(response, ShouldResemble, &Request{
Routing: &Routing{
RouteTo: []*Route{
&Route{
Uri: String("testing-route-to"),
},
&Route{
Uri: String("testing-route-from"),
},
},
RouteFrom: []*Route{},
},
})
})
}
func TestGetenv(t *testing.T) {
Convey("Getting an environment variable that doesn't exist should resort to the default", t, func() {
So(Getenv("RANDOM_KEY", "DEFAULT_VALUE"), ShouldEqual, "DEFAULT_VALUE")
})
Convey("Getting an environment variable that does exist should resort to the value that was set", t, func() {
os.Setenv("TEST_GET_ENV", "HELLO")
So(Getenv("TEST_GET_ENV", "DEFAULT_VALUE"), ShouldEqual, "HELLO")
})
}
func TestGetLogger(t *testing.T) {
Convey("Get logger should always return a non nil logger", t, func() {
logger1 := GetLogger("testing")
So(logger1, ShouldNotBeNil)
})
}
func TestGetMyIp(t *testing.T) {
Convey("Fetching our IP should invoke the handler 3 times", t, func() {
var totalCalls int32
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&totalCalls, 1)
// Just buying some time to let all goroutines run
time.Sleep(10 * time.Millisecond)
fmt.Fprint(w, "127.0.0.1")
}))
defer s.Close()
ip, err := getMyIp(&http.Client{
Transport: &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(s.URL)
},
},
}, 1*time.Second)
So(ip, ShouldEqual, "127.0.0.1")
So(err, ShouldBeNil)
So(totalCalls, ShouldEqual, 3)
})
Convey("Forcing a timeout should return an error", t, func() {
var totalCalls int32
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&totalCalls, 1)
// Just buying some time to let all goroutines run
time.Sleep(100 * time.Millisecond)
fmt.Fprint(w, "127.0.0.1")
}))
defer s.Close()
ip, err := getMyIp(&http.Client{
Transport: &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(s.URL)
},
},
}, 10*time.Millisecond)
So(ip, ShouldEqual, "")
So(err, ShouldResemble, errors.New("Timed out trying to fetch ip address."))
So(totalCalls, ShouldEqual, 3)
})
}
func TestIsInternalRequest(t *testing.T) {
Convey("A request with no routing should not be considered internal", t, func() {
So(IsInternalRequest(&Request{}), ShouldBeFalse)
So(IsInternalRequest(&Request{
Routing: &Routing{},
}), ShouldBeFalse)
})
Convey("A request that the client injected 'microservice' into should not be an internal request", t, func() {
So(IsInternalRequest(&Request{
Routing: &Routing{
RouteFrom: []*Route{
&Route{
Uri: String("microservice:///shenanigans"),
},
&Route{
Uri: String("client:///shenanigans"),
},
&Route{
Uri: String("router:///shenanigans"),
},
},
},
}), ShouldBeFalse)
})
Convey("A request that the client that properly comes from a microservice according to the last to routes should be considered internal", t, func() {
So(IsInternalRequest(&Request{
Routing: &Routing{
RouteFrom: []*Route{
&Route{
Uri: String("microservice:///shenanigans"),
},
&Route{
Uri: String("client:///shenanigans"),
},
&Route{
Uri: String("router:///shenanigans"),
},
&Route{
Uri: String("microservice:///shenanigans"),
},
},
},
}), ShouldBeTrue)
So(IsInternalRequest(&Request{
Routing: &Routing{
RouteFrom: []*Route{
&Route{
Uri: String("microservice:///shenanigans"),
},
&Route{
Uri: String("client:///shenanigans"),
},
&Route{
Uri: String("router:///shenanigans"),
},
&Route{
Uri: String("microservice:///shenanigans"),
},
&Route{
Uri: String("router:///shenanigans"),
},
},
},
}), ShouldBeTrue)
})
}
func TestRouteToSchemeMatches(t *testing.T) {
Convey("Matching against the route to scheme on a request without routing info should return false", t, func() {
request := &Request{}
So(RouteToSchemeMatches(request, "microservice"), ShouldBeFalse)
})
Convey("Matching against the route to scheme on a request with an empty route to array should return false", t, func() {
request := &Request{
Routing: &Routing{
RouteTo: []*Route{},
},
}
So(RouteToSchemeMatches(request, "microservice"), ShouldBeFalse)
})
Convey("Matching against the route to scheme on a request that has an invalid url should return false", t, func() {
request := &Request{
Routing: &Routing{
RouteTo: []*Route{
&Route{
Uri: String("://testing"),
},
},
},
}
So(RouteToSchemeMatches(request, "microservice"), ShouldBeFalse)
})
Convey("Matching against the route to scheme with a matching request should return true", t, func() {
request := &Request{
Routing: &Routing{
RouteTo: []*Route{
&Route{
Uri: String("microservice://testing"),
},
},
},
}
So(RouteToSchemeMatches(request, "microservice"), ShouldBeTrue)
})
}