MCPcopy
hub / github.com/gorilla/mux / ExampleRoute_HeadersRegexp_exactMatch

Function ExampleRoute_HeadersRegexp_exactMatch

example_route_test.go:35–51  ·  example_route_test.go::ExampleRoute_HeadersRegexp_exactMatch

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.

()

Source from the content-addressed store, hash-verified

33// for the header value. Using the start and end of string anchors, the
34// value must be an exact match.
35func ExampleRoute_HeadersRegexp_exactMatch() {
36 r := mux.NewRouter()
37 route := r.NewRoute().HeadersRegexp("Origin", "^https://example.co$")
38
39 yes, _ := http.NewRequest("GET", "example.co", nil)
40 yes.Header.Set("Origin", "https://example.co")
41
42 no, _ := http.NewRequest("GET", "example.co.uk", nil)
43 no.Header.Set("Origin", "https://example.co.uk")
44
45 matchInfo := &mux.RouteMatch{}
46 fmt.Printf("Match: %v %q\n", route.Match(yes, matchInfo), yes.Header["Origin"])
47 fmt.Printf("Match: %v %q\n", route.Match(no, matchInfo), no.Header["Origin"])
48 // Output:
49 // Match: true ["https://example.co"]
50 // Match: false ["https://example.co.uk"]
51}

Callers

nothing calls this directly

Calls 4

NewRouteMethod · 0.95
NewRouterFunction · 0.92
HeadersRegexpMethod · 0.80
MatchMethod · 0.65

Tested by

no test coverage detected