-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[docs] Add example usage for Route.HeadersRegexp #320
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to just above the |
||
|
||
req1.Header.Add("Accept", "text/plain") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move next to the declaration of |
||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar changes to this function. |
||
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"] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to just before the
fmt.Printf
, leave a space here to separate ther
androute
from the rest.