Add tests for IsLocationInLocationList

This commit is contained in:
Alvaro Aleman 2018-03-18 00:30:37 +01:00
parent f0ec20ddec
commit 467ff9924c
No known key found for this signature in database
GPG key ID: D9D78F2AEF6D1EDF
2 changed files with 26 additions and 0 deletions

View file

@ -519,6 +519,9 @@ func isLocationInLocationList(location interface{}, rawLocationList string) bool
for _, locationListItem := range locationList {
locationListItem = strings.TrimLeft(locationListItem, "- ")
if locationListItem == "" {
continue
}
if strings.HasPrefix(loc.Path, locationListItem) {
return true
}

View file

@ -454,3 +454,26 @@ func TestBuildAuthSignURL(t *testing.T) {
}
}
}
func TestIsLocationInLocationList(t *testing.T) {
testCases := []struct {
location *ingress.Location
rawLocationList string
expected bool
}{
{&ingress.Location{Path: "/match"}, "- /match", true},
{&ingress.Location{Path: "/match"}, "\n- /match", true},
{&ingress.Location{Path: "/match"}, "- /dontmatch", false},
{&ingress.Location{Path: "/match"}, "\n- /dontmatch", false},
{&ingress.Location{Path: "/match"}, "- /dontmatch\n- /match", true},
{&ingress.Location{Path: "/match"}, "\n- /dontmatch\n- /dontmatcheither", false},
}
for _, testCase := range testCases {
result := isLocationInLocationList(testCase.location, testCase.rawLocationList)
if result != testCase.expected {
t.Errorf(" expected %v but return %v, path: '%s', rawLocation: '%s'", testCase.expected, result, testCase.location.Path, testCase.rawLocationList)
}
}
}