----------------------------------------------------------------------------
(t *testing.T)
| 65 | // ---------------------------------------------------------------------------- |
| 66 | |
| 67 | func TestRouteMatchers(t *testing.T) { |
| 68 | var scheme, host, path, query, method string |
| 69 | var headers map[string]string |
| 70 | var resultVars map[bool]map[string]string |
| 71 | |
| 72 | router := NewRouter() |
| 73 | router.NewRoute().Host("{var1}.google.com"). |
| 74 | Path("/{var2:[a-z]+}/{var3:[0-9]+}"). |
| 75 | Queries("foo", "bar"). |
| 76 | Methods("GET"). |
| 77 | Schemes("https"). |
| 78 | Headers("x-requested-with", "XMLHttpRequest") |
| 79 | router.NewRoute().Host("www.{var4}.com"). |
| 80 | PathPrefix("/foo/{var5:[a-z]+}/{var6:[0-9]+}"). |
| 81 | Queries("baz", "ding"). |
| 82 | Methods("POST"). |
| 83 | Schemes("http"). |
| 84 | Headers("Content-Type", "application/json") |
| 85 | |
| 86 | reset := func() { |
| 87 | // Everything match. |
| 88 | scheme = "https" |
| 89 | host = "www.google.com" |
| 90 | path = "/product/42" |
| 91 | query = "?foo=bar" |
| 92 | method = "GET" |
| 93 | headers = map[string]string{"X-Requested-With": "XMLHttpRequest"} |
| 94 | resultVars = map[bool]map[string]string{ |
| 95 | true: {"var1": "www", "var2": "product", "var3": "42"}, |
| 96 | false: {}, |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | reset2 := func() { |
| 101 | // Everything match. |
| 102 | scheme = "http" |
| 103 | host = "www.google.com" |
| 104 | path = "/foo/product/42/path/that/is/ignored" |
| 105 | query = "?baz=ding" |
| 106 | method = "POST" |
| 107 | headers = map[string]string{"Content-Type": "application/json"} |
| 108 | resultVars = map[bool]map[string]string{ |
| 109 | true: {"var4": "google", "var5": "product", "var6": "42"}, |
| 110 | false: {}, |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | match := func(shouldMatch bool) { |
| 115 | url := scheme + "://" + host + path + query |
| 116 | request, _ := http.NewRequest(method, url, nil) |
| 117 | for key, value := range headers { |
| 118 | request.Header.Add(key, value) |
| 119 | } |
| 120 | |
| 121 | var routeMatch RouteMatch |
| 122 | matched := router.Match(request, &routeMatch) |
| 123 | if matched != shouldMatch { |
| 124 | t.Errorf("Expected: %v\nGot: %v\nRequest: %v %v", shouldMatch, matched, request.Method, url) |