Add a helper for checking for all expected responses. (#1676)

This commit is contained in:
Matt Moore 2020-09-01 10:20:15 -07:00 committed by GitHub
parent 383dd08a32
commit edd1b5e0be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 0 deletions

View File

@ -24,8 +24,10 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"
"k8s.io/apimachinery/pkg/util/sets"
"knative.dev/pkg/test/logging"
"knative.dev/pkg/test/spoof"
)
@ -83,6 +85,33 @@ func IsStatusOK(resp *spoof.Response) (bool, error) {
return IsOneOfStatusCodes(http.StatusOK)(resp)
}
// MatchesAllBodies checks that the *first* response body matches the "expected" body, otherwise failing.
func MatchesAllBodies(all ...string) spoof.ResponseChecker {
var m sync.Mutex
seen := make(sets.String, len(all))
return func(resp *spoof.Response) (bool, error) {
for _, expected := range all {
if !strings.Contains(string(resp.Body), expected) {
// See if the next one matches.
continue
}
// Protect our set
m.Lock()
defer m.Unlock()
seen.Insert(expected)
// Stop once we've seen them all.
return seen.HasAll(all...), nil
}
// Returning (true, err) causes SpoofingClient.Poll to fail.
return true, fmt.Errorf("body = %s, want one of: %s", string(resp.Body), all)
}
}
// MatchesBody checks that the *first* response body matches the "expected" body, otherwise failing.
func MatchesBody(expected string) spoof.ResponseChecker {
return func(resp *spoof.Response) (bool, error) {