aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/gorilla/mux/example_route_test.go
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net> 2018-04-27 20:38:46 -0700
committerGravatar Frédéric Guillot <fred@miniflux.net> 2018-04-27 20:38:46 -0700
commit6b360d08c1f6c8a6cd1b7608f7af734a3ceef8d7 (patch)
tree48352d35fa9f3559df05accf4ce4fce1672a2830 /vendor/github.com/gorilla/mux/example_route_test.go
parent322b265d7aec7731f7fa703c9a74ceb61ae73f3f (diff)
downloadv2-6b360d08c1f6c8a6cd1b7608f7af734a3ceef8d7.tar.gz
v2-6b360d08c1f6c8a6cd1b7608f7af734a3ceef8d7.tar.zst
v2-6b360d08c1f6c8a6cd1b7608f7af734a3ceef8d7.zip
Use Gorilla middleware (refactoring)
Diffstat (limited to 'vendor/github.com/gorilla/mux/example_route_test.go')
-rw-r--r--vendor/github.com/gorilla/mux/example_route_test.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/vendor/github.com/gorilla/mux/example_route_test.go b/vendor/github.com/gorilla/mux/example_route_test.go
new file mode 100644
index 00000000..11255707
--- /dev/null
+++ b/vendor/github.com/gorilla/mux/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")
+
+ 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"]
+}