Skip to content
Permalink
Browse files
[docs] Add example usage for Route.HeadersRegexp (#320)
* Add example usage for Route.HeadersRegexp

* Improve example_route_test.go style
  • Loading branch information
mtso authored and kisielk committed Dec 4, 2017
1 parent c572efe commit 7904d2e42e7ebbdb4a6eb3e57eb201b11df25c57
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
@@ -0,0 +1,51 @@
package mux_test

This comment has been minimized.

Copy link
@roobre

roobre Jan 6, 2018

Contributor

All the other test files belong to the mux package. Why is this one different?

This comment has been minimized.

Copy link
@elithrar

elithrar Jan 6, 2018

Member

This prevents the tests from accessing private (not-exported) identifiers, which is a valid approach for these tests.

If you need to add tests, both approaches are acceptable, depending on needs. I could see the Middleware tests using both.

This comment has been minimized.

Copy link
@roobre

roobre Jan 6, 2018

Contributor

It's not actually relevant imho, but having it on a different package makes it crash if mux code is on a different path than "github.com/gorilla/mux" (which is kind of a go design problem, but whatever). Nvm, just wanted to note it in case it was a typo :)

This comment has been minimized.

Copy link
@elithrar

elithrar via email Jan 6, 2018

Member

This comment has been minimized.

Copy link
@roobre

roobre Jan 6, 2018

Contributor

Also it seems to be the cause of that strange go vet issue.

But again, I don't think it's actually worth changing if there are reasons to keep it this way. Just wanted to note it :P


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")

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:
// 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$")

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:
// Match: true ["https://example.co"]
// Match: false ["https://example.co.uk"]
}
@@ -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

0 comments on commit 7904d2e

Please sign in to comment.