@@ -71,6 +71,18 @@ func (l *fakeAppEngineLocator) Locate(req *http.Request) (*clientgeo.Location, e
7171 return l .loc , l .err
7272}
7373
74+ type fakeRateLimiter struct {
75+ limited bool
76+ err error
77+ }
78+
79+ func (r * fakeRateLimiter ) IsLimited (ip , ua string ) (bool , error ) {
80+ if r .err != nil {
81+ return false , r .err
82+ }
83+ return r .limited , nil
84+ }
85+
7486func TestClient_Nearest (t * testing.T ) {
7587 tests := []struct {
7688 name string
@@ -81,6 +93,7 @@ func TestClient_Nearest(t *testing.T) {
8193 project string
8294 latlon string
8395 limits limits.Agents
96+ ipLimiter Limiter
8497 header http.Header
8598 wantLatLon string
8699 wantKey string
@@ -206,13 +219,96 @@ func TestClient_Nearest(t *testing.T) {
206219 wantKey : "ws://:3001/ndt_protocol" ,
207220 wantStatus : http .StatusOK ,
208221 },
222+ {
223+ name : "error-rate-limit-exceeded" ,
224+ path : "ndt/ndt5" ,
225+ signer : & fakeSigner {},
226+ locator : & fakeLocatorV2 {
227+ targets : []v2.Target {{Machine : "mlab1-lga0t.measurement-lab.org" }},
228+ },
229+ header : http.Header {
230+ "X-Forwarded-For" : []string {"192.0.2.1" },
231+ "User-Agent" : []string {"test-client" },
232+ },
233+ ipLimiter : & fakeRateLimiter {
234+ limited : true ,
235+ },
236+ wantStatus : http .StatusTooManyRequests ,
237+ },
238+ {
239+ name : "success-rate-limit-not-exceeded" ,
240+ path : "ndt/ndt5" ,
241+ signer : & fakeSigner {},
242+ locator : & fakeLocatorV2 {
243+ targets : []v2.Target {{Machine : "mlab1-lga0t.measurement-lab.org" }},
244+ urls : []url.URL {
245+ {Scheme : "ws" , Host : ":3001" , Path : "/ndt_protocol" },
246+ {Scheme : "wss" , Host : ":3010" , Path : "ndt_protocol" },
247+ },
248+ },
249+ header : http.Header {
250+ "X-AppEngine-CityLatLong" : []string {"40.3,-70.4" },
251+ "X-Forwarded-For" : []string {"192.168.1.1" },
252+ "User-Agent" : []string {"test-client" },
253+ },
254+ ipLimiter : & fakeRateLimiter {
255+ limited : false ,
256+ },
257+ wantLatLon : "40.3,-70.4" ,
258+ wantKey : "ws://:3001/ndt_protocol" ,
259+ wantStatus : http .StatusOK ,
260+ },
261+ {
262+ name : "success-rate-limiter-error" ,
263+ path : "ndt/ndt5" ,
264+ signer : & fakeSigner {},
265+ locator : & fakeLocatorV2 {
266+ targets : []v2.Target {{Machine : "mlab1-lga0t.measurement-lab.org" }},
267+ urls : []url.URL {
268+ {Scheme : "ws" , Host : ":3001" , Path : "/ndt_protocol" },
269+ {Scheme : "wss" , Host : ":3010" , Path : "/ndt_protocol" },
270+ },
271+ },
272+ header : http.Header {
273+ "X-AppEngine-CityLatLong" : []string {"40.3,-70.4" },
274+ "X-Forwarded-For" : []string {"192.168.1.1" },
275+ "User-Agent" : []string {"test-client" },
276+ },
277+ ipLimiter : & fakeRateLimiter {
278+ err : errors .New ("redis error" ),
279+ },
280+ wantLatLon : "40.3,-70.4" ,
281+ wantKey : "ws://:3001/ndt_protocol" ,
282+ wantStatus : http .StatusOK , // Should fail open
283+ },
284+ {
285+ name : "success-missing-forwarded-for" ,
286+ path : "ndt/ndt5" ,
287+ signer : & fakeSigner {},
288+ locator : & fakeLocatorV2 {
289+ targets : []v2.Target {{Machine : "mlab1-lga0t.measurement-lab.org" }},
290+ urls : []url.URL {
291+ {Scheme : "ws" , Host : ":3001" , Path : "/ndt_protocol" },
292+ {Scheme : "wss" , Host : ":3010" , Path : "/ndt_protocol" },
293+ },
294+ },
295+ header : http.Header {
296+ "X-AppEngine-CityLatLong" : []string {"40.3,-70.4" },
297+ // No X-Forwarded-For
298+ "User-Agent" : []string {"test-client" },
299+ },
300+ ipLimiter : & fakeRateLimiter {limited : false },
301+ wantLatLon : "40.3,-70.4" ,
302+ wantKey : "ws://:3001/ndt_protocol" ,
303+ wantStatus : http .StatusOK ,
304+ },
209305 }
210306 for _ , tt := range tests {
211307 t .Run (tt .name , func (t * testing.T ) {
212308 if tt .cl == nil {
213309 tt .cl = clientgeo .NewAppEngineLocator ()
214310 }
215- c := NewClient (tt .project , tt .signer , tt .locator , tt .cl , prom .NewAPI (nil ), tt .limits , nil )
311+ c := NewClient (tt .project , tt .signer , tt .locator , tt .cl , prom .NewAPI (nil ), tt .limits , tt . ipLimiter )
216312
217313 mux := http .NewServeMux ()
218314 mux .HandleFunc ("/v2/nearest/" , c .Nearest )
0 commit comments