Documentation ¶
Overview ¶
Package predicate defines an interface named Predicate that has a function named Accept that returns true or false based on the value passed to it. The package also defines a set of predicates useful for making decisions about http.Request objects. For instance, the QueryParameterEquals(name, value string) will return a Predicate that expects a *http.Request and will return true if the value of query parameter named 'name' equals 'value'.
Index ¶
- type Predicate
- func And(predicates ...Predicate) Predicate
- func ExtractedValueAccepted(extractor Extractor, predicate Predicate) Predicate
- func False() Predicate
- func HeaderContains(name string, path string) Predicate
- func HeaderContainsIgnoreCase(name string, path string) Predicate
- func HeaderEquals(name string, value string) Predicate
- func HeaderEqualsIgnoreCase(name string, path string) Predicate
- func HeaderMatches(name string, regex *regexp.Regexp) Predicate
- func HeaderStartsWith(name string, path string) Predicate
- func MethodIs(method string) Predicate
- func Not(predicate Predicate) Predicate
- func Or(predicates ...Predicate) Predicate
- func PathEquals(path string) Predicate
- func PathMatches(pathRegex *regexp.Regexp) Predicate
- func PathStartsWith(path string) Predicate
- func QueryParamContains(name, value string) Predicate
- func QueryParamContainsIgnoreCase(name, value string) Predicate
- func QueryParamEquals(name, value string) Predicate
- func QueryParamEqualsIgnoreCase(name, value string) Predicate
- func QueryParamMatches(name string, pattern *regexp.Regexp) Predicate
- func QueryParamStartsWith(name string, prefix string) Predicate
- func RequestURIEquals(path string) Predicate
- func RequestURIMatches(pathRegex *regexp.Regexp) Predicate
- func RequestURIStartsWith(path string) Predicate
- func StringContains(value string) Predicate
- func StringEndsWith(value string) Predicate
- func StringEquals(value string) Predicate
- func StringMatches(regex *regexp.Regexp) Predicate
- func StringStartsWith(value string) Predicate
- func True() Predicate
- type PredicateFunc
Examples ¶
- And
- HeaderContains
- HeaderContainsIgnoreCase
- HeaderEquals
- HeaderEqualsIgnoreCase
- HeaderMatches
- HeaderStartsWith
- Not
- Or
- PathEquals
- PathMatches
- PathStartsWith
- QueryParamContains
- QueryParamContainsIgnoreCase
- QueryParamEquals
- QueryParamEqualsIgnoreCase
- QueryParamMatches
- QueryParamStartsWith
- RequestURIEquals
- RequestURIMatches
- RequestURIStartsWith
- StringContains
- StringEndsWith
- StringEquals
- StringMatches
- StringStartsWith
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Predicate ¶
type Predicate interface {
Accept(interface{}) bool
}
Predicate is a class that can accept or reject a value based on some condition.
func And ¶
And returns a predicate that is true if all of the passed predicate are true for the input. Furthermore, it stops executing predicates after the first false one.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/predicate" ) func main() { fmt.Printf("%v\n", predicate.And(predicate.True(), predicate.True()).Accept(nil)) fmt.Printf("%v\n", predicate.And(predicate.False(), predicate.True()).Accept(nil)) }
Output: true false
func ExtractedValueAccepted ¶
ExtractedValueAccepted returns A predicate that extracts a value using the Extractor and passes that value to the provided predicate
func False ¶
func False() Predicate
FalsePredicate is a predicate that returns false for all inputs.
func HeaderContains ¶
HeaderContains returns a predicate that returns true if the header named 'name' contains 'value'.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/predicate" "net/http" ) func main() { req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) req.Header.Add("FOO", "FOOBAR") fmt.Printf("%v\n", predicate.HeaderContainsIgnoreCase("FOO", "BAR").Accept(req)) fmt.Printf("%v\n", predicate.HeaderContainsIgnoreCase("FOO", "snafu").Accept(req)) }
Output: true false
func HeaderContainsIgnoreCase ¶
HeaderContainsIgnoreCase returns a predicate that returns true if the header named 'name' contains 'value', ignoring case.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/predicate" "net/http" ) func main() { req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) req.Header.Add("FOO", "FOOBAR") fmt.Printf("%v\n", predicate.HeaderContainsIgnoreCase("FOO", "bar").Accept(req)) fmt.Printf("%v\n", predicate.HeaderContainsIgnoreCase("FOO", "snafu").Accept(req)) }
Output: true false
func HeaderEquals ¶
HeaderEquals returns a predicate that returns true if the header named 'name' equals 'value'
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/predicate" "net/http" ) func main() { req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) req.Header.Add("FOO", "FOOBAR") fmt.Printf("%v\n", predicate.HeaderEquals("FOO", "FOOBAR").Accept(req)) fmt.Printf("%v\n", predicate.HeaderEquals("FOO", "snafu").Accept(req)) }
Output: true false
func HeaderEqualsIgnoreCase ¶
HeaderEqualsIgnoreCase returns a predicate that returns true if the header named 'name' equals 'value', ignoring case.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/predicate" "net/http" ) func main() { req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) req.Header.Add("FOO", "FOOBAR") fmt.Printf("%v\n", predicate.HeaderEqualsIgnoreCase("FOO", "FooBar").Accept(req)) fmt.Printf("%v\n", predicate.HeaderEqualsIgnoreCase("FOO", "snafu").Accept(req)) }
Output: true false
func HeaderMatches ¶
HeaderMatches returns a predicate that returns true if the header named 'name' matches 'regex'
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/predicate" "net/http" "regexp" ) func main() { truePattern := regexp.MustCompile("FO{2}B.*") falsePattern := regexp.MustCompile("FO{3}B.*") req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) req.Header.Add("FOO", "FOOBAR") fmt.Printf("%v\n", predicate.HeaderMatches("FOO", truePattern).Accept(req)) fmt.Printf("%v\n", predicate.HeaderMatches("FOO", falsePattern).Accept(req)) }
Output: true false
func HeaderStartsWith ¶
HeaderStartsWith returns a predicate that returns true if the header named 'name' starts with 'value'.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/predicate" "net/http" ) func main() { req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) req.Header.Add("FOO", "FOOBAR") fmt.Printf("%v\n", predicate.HeaderStartsWith("FOO", "FOO").Accept(req)) fmt.Printf("%v\n", predicate.HeaderStartsWith("FOO", "snafu").Accept(req)) }
Output: true false
func MethodIs ¶
MethodIs returns a predicate that takes a request, extracts the method, and returns true if it equals the method provided, ignoring case.
func Not ¶
Not returns a predicate that negates the condition defined by the passed predicate.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/predicate" ) func main() { fmt.Printf("%v\n", predicate.Not(predicate.True()).Accept(nil)) fmt.Printf("%v\n", predicate.Not(predicate.False()).Accept(nil)) }
Output: false true
func Or ¶
Or returns a predicate that is true if any of the passed predicate are true. Furthermore, it stops executing predicates after the first true one.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/predicate" ) func main() { fmt.Printf("%v\n", predicate.Or(predicate.False(), predicate.False()).Accept(nil)) fmt.Printf("%v\n", predicate.Or(predicate.False(), predicate.True()).Accept(nil)) }
Output: false true
func PathEquals ¶
PathEquals returns a predicate that returns true if the path equals 'path'
Example ¶
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) fmt.Printf("%v\n", PathEquals("/test/foo/bar").Accept(req)) fmt.Printf("%v\n", PathEquals("/test/bar/foo").Accept(req))
Output: true false
func PathMatches ¶
PathMatches returns a predicate that returns true if the path matches the pathRegex.
Example ¶
truePattern := regexp.MustCompile("fo{2}/bar") falsePattern := regexp.MustCompile("bar/fo{2}") req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) fmt.Printf("%v\n", PathMatches(truePattern).Accept(req)) fmt.Printf("%v\n", PathMatches(falsePattern).Accept(req))
Output: true false
func PathStartsWith ¶
PathStartsWith returns a predicate that returns true if the path starts with 'path'
Example ¶
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) fmt.Printf("%v\n", PathStartsWith("/test/foo/").Accept(req)) fmt.Printf("%v\n", PathStartsWith("/test/bar/").Accept(req))
Output: true false
func QueryParamContains ¶
QueryParamContainsIgnoreCase returns a Predicate that takes a request, extracts the query parameter specified and returns true if it contains the value provided.
Example ¶
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=foobar&l=3", nil) fmt.Printf("%v\n", QueryParamContains("q", "oob").Accept(req)) fmt.Printf("%v\n", QueryParamContains("q", "snafu").Accept(req)) fmt.Printf("%v\n", QueryParamContains("x", "oob").Accept(req))
Output: true false false
func QueryParamContainsIgnoreCase ¶
QueryParamContainsIgnoreCase returns a Predicate that takes a request, extracts the query parameter specified and returns true if it contains the value provided, ignoring case.
Example ¶
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=foobar&l=3", nil) fmt.Printf("%v\n", QueryParamContainsIgnoreCase("q", "OoB").Accept(req)) fmt.Printf("%v\n", QueryParamContainsIgnoreCase("q", "snafu").Accept(req)) fmt.Printf("%v\n", QueryParamContainsIgnoreCase("x", "OoB").Accept(req))
Output: true false false
func QueryParamEquals ¶
QueryParamContainsIgnoreCase returns a Predicate that takes a request, extracts the query parameter specified and returns true if it equals the value provided.
Example ¶
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=foobar&l=3", nil) fmt.Printf("%v\n", QueryParamEquals("q", "foobar").Accept(req)) fmt.Printf("%v\n", QueryParamEquals("q", "snafu").Accept(req)) fmt.Printf("%v\n", QueryParamEquals("x", "foobar").Accept(req))
Output: true false false
func QueryParamEqualsIgnoreCase ¶
QueryParamContainsIgnoreCase returns a Predicate that takes a request, extracts the query parameter specified and returns true if it equals the value provided, ignoring case.
Example ¶
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=foobar&l=3", nil) fmt.Printf("%v\n", QueryParamEqualsIgnoreCase("q", "FooBar").Accept(req)) fmt.Printf("%v\n", QueryParamEqualsIgnoreCase("q", "Snafu").Accept(req)) fmt.Printf("%v\n", QueryParamEqualsIgnoreCase("x", "FooBar").Accept(req))
Output: true false false
func QueryParamMatches ¶
QueryParamMatches returns a Predicate that takes a request, extracts the query parameter specified and returns true if the value matches the pattern provided.
Example ¶
truePattern := regexp.MustCompile("fo{2}bar") falsePattern := regexp.MustCompile("barfo{2}") req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=foobar&l=3", nil) fmt.Printf("%v\n", QueryParamMatches("q", truePattern).Accept(req)) fmt.Printf("%v\n", QueryParamMatches("q", falsePattern).Accept(req))
Output: true false
func QueryParamStartsWith ¶
QueryParamStartsWith returns a Predicate that takes a request, extracts the query parameter specified and returns true if the value starts with the prefix provided.
Example ¶
req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=foobar&l=3", nil) fmt.Printf("%v\n", QueryParamStartsWith("q", "foo").Accept(req)) fmt.Printf("%v\n", QueryParamStartsWith("q", "snafu").Accept(req)) fmt.Printf("%v\n", QueryParamStartsWith("x", "foo").Accept(req))
Output: true false false
func RequestURIEquals ¶
RequestURIEquals returns a predicate that returns true if the request URI equals the path.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/predicate" "net/http" ) func main() { req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) fmt.Printf("%v\n", predicate.RequestURIEquals("/test/foo/bar?q=5&l=3").Accept(req)) fmt.Printf("%v\n", predicate.RequestURIEquals("/test/foo/bar?q=6&l=3").Accept(req)) }
Output: true false
func RequestURIMatches ¶
RequestURIMatches returns a predicate that returns true if the request URI matches the pathRegex.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/predicate" "net/http" "regexp" ) func main() { truePattern := regexp.MustCompile("bar\\?q=\\d*") falsePattern := regexp.MustCompile("foo\\?q\\d*") req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) fmt.Printf("%v\n", predicate.RequestURIMatches(truePattern).Accept(req)) fmt.Printf("%v\n", predicate.RequestURIMatches(falsePattern).Accept(req)) }
Output: true false
func RequestURIStartsWith ¶
RequestURIStartsWith returns a predicate that returns true if the request URI starts with the path.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/predicate" "net/http" ) func main() { req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) fmt.Printf("%v\n", predicate.RequestURIStartsWith("/test/foo").Accept(req)) fmt.Printf("%v\n", predicate.RequestURIStartsWith("/test/bar").Accept(req)) }
Output: true false
func StringContains ¶
StringContains returns a predicate that returns true if the value passed contains a substring matching 'value'.
Example ¶
fmt.Printf("%v\n", StringContains("oob").Accept("foobar")) fmt.Printf("%v\n", StringContains("snafu").Accept("foobar"))
Output: true false
func StringEndsWith ¶
StringEndsWith returns a predicate that returns true if the value passed ends with a substring matching 'value'.
Example ¶
fmt.Printf("%v\n", StringEndsWith("bar").Accept("foobar")) fmt.Printf("%v\n", StringEndsWith("foo").Accept("foobar"))
Output: true false
func StringEquals ¶
StringEquals returns a predicate that returns true if the value passed is a string and is equal to the value of 'value'
Example ¶
fmt.Printf("%v\n", StringEquals("foobar").Accept("foobar")) fmt.Printf("%v\n", StringEquals("barfoo").Accept("foobar"))
Output: true false
func StringMatches ¶
StringMatches returns a predicate that returns true if the regex matches 'value'.
Example ¶
truePattern := regexp.MustCompile("fo{2}bar") falsePattern := regexp.MustCompile("barfo{2}") fmt.Printf("%v\n", StringMatches(truePattern).Accept("foobar")) fmt.Printf("%v\n", StringMatches(falsePattern).Accept("foobar"))
Output: true false
func StringStartsWith ¶
StringStartsWith returns a predicate that returns true if the value passed starts with a substring matching 'value'.
Example ¶
fmt.Printf("%v\n", StringStartsWith("foo").Accept("foobar")) fmt.Printf("%v\n", StringStartsWith("bar").Accept("foobar"))
Output: true false
type PredicateFunc ¶
type PredicateFunc func(interface{}) bool
PredicateFunc is an implementation of Predicate that is a function and calls itself on a call to Accept
func (PredicateFunc) Accept ¶
func (pf PredicateFunc) Accept(v interface{}) bool
PredicateFunc.Accept calls the predicate func with the passed value.