Use subtests for testing with different values.

This commit is contained in:
Joachim Bauch 2021-06-07 10:08:28 +02:00
parent 04d315b0a4
commit 7bdff5ddef
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
4 changed files with 87 additions and 59 deletions

View file

@ -32,10 +32,12 @@ import (
func testUrls(t *testing.T, config *BackendConfiguration, valid_urls []string, invalid_urls []string) { func testUrls(t *testing.T, config *BackendConfiguration, valid_urls []string, invalid_urls []string) {
for _, u := range valid_urls { for _, u := range valid_urls {
u := u
t.Run(u, func(t *testing.T) {
parsed, err := url.ParseRequestURI(u) parsed, err := url.ParseRequestURI(u)
if err != nil { if err != nil {
t.Errorf("The url %s should be valid, got %s", u, err) t.Errorf("The url %s should be valid, got %s", u, err)
continue return
} }
if !config.IsUrlAllowed(parsed) { if !config.IsUrlAllowed(parsed) {
t.Errorf("The url %s should be allowed", u) t.Errorf("The url %s should be allowed", u)
@ -43,22 +45,28 @@ func testUrls(t *testing.T, config *BackendConfiguration, valid_urls []string, i
if secret := config.GetSecret(parsed); !bytes.Equal(secret, testBackendSecret) { if secret := config.GetSecret(parsed); !bytes.Equal(secret, testBackendSecret) {
t.Errorf("Expected secret %s for url %s, got %s", string(testBackendSecret), u, string(secret)) t.Errorf("Expected secret %s for url %s, got %s", string(testBackendSecret), u, string(secret))
} }
})
} }
for _, u := range invalid_urls { for _, u := range invalid_urls {
u := u
t.Run(u, func(t *testing.T) {
parsed, _ := url.ParseRequestURI(u) parsed, _ := url.ParseRequestURI(u)
if config.IsUrlAllowed(parsed) { if config.IsUrlAllowed(parsed) {
t.Errorf("The url %s should not be allowed", u) t.Errorf("The url %s should not be allowed", u)
} }
})
} }
} }
func testBackends(t *testing.T, config *BackendConfiguration, valid_urls [][]string, invalid_urls []string) { func testBackends(t *testing.T, config *BackendConfiguration, valid_urls [][]string, invalid_urls []string) {
for _, entry := range valid_urls { for _, entry := range valid_urls {
entry := entry
t.Run(entry[0], func(t *testing.T) {
u := entry[0] u := entry[0]
parsed, err := url.ParseRequestURI(u) parsed, err := url.ParseRequestURI(u)
if err != nil { if err != nil {
t.Errorf("The url %s should be valid, got %s", u, err) t.Errorf("The url %s should be valid, got %s", u, err)
continue return
} }
if !config.IsUrlAllowed(parsed) { if !config.IsUrlAllowed(parsed) {
t.Errorf("The url %s should be allowed", u) t.Errorf("The url %s should be allowed", u)
@ -67,12 +75,16 @@ func testBackends(t *testing.T, config *BackendConfiguration, valid_urls [][]str
if secret := config.GetSecret(parsed); !bytes.Equal(secret, []byte(s)) { 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)) t.Errorf("Expected secret %s for url %s, got %s", string(s), u, string(secret))
} }
})
} }
for _, u := range invalid_urls { for _, u := range invalid_urls {
u := u
t.Run(u, func(t *testing.T) {
parsed, _ := url.ParseRequestURI(u) parsed, _ := url.ParseRequestURI(u)
if config.IsUrlAllowed(parsed) { if config.IsUrlAllowed(parsed) {
t.Errorf("The url %s should not be allowed", u) t.Errorf("The url %s should not be allowed", u)
} }
})
} }
} }

View file

@ -22,6 +22,7 @@
package signaling package signaling
import ( import (
"strconv"
"testing" "testing"
) )
@ -111,10 +112,13 @@ func Test_permissionsEqual(t *testing.T) {
equal: false, equal: false,
}, },
} }
for _, test := range tests { for idx, test := range tests {
test := test
t.Run(strconv.Itoa(idx), func(t *testing.T) {
equal := permissionsEqual(test.a, test.b) equal := permissionsEqual(test.a, test.b)
if equal != test.equal { 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]) t.Errorf("Expected %+v to be %s to %+v but was %s", test.a, equalStrings[test.equal], test.b, equalStrings[equal])
} }
})
} }
} }

View file

@ -42,15 +42,19 @@ func testGeoLookupReader(t *testing.T, reader *GeoLookup) {
} }
for ip, expected := range tests { for ip, expected := range tests {
ip := ip
expected := expected
t.Run(ip, func(t *testing.T) {
country, err := reader.LookupCountry(net.ParseIP(ip)) country, err := reader.LookupCountry(net.ParseIP(ip))
if err != nil { if err != nil {
t.Errorf("Could not lookup %s: %s", ip, err) t.Errorf("Could not lookup %s: %s", ip, err)
continue return
} }
if country != expected { if country != expected {
t.Errorf("Expected %s for %s, got %s", expected, ip, country) t.Errorf("Expected %s for %s, got %s", expected, ip, country)
} }
})
} }
} }
@ -106,10 +110,13 @@ func TestGeoLookupContinent(t *testing.T) {
} }
for country, expected := range tests { for country, expected := range tests {
country := country
expected := expected
t.Run(country, func(t *testing.T) {
continents := LookupContinents(country) continents := LookupContinents(country)
if len(continents) != len(expected) { if len(continents) != len(expected) {
t.Errorf("Continents didn't match for %s: got %s, expected %s", country, continents, expected) t.Errorf("Continents didn't match for %s: got %s, expected %s", country, continents, expected)
continue return
} }
for idx, c := range expected { for idx, c := range expected {
if continents[idx] != c { if continents[idx] != c {
@ -117,6 +124,7 @@ func TestGeoLookupContinent(t *testing.T) {
break break
} }
} }
})
} }
} }

View file

@ -76,11 +76,15 @@ func Test_sortConnectionsForCountry(t *testing.T) {
} }
for country, test := range testcases { for country, test := range testcases {
country := country
test := test
t.Run(country, func(t *testing.T) {
sorted := sortConnectionsForCountry(test[0], country) sorted := sortConnectionsForCountry(test[0], country)
for idx, conn := range sorted { for idx, conn := range sorted {
if test[1][idx] != conn { if test[1][idx] != conn {
t.Errorf("Index %d for %s: expected %s, got %s", idx, country, test[1][idx].Country(), conn.Country()) t.Errorf("Index %d for %s: expected %s, got %s", idx, country, test[1][idx].Country(), conn.Country())
} }
} }
})
} }
} }