@@ -318,22 +318,60 @@ func TestCORS(t *testing.T) {
318
318
}
319
319
320
320
func TestIP (t * testing.T ) {
321
- r , _ := http .NewRequest ("GET" , "/ip" , nil )
322
- r .RemoteAddr = "192.168.0.100"
323
- w := httptest .NewRecorder ()
324
- handler .ServeHTTP (w , r )
321
+ t .Parallel ()
325
322
326
- assertStatusCode (t , w , http .StatusOK )
327
- assertContentType (t , w , jsonContentType )
328
-
329
- var resp * ipResponse
330
- err := json .Unmarshal (w .Body .Bytes (), & resp )
331
- if err != nil {
332
- t .Fatalf ("failed to unmarshal body %s from JSON: %s" , w .Body , err )
323
+ testCases := map [string ]struct {
324
+ remoteAddr string
325
+ headers map [string ]string
326
+ wantOrigin string
327
+ }{
328
+ "remote addr used if no x-forwarded-for" : {
329
+ remoteAddr : "192.168.0.100" ,
330
+ wantOrigin : "192.168.0.100" ,
331
+ },
332
+ "remote addr used if x-forwarded-for empty" : {
333
+ remoteAddr : "192.168.0.100" ,
334
+ headers : map [string ]string {"X-Forwarded-For" : "" },
335
+ wantOrigin : "192.168.0.100" ,
336
+ },
337
+ "first entry in x-forwarded-for used if present" : {
338
+ remoteAddr : "192.168.0.100" ,
339
+ headers : map [string ]string {"X-Forwarded-For" : "10.1.1.1, 10.2.2.2, 10.3.3.3" },
340
+ wantOrigin : "10.1.1.1" ,
341
+ },
342
+ "single entry x-forwarded-for ok" : {
343
+ remoteAddr : "192.168.0.100" ,
344
+ headers : map [string ]string {"X-Forwarded-For" : "10.1.1.1" },
345
+ wantOrigin : "10.1.1.1" ,
346
+ },
333
347
}
334
348
335
- if resp .Origin != r .RemoteAddr {
336
- t .Fatalf ("%#v != %#v" , resp .Origin , r .RemoteAddr )
349
+ for name , tc := range testCases {
350
+ tc := tc
351
+ t .Run (name , func (t * testing.T ) {
352
+ t .Parallel ()
353
+
354
+ r , _ := http .NewRequest ("GET" , "/ip" , nil )
355
+ r .RemoteAddr = tc .remoteAddr
356
+ for k , v := range tc .headers {
357
+ r .Header .Set (k , v )
358
+ }
359
+ w := httptest .NewRecorder ()
360
+ handler .ServeHTTP (w , r )
361
+
362
+ assertStatusCode (t , w , http .StatusOK )
363
+ assertContentType (t , w , jsonContentType )
364
+
365
+ var resp * ipResponse
366
+ err := json .Unmarshal (w .Body .Bytes (), & resp )
367
+ if err != nil {
368
+ t .Fatalf ("failed to unmarshal body %s from JSON: %s" , w .Body , err )
369
+ }
370
+
371
+ if resp .Origin != tc .wantOrigin {
372
+ t .Fatalf ("got %q, want %q" , resp .Origin , tc .wantOrigin )
373
+ }
374
+ })
337
375
}
338
376
}
339
377
0 commit comments