From e1643a3f6a955f551f7ff817d3a96e573708f86b Mon Sep 17 00:00:00 2001 From: mtso Date: Fri, 1 Dec 2017 14:30:16 -0800 Subject: [PATCH 1/2] Add example usage for Route.HeadersRegexp --- example_route_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++ route.go | 3 ++- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 example_route_test.go diff --git a/example_route_test.go b/example_route_test.go new file mode 100644 index 00000000..7e091317 --- /dev/null +++ b/example_route_test.go @@ -0,0 +1,51 @@ +package mux_test + +import ( + "fmt" + "net/http" + + "github.com/gorilla/mux" +) + +// This example demonstrates setting a regular expression matcher for +// the header value. A plain word will match any value that contains a +// matching substring as if the pattern was wrapped with `.*`. +func ExampleRoute_HeadersRegexp() { + r := mux.NewRouter() + route := r.NewRoute().HeadersRegexp("Accept", "html") + matchInfo := &mux.RouteMatch{} + req1, _ := http.NewRequest("GET", "example.com", nil) + req2, _ := http.NewRequest("GET", "example.com", nil) + + req1.Header.Add("Accept", "text/plain") + req1.Header.Add("Accept", "text/html") + + req2.Header.Set("Accept", "application/xhtml+xml") + + fmt.Printf("Match: %v %q\n", route.Match(req1, matchInfo), req1.Header["Accept"]) + fmt.Printf("Match: %v %q\n", route.Match(req2, matchInfo), req2.Header["Accept"]) + // Output: + // Match: true ["text/plain" "text/html"] + // Match: true ["application/xhtml+xml"] +} + +// This example demonstrates setting a strict regular expression matcher +// for the header value. Using the start and end of string anchors, the +// value must be an exact match. +func ExampleRoute_HeadersRegexp_exactMatch() { + r := mux.NewRouter() + route := r.NewRoute().HeadersRegexp("Origin", "^https://example.co$") + matchInfo := &mux.RouteMatch{} + yes, _ := http.NewRequest("GET", "example.co", nil) + no, _ := http.NewRequest("GET", "example.co.uk", nil) + + yes.Header.Set("Origin", "https://example.co") + + no.Header.Set("Origin", "https://example.co.uk") + + fmt.Printf("Match: %v %q\n", route.Match(yes, matchInfo), yes.Header["Origin"]) + fmt.Printf("Match: %v %q\n", route.Match(no, matchInfo), no.Header["Origin"]) + // Output: + // Match: true ["https://example.co"] + // Match: false ["https://example.co.uk"] +} diff --git a/route.go b/route.go index 2b3de8bf..d0a71a92 100644 --- a/route.go +++ b/route.go @@ -258,7 +258,8 @@ func (m headerRegexMatcher) Match(r *http.Request, match *RouteMatch) bool { // "X-Requested-With", "XMLHttpRequest") // // The above route will only match if both the request header matches both regular expressions. -// It the value is an empty string, it will match any value if the key is set. +// If the value is an empty string, it will match any value if the key is set. +// Use the start and end of string anchors (^ and $) to match an exact value. func (r *Route) HeadersRegexp(pairs ...string) *Route { if r.err == nil { var headers map[string]*regexp.Regexp From bdb86053dd3bd997d8cb7da1388d0f70fde48611 Mon Sep 17 00:00:00 2001 From: mtso Date: Mon, 4 Dec 2017 08:07:06 -0800 Subject: [PATCH 2/2] Improve example_route_test.go style --- example_route_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/example_route_test.go b/example_route_test.go index 7e091317..11255707 100644 --- a/example_route_test.go +++ b/example_route_test.go @@ -13,15 +13,15 @@ import ( func ExampleRoute_HeadersRegexp() { r := mux.NewRouter() route := r.NewRoute().HeadersRegexp("Accept", "html") - matchInfo := &mux.RouteMatch{} - req1, _ := http.NewRequest("GET", "example.com", nil) - req2, _ := http.NewRequest("GET", "example.com", nil) + req1, _ := http.NewRequest("GET", "example.com", nil) req1.Header.Add("Accept", "text/plain") req1.Header.Add("Accept", "text/html") + req2, _ := http.NewRequest("GET", "example.com", nil) req2.Header.Set("Accept", "application/xhtml+xml") + matchInfo := &mux.RouteMatch{} fmt.Printf("Match: %v %q\n", route.Match(req1, matchInfo), req1.Header["Accept"]) fmt.Printf("Match: %v %q\n", route.Match(req2, matchInfo), req2.Header["Accept"]) // Output: @@ -35,14 +35,14 @@ func ExampleRoute_HeadersRegexp() { func ExampleRoute_HeadersRegexp_exactMatch() { r := mux.NewRouter() route := r.NewRoute().HeadersRegexp("Origin", "^https://example.co$") - matchInfo := &mux.RouteMatch{} - yes, _ := http.NewRequest("GET", "example.co", nil) - no, _ := http.NewRequest("GET", "example.co.uk", nil) + yes, _ := http.NewRequest("GET", "example.co", nil) yes.Header.Set("Origin", "https://example.co") + no, _ := http.NewRequest("GET", "example.co.uk", nil) no.Header.Set("Origin", "https://example.co.uk") + matchInfo := &mux.RouteMatch{} fmt.Printf("Match: %v %q\n", route.Match(yes, matchInfo), yes.Header["Origin"]) fmt.Printf("Match: %v %q\n", route.Match(no, matchInfo), no.Header["Origin"]) // Output: