diff --git a/backend_configuration_test.go b/backend_configuration_test.go index 107c787..252d6e8 100644 --- a/backend_configuration_test.go +++ b/backend_configuration_test.go @@ -32,47 +32,59 @@ import ( func testUrls(t *testing.T, config *BackendConfiguration, valid_urls []string, invalid_urls []string) { for _, u := range valid_urls { - parsed, err := url.ParseRequestURI(u) - if err != nil { - t.Errorf("The url %s should be valid, got %s", u, err) - continue - } - if !config.IsUrlAllowed(parsed) { - t.Errorf("The url %s should be allowed", u) - } - if secret := config.GetSecret(parsed); !bytes.Equal(secret, testBackendSecret) { - t.Errorf("Expected secret %s for url %s, got %s", string(testBackendSecret), u, string(secret)) - } + u := u + t.Run(u, func(t *testing.T) { + parsed, err := url.ParseRequestURI(u) + if err != nil { + t.Errorf("The url %s should be valid, got %s", u, err) + return + } + if !config.IsUrlAllowed(parsed) { + t.Errorf("The url %s should be allowed", u) + } + if secret := config.GetSecret(parsed); !bytes.Equal(secret, testBackendSecret) { + t.Errorf("Expected secret %s for url %s, got %s", string(testBackendSecret), u, string(secret)) + } + }) } for _, u := range invalid_urls { - parsed, _ := url.ParseRequestURI(u) - if config.IsUrlAllowed(parsed) { - t.Errorf("The url %s should not be allowed", u) - } + u := u + t.Run(u, func(t *testing.T) { + parsed, _ := url.ParseRequestURI(u) + if config.IsUrlAllowed(parsed) { + t.Errorf("The url %s should not be allowed", u) + } + }) } } func testBackends(t *testing.T, config *BackendConfiguration, valid_urls [][]string, invalid_urls []string) { for _, entry := range valid_urls { - u := entry[0] - parsed, err := url.ParseRequestURI(u) - if err != nil { - t.Errorf("The url %s should be valid, got %s", u, err) - continue - } - if !config.IsUrlAllowed(parsed) { - t.Errorf("The url %s should be allowed", u) - } - s := entry[1] - if secret := config.GetSecret(parsed); !bytes.Equal(secret, []byte(s)) { - t.Errorf("Expected secret %s for url %s, got %s", string(s), u, string(secret)) - } + entry := entry + t.Run(entry[0], func(t *testing.T) { + u := entry[0] + parsed, err := url.ParseRequestURI(u) + if err != nil { + t.Errorf("The url %s should be valid, got %s", u, err) + return + } + if !config.IsUrlAllowed(parsed) { + t.Errorf("The url %s should be allowed", u) + } + s := entry[1] + if secret := config.GetSecret(parsed); !bytes.Equal(secret, []byte(s)) { + t.Errorf("Expected secret %s for url %s, got %s", string(s), u, string(secret)) + } + }) } for _, u := range invalid_urls { - parsed, _ := url.ParseRequestURI(u) - if config.IsUrlAllowed(parsed) { - t.Errorf("The url %s should not be allowed", u) - } + u := u + t.Run(u, func(t *testing.T) { + parsed, _ := url.ParseRequestURI(u) + if config.IsUrlAllowed(parsed) { + t.Errorf("The url %s should not be allowed", u) + } + }) } } diff --git a/clientsession_test.go b/clientsession_test.go index 028109c..1dadd3d 100644 --- a/clientsession_test.go +++ b/clientsession_test.go @@ -22,6 +22,7 @@ package signaling import ( + "strconv" "testing" ) @@ -111,10 +112,13 @@ func Test_permissionsEqual(t *testing.T) { equal: false, }, } - for _, test := range tests { - equal := permissionsEqual(test.a, test.b) - if equal != test.equal { - t.Errorf("Expected %+v to be %s to %+v but was %s", test.a, equalStrings[test.equal], test.b, equalStrings[equal]) - } + for idx, test := range tests { + test := test + t.Run(strconv.Itoa(idx), func(t *testing.T) { + equal := permissionsEqual(test.a, test.b) + if equal != test.equal { + t.Errorf("Expected %+v to be %s to %+v but was %s", test.a, equalStrings[test.equal], test.b, equalStrings[equal]) + } + }) } } diff --git a/geoip_test.go b/geoip_test.go index cfc4541..8413f29 100644 --- a/geoip_test.go +++ b/geoip_test.go @@ -42,15 +42,19 @@ func testGeoLookupReader(t *testing.T, reader *GeoLookup) { } for ip, expected := range tests { - country, err := reader.LookupCountry(net.ParseIP(ip)) - if err != nil { - t.Errorf("Could not lookup %s: %s", ip, err) - continue - } + ip := ip + expected := expected + t.Run(ip, func(t *testing.T) { + country, err := reader.LookupCountry(net.ParseIP(ip)) + if err != nil { + t.Errorf("Could not lookup %s: %s", ip, err) + return + } - if country != expected { - t.Errorf("Expected %s for %s, got %s", expected, ip, country) - } + if country != expected { + t.Errorf("Expected %s for %s, got %s", expected, ip, country) + } + }) } } @@ -106,17 +110,21 @@ func TestGeoLookupContinent(t *testing.T) { } for country, expected := range tests { - continents := LookupContinents(country) - if len(continents) != len(expected) { - t.Errorf("Continents didn't match for %s: got %s, expected %s", country, continents, expected) - continue - } - for idx, c := range expected { - if continents[idx] != c { + country := country + expected := expected + t.Run(country, func(t *testing.T) { + continents := LookupContinents(country) + if len(continents) != len(expected) { t.Errorf("Continents didn't match for %s: got %s, expected %s", country, continents, expected) - break + return } - } + for idx, c := range expected { + if continents[idx] != c { + t.Errorf("Continents didn't match for %s: got %s, expected %s", country, continents, expected) + break + } + } + }) } } diff --git a/mcu_proxy_test.go b/mcu_proxy_test.go index 29f24cc..ef2ecce 100644 --- a/mcu_proxy_test.go +++ b/mcu_proxy_test.go @@ -76,11 +76,15 @@ func Test_sortConnectionsForCountry(t *testing.T) { } for country, test := range testcases { - sorted := sortConnectionsForCountry(test[0], country) - for idx, conn := range sorted { - if test[1][idx] != conn { - t.Errorf("Index %d for %s: expected %s, got %s", idx, country, test[1][idx].Country(), conn.Country()) + country := country + test := test + t.Run(country, func(t *testing.T) { + sorted := sortConnectionsForCountry(test[0], country) + for idx, conn := range sorted { + if test[1][idx] != conn { + t.Errorf("Index %d for %s: expected %s, got %s", idx, country, test[1][idx].Country(), conn.Country()) + } } - } + }) } }