MCPcopy
hub / github.com/grafana/dskit / TestGetSourceIPsWithCustomRegex

Function TestGetSourceIPsWithCustomRegex

middleware/source_ips_test.go:406–458  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

404}
405
406func 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
460func TestGetSourceIPsWithCustomRegexFullExtract(t *testing.T) {
461 tests := []struct {

Callers

nothing calls this directly

Calls 4

NewSourceIPsFunction · 0.85
RunMethod · 0.80
ErrorfMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected