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.
()
| 33 | // for the header value. Using the start and end of string anchors, the |
| 34 | // value must be an exact match. |
| 35 | func 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 | } |
nothing calls this directly
no test coverage detected