(t *testing.T)
| 404 | } |
| 405 | |
| 406 | func TestGetSourceIPsWithCustomRegex(t *testing.T) { |
| 407 | tests := []struct { |
| 408 | name string |
| 409 | req *http.Request |
| 410 | want string |
| 411 | }{ |
| 412 | { |
| 413 | name: "no header", |
| 414 | req: &http.Request{RemoteAddr: "192.168.1.100:3454"}, |
| 415 | want: "192.168.1.100", |
| 416 | }, |
| 417 | { |
| 418 | name: "No matching entry in the header", |
| 419 | req: &http.Request{ |
| 420 | RemoteAddr: "192.168.1.100:3454", |
| 421 | Header: map[string][]string{ |
| 422 | http.CanonicalHeaderKey("SomeHeader"): {"not matching"}, |
| 423 | }, |
| 424 | }, |
| 425 | want: "192.168.1.100", |
| 426 | }, |
| 427 | { |
| 428 | name: "one matching entry in the header", |
| 429 | req: &http.Request{ |
| 430 | RemoteAddr: "192.168.1.100:3454", |
| 431 | Header: map[string][]string{ |
| 432 | http.CanonicalHeaderKey("SomeHeader"): {"172.16.1.1"}, |
| 433 | }, |
| 434 | }, |
| 435 | want: "172.16.1.1, 192.168.1.100", |
| 436 | }, |
| 437 | { |
| 438 | name: "multiple matching entries in the header, only first used", |
| 439 | req: &http.Request{ |
| 440 | RemoteAddr: "192.168.1.100:3454", |
| 441 | Header: map[string][]string{ |
| 442 | http.CanonicalHeaderKey("SomeHeader"): {"172.16.1.1,172.16.2.1"}, |
| 443 | }, |
| 444 | }, |
| 445 | want: "172.16.1.1, 192.168.1.100", |
| 446 | }, |
| 447 | } |
| 448 | for _, tt := range tests { |
| 449 | t.Run(tt.name, func(t *testing.T) { |
| 450 | sourceIPs, err := NewSourceIPs("SomeHeader", "((?:[0-9]{1,3}\\.){3}[0-9]{1,3})", false) |
| 451 | require.NoError(t, err) |
| 452 | |
| 453 | if got := sourceIPs.Get(tt.req); got != tt.want { |
| 454 | t.Errorf("GetSource() = %v, want %v", got, tt.want) |
| 455 | } |
| 456 | }) |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | func TestGetSourceIPsWithCustomRegexFullExtract(t *testing.T) { |
| 461 | tests := []struct { |
nothing calls this directly
no test coverage detected