TestAddressFamilies tests the middleware using different combinations of address families for remote and proxy endpoints.
(t *testing.T)
| 355 | // TestAddressFamilies tests the middleware using different combinations of |
| 356 | // address families for remote and proxy endpoints. |
| 357 | func TestAddressFamilies(t *testing.T) { |
| 358 | t.Parallel() |
| 359 | |
| 360 | for _, clientFamily := range []string{"ipv4", "ipv6"} { |
| 361 | for _, proxyFamily := range []string{"ipv4", "ipv6"} { |
| 362 | for _, header := range []string{"Cf-Connecting-Ip", "True-Client-Ip", "X-Real-Ip", "X-Forwarded-For"} { |
| 363 | name := fmt.Sprintf("%s-%s-%s", strings.ToLower(header), clientFamily, proxyFamily) |
| 364 | |
| 365 | t.Run(name, func(t *testing.T) { |
| 366 | t.Parallel() |
| 367 | |
| 368 | clientAddr := "123.123.123.123" |
| 369 | if clientFamily == "ipv6" { |
| 370 | clientAddr = "2a03:2880:f10c:83:face:b00c:0:25de" |
| 371 | } |
| 372 | |
| 373 | proxyAddr := "4.4.4.4" |
| 374 | if proxyFamily == "ipv6" { |
| 375 | proxyAddr = "2001:4860:4860::8888" |
| 376 | } |
| 377 | |
| 378 | config := &httpmw.RealIPConfig{ |
| 379 | TrustedOrigins: []*net.IPNet{ |
| 380 | { |
| 381 | IP: net.ParseIP("0.0.0.0"), |
| 382 | Mask: net.CIDRMask(0, 32), |
| 383 | }, |
| 384 | { |
| 385 | IP: net.ParseIP("0::"), |
| 386 | Mask: net.CIDRMask(0, 128), |
| 387 | }, |
| 388 | }, |
| 389 | TrustedHeaders: []string{ |
| 390 | "Cf-Connecting-Ip", |
| 391 | "X-Forwarded-For", |
| 392 | "X-Real-Ip", |
| 393 | "True-Client-Ip", |
| 394 | }, |
| 395 | } |
| 396 | |
| 397 | middleware := httpmw.ExtractRealIP(config) |
| 398 | |
| 399 | req := httptest.NewRequest(http.MethodGet, "http://localhost", nil) |
| 400 | req.Header.Set(header, clientAddr) |
| 401 | req.RemoteAddr = proxyAddr |
| 402 | |
| 403 | handlerCalled := false |
| 404 | |
| 405 | nextHandler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 406 | require.Equal(t, clientAddr, req.RemoteAddr, "remote address should match remote client") |
| 407 | |
| 408 | handlerCalled = true |
| 409 | }) |
| 410 | |
| 411 | middleware(nextHandler).ServeHTTP(httptest.NewRecorder(), req) |
| 412 | |
| 413 | require.True(t, handlerCalled, "expected handler to be invoked") |
| 414 | }) |
nothing calls this directly
no test coverage detected