From adc7b284ebcd7f092428f71c1c212b0913e41725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mazeau?= Date: Thu, 9 Jun 2022 17:34:33 +0200 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Gabriel Aszalos --- contrib/internal/httptrace/httptrace.go | 8 ++------ contrib/internal/httptrace/options_test.go | 14 ++++++++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/contrib/internal/httptrace/httptrace.go b/contrib/internal/httptrace/httptrace.go index f5c2dc5947..0b16f0ffc9 100644 --- a/contrib/internal/httptrace/httptrace.go +++ b/contrib/internal/httptrace/httptrace.go @@ -95,21 +95,17 @@ func getClientIP(remoteAddr string, headers http.Header, clientIPHeader string) ipHeaders = []string{clientIPHeader} } check := func(value string) netaddr.IP { - for _, ip := range strings.Split(value, ",") { - ipStr := strings.Trim(ip, " ") - ip := parseIP(ipStr) - + for _, ipstr := range strings.Split(value, ",") { + ip := parseIP(strings.TrimSpace(ipstr)) if !ip.IsValid() { continue } - if isGlobal(ip) { return ip } } return netaddr.IP{} } - for _, hdr := range ipHeaders { if value := headers.Get(hdr); value != "" { if ip := check(value); ip.IsValid() { diff --git a/contrib/internal/httptrace/options_test.go b/contrib/internal/httptrace/options_test.go index 2e33ba3879..10b31d7f3c 100644 --- a/contrib/internal/httptrace/options_test.go +++ b/contrib/internal/httptrace/options_test.go @@ -13,13 +13,18 @@ import ( ) func TestConfig(t *testing.T) { - t.Run("config", func(t *testing.T) { - t.Run("client-ip-header-unset", func(t *testing.T) { + t.Run("client-ip", func(t *testing.T) { + t.Run("unset", func(t *testing.T) { + restore := cleanEnv() + err := os.Unsetenv(clientIPHeaderEnvVar) + require.NoError(t, err) cfg := newConfig() require.Empty(t, cfg.ipHeader) + defer restore() }) - t.Run("client-ip-header-empty", func(t *testing.T) { + + t.Run("empty", func(t *testing.T) { restore := cleanEnv() err := os.Setenv(clientIPHeaderEnvVar, "") require.NoError(t, err) @@ -28,7 +33,8 @@ func TestConfig(t *testing.T) { defer restore() }) - t.Run("client-ip-header-set", func(t *testing.T) { + + t.Run("set", func(t *testing.T) { restore := cleanEnv() err := os.Setenv(clientIPHeaderEnvVar, "custom-header") require.NoError(t, err)