Use subtests for testing with different values.
This commit is contained in:
parent
04d315b0a4
commit
7bdff5ddef
|
@ -32,47 +32,59 @@ 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 {
|
||||||
parsed, err := url.ParseRequestURI(u)
|
u := u
|
||||||
if err != nil {
|
t.Run(u, func(t *testing.T) {
|
||||||
t.Errorf("The url %s should be valid, got %s", u, err)
|
parsed, err := url.ParseRequestURI(u)
|
||||||
continue
|
if err != nil {
|
||||||
}
|
t.Errorf("The url %s should be valid, got %s", u, err)
|
||||||
if !config.IsUrlAllowed(parsed) {
|
return
|
||||||
t.Errorf("The url %s should be allowed", u)
|
}
|
||||||
}
|
if !config.IsUrlAllowed(parsed) {
|
||||||
if secret := config.GetSecret(parsed); !bytes.Equal(secret, testBackendSecret) {
|
t.Errorf("The url %s should be allowed", u)
|
||||||
t.Errorf("Expected secret %s for url %s, got %s", string(testBackendSecret), u, string(secret))
|
}
|
||||||
}
|
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 {
|
for _, u := range invalid_urls {
|
||||||
parsed, _ := url.ParseRequestURI(u)
|
u := u
|
||||||
if config.IsUrlAllowed(parsed) {
|
t.Run(u, func(t *testing.T) {
|
||||||
t.Errorf("The url %s should not be allowed", u)
|
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) {
|
func testBackends(t *testing.T, config *BackendConfiguration, valid_urls [][]string, invalid_urls []string) {
|
||||||
for _, entry := range valid_urls {
|
for _, entry := range valid_urls {
|
||||||
u := entry[0]
|
entry := entry
|
||||||
parsed, err := url.ParseRequestURI(u)
|
t.Run(entry[0], func(t *testing.T) {
|
||||||
if err != nil {
|
u := entry[0]
|
||||||
t.Errorf("The url %s should be valid, got %s", u, err)
|
parsed, err := url.ParseRequestURI(u)
|
||||||
continue
|
if err != nil {
|
||||||
}
|
t.Errorf("The url %s should be valid, got %s", u, err)
|
||||||
if !config.IsUrlAllowed(parsed) {
|
return
|
||||||
t.Errorf("The url %s should be allowed", u)
|
}
|
||||||
}
|
if !config.IsUrlAllowed(parsed) {
|
||||||
s := entry[1]
|
t.Errorf("The url %s should be allowed", u)
|
||||||
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))
|
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 {
|
for _, u := range invalid_urls {
|
||||||
parsed, _ := url.ParseRequestURI(u)
|
u := u
|
||||||
if config.IsUrlAllowed(parsed) {
|
t.Run(u, func(t *testing.T) {
|
||||||
t.Errorf("The url %s should not be allowed", u)
|
parsed, _ := url.ParseRequestURI(u)
|
||||||
}
|
if config.IsUrlAllowed(parsed) {
|
||||||
|
t.Errorf("The url %s should not be allowed", u)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 {
|
||||||
equal := permissionsEqual(test.a, test.b)
|
test := test
|
||||||
if equal != test.equal {
|
t.Run(strconv.Itoa(idx), func(t *testing.T) {
|
||||||
t.Errorf("Expected %+v to be %s to %+v but was %s", test.a, equalStrings[test.equal], test.b, equalStrings[equal])
|
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])
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,15 +42,19 @@ func testGeoLookupReader(t *testing.T, reader *GeoLookup) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for ip, expected := range tests {
|
for ip, expected := range tests {
|
||||||
country, err := reader.LookupCountry(net.ParseIP(ip))
|
ip := ip
|
||||||
if err != nil {
|
expected := expected
|
||||||
t.Errorf("Could not lookup %s: %s", ip, err)
|
t.Run(ip, func(t *testing.T) {
|
||||||
continue
|
country, err := reader.LookupCountry(net.ParseIP(ip))
|
||||||
}
|
if err != nil {
|
||||||
|
t.Errorf("Could not lookup %s: %s", ip, err)
|
||||||
|
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,17 +110,21 @@ func TestGeoLookupContinent(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for country, expected := range tests {
|
for country, expected := range tests {
|
||||||
continents := LookupContinents(country)
|
country := country
|
||||||
if len(continents) != len(expected) {
|
expected := expected
|
||||||
t.Errorf("Continents didn't match for %s: got %s, expected %s", country, continents, expected)
|
t.Run(country, func(t *testing.T) {
|
||||||
continue
|
continents := LookupContinents(country)
|
||||||
}
|
if len(continents) != len(expected) {
|
||||||
for idx, c := range expected {
|
|
||||||
if continents[idx] != c {
|
|
||||||
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)
|
||||||
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -76,11 +76,15 @@ func Test_sortConnectionsForCountry(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for country, test := range testcases {
|
for country, test := range testcases {
|
||||||
sorted := sortConnectionsForCountry(test[0], country)
|
country := country
|
||||||
for idx, conn := range sorted {
|
test := test
|
||||||
if test[1][idx] != conn {
|
t.Run(country, func(t *testing.T) {
|
||||||
t.Errorf("Index %d for %s: expected %s, got %s", idx, country, test[1][idx].Country(), conn.Country())
|
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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue