-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_matcher.go
46 lines (38 loc) · 1.24 KB
/
query_matcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package hex
import (
"fmt"
"net/http"
)
// WithQuery matches against the query string.
// It has several forms:
//
// WithQuery() // passes if any query string is present
// WithQuery("key") // passes ?key and ?key=<any value>
// WithQuery("key", "value") // passes ?key=value or &key=value&key=value2
// WithQuery(hex.R(`^key$`)) // find keys using regular expressions
// WithQuery(hex.P{"key1": "value1", "key2": "value2"}) // match against multiple key/value pairs
// WithQuery(hex.P{"key1": hex.R(`^value\d$`)}) // mix-and-match strings, regular expressions and key/value maps
func (exp *Expectation) WithQuery(args ...interface{}) *Expectation {
matcher, err := makeURLValuesMatcher(args)
if err != nil {
panic(fmt.Sprintf("WithQuery: %s", err.Error()))
}
exp.matchers = append(exp.matchers, &queryMatcher{
args: args,
urlValuesMatcher: matcher,
})
return exp
}
type queryMatcher struct {
args []interface{}
exact bool
urlValuesMatcher
}
func (q *queryMatcher) matches(req *http.Request) bool {
query := req.URL.Query()
return q.urlValuesMatcher.matches(query)
}
func (q *queryMatcher) String() string {
return fmt.Sprintf("query string matching %s", matcherArgsToString(q.args))
}
var _ matcher = &queryMatcher{}