Browse Source

Use subtests for testing with different values.

pull/115/head
Joachim Bauch 1 week ago
parent
commit
7bdff5ddef
No known key found for this signature in database GPG Key ID: 77C1D22D53E15F02
  1. 76
      backend_configuration_test.go
  2. 14
      clientsession_test.go
  3. 42
      geoip_test.go
  4. 14
      mcu_proxy_test.go

76
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)
}
})
}
}

14
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])
}
})
}
}

42
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
}
}
})
}
}

14
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())
}
}
}
})
}
}
Loading…
Cancel
Save