Documentation ¶
Overview ¶
Package extractor contains higher order functions that return functions that can take a value and return some derivative value for it. It also defines a number of convenient extractors for use with http.Request objects. The idea is that the Extractor producing functions can take inputs that affect the derivative function's output. For instance the ExtractHeader(name string) function returns an instance of Extractor that expects a *http.Request to be passed and returns the Header with the name passed to the higher order function. In this way, the extractor can be called repeatedly with different requests. In most cases, Extractors are used with Predicates.
Index ¶
- type Extractor
- func ExtractHeader(name string) Extractor
- func ExtractHost() Extractor
- func ExtractMethod() Extractor
- func ExtractPath() Extractor
- func ExtractPathElementByIndex(idx int) Extractor
- func ExtractQueryParameter(name string) Extractor
- func ExtractRequestURI() Extractor
- func ExtractXPathString(xpath string) Extractor
- func IdentityExtractor() Extractor
- func UpperCaseExtractor(extractor Extractor) Extractor
- type ExtractorFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Extractor ¶
type Extractor interface {
Extract(interface{}) interface{}
}
Extractor can extract a value from another value by calling the Extract method.
func ExtractHeader ¶
ExtractHeader returns an Extractor that expects a *http.Request and returns the value of the header named 'name'.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/extractor" "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("header[FOO] = %s", extractor.ExtractHeader("FOO").Extract(req)) }
Output: header[FOO] = FOOBAR
func ExtractHost ¶
func ExtractHost() Extractor
ExtractHost returns an Extractor that returns the value of the "Host" element in the request.
func ExtractMethod ¶
func ExtractMethod() Extractor
ExtractMethod returns an extractor that expects a *http.Request and returns the method.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/extractor" "net/http" ) func main() { req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) fmt.Printf("method = %s", extractor.ExtractMethod().Extract(req)) }
Output: method = GET
func ExtractPath ¶
func ExtractPath() Extractor
ExtractPath returns an Extractor that expects a *http.Request and returns the URL's Path property.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/extractor" "net/http" ) func main() { req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) fmt.Printf("path = %s", extractor.ExtractPath().Extract(req)) }
Output: path = /test/foo/bar
func ExtractPathElementByIndex ¶
ExtractPathElementByIndex returns an Extractor that expects a *http.Request and extracts the path element at the given position. A negative number denotes a position from the end (starting at 1 e.g. -1 is the last element in the path). For positive inputs, the counting starts at 1 as well.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/extractor" "net/http" ) func main() { req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) for i := 0; i <= 4; i++ { fmt.Printf("PathElementByIndex[%d] = '%s'\n", i, extractor.ExtractPathElementByIndex(i).Extract(req)) } for i := -1; i >= -4; i-- { fmt.Printf("PathElementByIndex[%d] = '%s'\n", i, extractor.ExtractPathElementByIndex(i).Extract(req)) } }
Output: PathElementByIndex[0] = '' PathElementByIndex[1] = 'test' PathElementByIndex[2] = 'foo' PathElementByIndex[3] = 'bar' PathElementByIndex[4] = '' PathElementByIndex[-1] = 'bar' PathElementByIndex[-2] = 'foo' PathElementByIndex[-3] = 'test' PathElementByIndex[-4] = ''
func ExtractQueryParameter ¶
ExtractQueryParameter returns an Extractor that expects a *http.Request and extracts they named query parameter's value.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/extractor" "net/http" ) func main() { req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) fmt.Printf("query[q] = %s\n", extractor.ExtractQueryParameter("q").Extract(req)) fmt.Printf("query[l] = %s\n", extractor.ExtractQueryParameter("l").Extract(req)) fmt.Printf("query[x] = %s\n", extractor.ExtractQueryParameter("x").Extract(req)) }
Output: query[q] = 5 query[l] = 3 query[x] =
func ExtractRequestURI ¶
func ExtractRequestURI() Extractor
ExtractRequestURI returns an Extractor that expects a *http.Request and returns the URL's RequestURI property.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/extractor" "net/http" ) func main() { req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) fmt.Printf("requestURI = %s", extractor.ExtractRequestURI().Extract(req)) }
Output: requestURI = /test/foo/bar?q=5&l=3
func ExtractXPathString ¶
ExtractXPathString returns a Extractor that expects a *http.Request and uses the passed XPath expression to extract a string from the Body of the request Request.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/extractor" "net/http" "strings" ) func main() { const testXml = ` <foo> <bar snafu="foobar"/> </foo> ` req, _ := http.NewRequest("POST", "http://foo.com/test", strings.NewReader(testXml)) fmt.Printf("xpath[/foo/bar/@snafu] = %s", extractor.ExtractXPathString("/foo/bar/@snafu").Extract(req)) }
Output: xpath[/foo/bar/@snafu] = foobar
func IdentityExtractor ¶
func IdentityExtractor() Extractor
IdentityExtractor returns an Extractor that returns the value passed.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/extractor" "net/http" "net/http/httputil" ) func main() { req, _ := http.NewRequest("GET", "http://foo.com/test/foo/bar?q=5&l=3", nil) dump, _ := httputil.DumpRequest(extractor.IdentityExtractor().Extract(req).(*http.Request), false) fmt.Printf("request = %q", dump) }
Output: request = "GET /test/foo/bar?q=5&l=3 HTTP/1.1\r\nHost: foo.com\r\n\r\n"
func UpperCaseExtractor ¶
UpperCaseExtractor returns an Extractor that decorates the passed extractor by applying strings.ToUpper to the value returned.
Example ¶
package main import ( "fmt" "github.com/bluesoftdev/go-http-matchers/extractor" ) func main() { fmt.Printf("upperCase[FooBar] = %s", extractor.UpperCaseExtractor(extractor.IdentityExtractor()).Extract("FooBar")) }
Output: upperCase[FooBar] = FOOBAR
type ExtractorFunc ¶
type ExtractorFunc func(interface{}) interface{}
ExtractorFunc is a function that calls itself when it's Extract method is called.
func (ExtractorFunc) Extract ¶
func (ef ExtractorFunc) Extract(v interface{}) interface{}