Created
October 5, 2012 05:01
-
-
Save kisielk/3838189 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| kyoto:mux kamil$ cat mux_test.diff | |
| diff --git a/mux_test.go b/mux_test.go | |
| index 19e0daf..158ce54 100644 | |
| --- a/mux_test.go | |
| +++ b/mux_test.go | |
| @@ -23,150 +23,126 @@ func TestRoute(t *testing.T) { | |
| return idValue | |
| } | |
| - // Host ------------------------------------------------------------------- | |
| - | |
| - route = new(Route).Host("aaa.bbb.ccc") | |
| - request, _ = http.NewRequest("GET", "http://aaa.bbb.ccc/111/222/333", nil) | |
| - vars = map[string]string{} | |
| - host = "aaa.bbb.ccc" | |
| - path = "" | |
| - url = host + path | |
| - testRoute(t, id(), true, route, request, vars, host, path, url) | |
| - // Non-match for the same config. | |
| - request, _ = http.NewRequest("GET", "http://aaa.222.ccc/111/222/333", nil) | |
| - testRoute(t, id(), false, route, request, vars, host, path, url) | |
| - | |
| - route = new(Route).Host("aaa.{v1:[a-z]{3}}.ccc") | |
| - request, _ = http.NewRequest("GET", "http://aaa.bbb.ccc/111/222/333", nil) | |
| - vars = map[string]string{"v1": "bbb"} | |
| - host = "aaa.bbb.ccc" | |
| - path = "" | |
| - url = host + path | |
| - testRoute(t, id(), true, route, request, vars, host, path, url) | |
| - // Non-match for the same config. | |
| - request, _ = http.NewRequest("GET", "http://aaa.222.ccc/111/222/333", nil) | |
| - testRoute(t, id(), false, route, request, vars, host, path, url) | |
| - | |
| - route = new(Route).Host("{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}") | |
| - request, _ = http.NewRequest("GET", "http://aaa.bbb.ccc/111/222/333", nil) | |
| - vars = map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"} | |
| - host = "aaa.bbb.ccc" | |
| - path = "" | |
| - url = host + path | |
| - testRoute(t, id(), true, route, request, vars, host, path, url) | |
| - // Non-match for the same config. | |
| - request, _ = http.NewRequest("GET", "http://aaa.222.ccc/111/222/333", nil) | |
| - testRoute(t, id(), false, route, request, vars, host, path, url) | |
| + newRequest := func(method, url string) *http.Request { | |
| + req, err := http.NewRequest(method, url, nil) | |
| + if err != nil { | |
| + panic(err) | |
| + } | |
| + return req | |
| + } | |
| - // Path ------------------------------------------------------------------- | |
| + newRequestHost := func(method, url, host string) *http.Request { | |
| + req, err := http.NewRequest(method, url, nil) | |
| + if err != nil { | |
| + panic(err) | |
| + } | |
| + req.Host = host | |
| + return req | |
| + } | |
| - route = new(Route).Path("/111/222/333") | |
| - request, _ = http.NewRequest("GET", "http://localhost/111/222/333", nil) | |
| - vars = map[string]string{} | |
| - host = "" | |
| - path = "/111/222/333" | |
| - url = host + path | |
| - testRoute(t, id(), true, route, request, vars, host, path, url) | |
| - // Non-match for the same config. | |
| - request, _ = http.NewRequest("GET", "http://localhost/1/2/3", nil) | |
| - testRoute(t, id(), false, route, request, vars, host, path, url) | |
| + type routeTest struct { | |
| + route *Route | |
| + request *http.Request | |
| + vars map[string]string | |
| + host string | |
| + path string | |
| + match bool | |
| + } | |
| - route = new(Route).Path("/111/{v1:[0-9]{3}}/333") | |
| - request, _ = http.NewRequest("GET", "http://localhost/111/222/333", nil) | |
| - vars = map[string]string{"v1": "222"} | |
| - host = "" | |
| - path = "/111/222/333" | |
| - url = host + path | |
| - testRoute(t, id(), true, route, request, vars, host, path, url) | |
| - // Non-match for the same config. | |
| - request, _ = http.NewRequest("GET", "http://localhost/111/aaa/333", nil) | |
| - testRoute(t, id(), false, route, request, vars, host, path, url) | |
| + tests := []routeTest{ | |
| + // Host | |
| + {new(Route).Host("aaa.bbb.ccc"), newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), map[string]string{}, "aaa.bbb.ccc", "", true}, | |
| + {new(Route).Host("aaa.bbb.ccc"), newRequest("GET", "http://aaa.222.ccc/111/222/333"), map[string]string{}, "aaa.bbb.ccc", "", false}, | |
| + {new(Route).Host("aaa.bbb.ccc:1234"), newRequest("GET", "http://aaa.bbb.ccc:1234/111/222/333"), map[string]string{}, "aaa.bbb.ccc:1234", "", true}, | |
| + {new(Route).Host("aaa.bbb.ccc:1234"), newRequest("GET", "http://aaa.bbb.ccc:9999/111/222/333"), map[string]string{}, "aaa.bbb.ccc:1234", "", false}, | |
| + {new(Route).Host("aaa.bbb.ccc"), newRequestHost("GET", "/111/222/333", "aaa.bbb.ccc"), map[string]string{}, "aaa.bbb.ccc", "", true}, | |
| + {new(Route).Host("aaa.bbb.ccc"), newRequestHost("GET", "/111/222/333", "aaa.222.ccc"), map[string]string{}, "aaa.bbb.ccc", "", false}, | |
| + // BUG {new(Route).Host("aaa.bbb.ccc:1234"), newRequestHost("GET", "/111/222/333", "aaa.bbb.ccc:1234"), map[string]string{}, "aaa.bbb.ccc:1234", "", true}, | |
| + {new(Route).Host("aaa.bbb.ccc:1234"), newRequestHost("GET", "/111/222/333", "aaa.bbb.ccc:9999"), map[string]string{}, "aaa.bbb.ccc:1234", "", false}, | |
| + {new(Route).Host("aaa.{v1:[a-z]{3}}.ccc"), newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), map[string]string{"v1": "bbb"}, "aaa.bbb.ccc", "", true}, | |
| + {new(Route).Host("aaa.{v1:[a-z]{3}}.ccc"), newRequest("GET", "http://aaa.222.ccc/111/222/333"), map[string]string{"v1": "bbb"}, "aaa.bbb.ccc", "", false}, | |
| + {new(Route).Host("{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}"), newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"}, "aaa.bbb.ccc", "", true}, | |
| + {new(Route).Host("{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}"), newRequest("GET", "http://aaa.222.ccc/111/222/333"), map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc"}, "aaa.bbb.ccc", "", false}, | |
| + | |
| + // Path | |
| + {new(Route).Path("/111/222/333"), newRequest("GET", "http://localhost/111/222/333"), map[string]string{}, "", "/111/222/333", true}, | |
| + {new(Route).Path("/111/222/333"), newRequest("GET", "http://localhost/1/2/3"), map[string]string{}, "", "/111/222/333", false}, | |
| + {new(Route).Path("/111/{v1:[0-9]{3}}/333"), newRequest("GET", "http://localhost/111/222/333"), map[string]string{"v1": "222"}, "", "/111/222/333", true}, | |
| + {new(Route).Path("/111/{v1:[0-9]{3}}/333"), newRequest("GET", "http://localhost/111/aaa/333"), map[string]string{"v1": "222"}, "", "/111/222/333", false}, | |
| + {new(Route).Path("/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}"), newRequest("GET", "http://localhost/111/222/333"), map[string]string{"v1": "111", "v2": "222", "v3": "333"}, "", "/111/222/333", true}, | |
| + {new(Route).Path("/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}"), newRequest("GET", "http://localhost/111/aaa/333"), map[string]string{"v1": "111", "v2": "222", "v3": "333"}, "", "/111/222/333", false}, | |
| + | |
| + // PathPrefix | |
| + {new(Route).PathPrefix("/111"), newRequest("GET", "http://localhost/111/222/333"), map[string]string{}, "", "/111", true}, | |
| + {new(Route).PathPrefix("/111"), newRequest("GET", "http://localhost/1/2/3"), map[string]string{}, "", "/111", false}, | |
| + {new(Route).PathPrefix("/111/{v1:[0-9]{3}}"), newRequest("GET", "http://localhost/111/222/333"), map[string]string{"v1": "222"}, "", "/111/222", true}, | |
| + {new(Route).PathPrefix("/111/{v1:[0-9]{3}}"), newRequest("GET", "http://localhost/111/aaa/333"), map[string]string{"v1": "222"}, "", "/111/222", false}, | |
| + {new(Route).PathPrefix("/{v1:[0-9]{3}}/{v2:[0-9]{3}}"), newRequest("GET", "http://localhost/111/222/333"), map[string]string{"v1": "111", "v2": "222"}, "", "/111/222", true}, | |
| + {new(Route).PathPrefix("/{v1:[0-9]{3}}/{v2:[0-9]{3}}"), newRequest("GET", "http://localhost/111/aaa/333"), map[string]string{"v1": "111", "v2": "222"}, "", "/111/222", true}, | |
| + | |
| + // Host + Path | |
| + { | |
| + route: new(Route).Host("aaa.bbb.ccc").Path("/111/222/333"), | |
| + request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), | |
| + vars: map[string]string{}, | |
| + host: "", | |
| + path: "", | |
| + match: true | |
| + }, | |
| + { | |
| + route: new(Route).Host("aaa.bbb.ccc").Path("/111/222/333"), | |
| + request: newRequest("GET", "http://aaa.222.ccc/111/222/333"), | |
| + vars: map[string]string{}, | |
| + host: "", | |
| + path: "", | |
| + match: false, | |
| + }, | |
| + { | |
| + route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc").Path("/111/{v2:[0-9]{3}}/333"), | |
| + request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), | |
| + vars: map[string]string{"v1": "bbb", "v2": "222"}, | |
| + host: "aaa.bbb.ccc", | |
| + path: "/111/222/333", | |
| + match: true | |
| + }, | |
| + { | |
| + route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc").Path("/111/{v2:[0-9]{3}}/333"), | |
| + request: newRequest("GET", "http://aaa.222.ccc/111/222/333"), | |
| + vars: map[string]string{"v1": "bbb", "v2": "222"}, | |
| + host: "aaa.bbb.ccc", | |
| + path: "/111/222/333", | |
| + match: false, | |
| + }, | |
| + { | |
| + route: new(Route).Host("{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}").Path("/{v4:[0-9]{3}}/{v5:[0-9]{3}}/{v6:[0-9]{3}}"), | |
| + request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), | |
| + vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc", "v4": "111", "v5": "222", "v6": "333"}, | |
| + host: "aaa.bbb.ccc", | |
| + path: "/111/222/333", | |
| + match: true, | |
| + }, | |
| + { | |
| + route: new(Route).Host("{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}").Path("/{v4:[0-9]{3}}/{v5:[0-9]{3}}/{v6:[0-9]{3}}"), | |
| + request: newRequest("GET", "http://aaa.222.ccc/111/222/333"), | |
| + vars: map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc", "v4": "111", "v5": "222", "v6": "333"}, | |
| + host: "aaa.bbb.ccc", | |
| + path: "/111/222/333", | |
| + match: false, | |
| + }, | |
| + } | |
| - route = new(Route).Path("/{v1:[0-9]{3}}/{v2:[0-9]{3}}/{v3:[0-9]{3}}") | |
| - request, _ = http.NewRequest("GET", "http://localhost/111/222/333", nil) | |
| - vars = map[string]string{"v1": "111", "v2": "222", "v3": "333"} | |
| - host = "" | |
| - path = "/111/222/333" | |
| - url = host + path | |
| - testRoute(t, id(), true, route, request, vars, host, path, url) | |
| - // Non-match for the same config. | |
| - request, _ = http.NewRequest("GET", "http://localhost/111/aaa/333", nil) | |
| - testRoute(t, id(), false, route, request, vars, host, path, url) | |
| + for i, test := range tests { | |
| + testRoute(t, i, test.match, test.route, test.request, test.vars, test.host, test.path, test.host+test.path) | |
| + } | |
| + // Host ------------------------------------------------------------------- | |
| + // Path ------------------------------------------------------------------- | |
| // PathPrefix ------------------------------------------------------------- | |
| - | |
| - route = new(Route).PathPrefix("/111") | |
| - request, _ = http.NewRequest("GET", "http://localhost/111/222/333", nil) | |
| - vars = map[string]string{} | |
| - host = "" | |
| - path = "/111" | |
| - url = host + path | |
| - testRoute(t, id(), true, route, request, vars, host, path, url) | |
| - // Non-match for the same config. | |
| - request, _ = http.NewRequest("GET", "http://localhost/1/2/3", nil) | |
| - testRoute(t, id(), false, route, request, vars, host, path, url) | |
| - | |
| - route = new(Route).PathPrefix("/111/{v1:[0-9]{3}}") | |
| - request, _ = http.NewRequest("GET", "http://localhost/111/222/333", nil) | |
| - vars = map[string]string{"v1": "222"} | |
| - host = "" | |
| - path = "/111/222" | |
| - url = host + path | |
| - testRoute(t, id(), true, route, request, vars, host, path, url) | |
| - // Non-match for the same config. | |
| - request, _ = http.NewRequest("GET", "http://localhost/111/aaa/333", nil) | |
| - testRoute(t, id(), false, route, request, vars, host, path, url) | |
| - | |
| - route = new(Route).PathPrefix("/{v1:[0-9]{3}}/{v2:[0-9]{3}}") | |
| - request, _ = http.NewRequest("GET", "http://localhost/111/222/333", nil) | |
| - vars = map[string]string{"v1": "111", "v2": "222"} | |
| - host = "" | |
| - path = "/111/222" | |
| - url = host + path | |
| - testRoute(t, id(), true, route, request, vars, host, path, url) | |
| - // Non-match for the same config. | |
| - request, _ = http.NewRequest("GET", "http://localhost/111/aaa/333", nil) | |
| - testRoute(t, id(), false, route, request, vars, host, path, url) | |
| - | |
| // Host + Path ------------------------------------------------------------ | |
| - | |
| - route = new(Route).Host("aaa.bbb.ccc").Path("/111/222/333") | |
| - request, _ = http.NewRequest("GET", "http://aaa.bbb.ccc/111/222/333", nil) | |
| - vars = map[string]string{} | |
| - host = "" | |
| - path = "" | |
| - url = host + path | |
| - testRoute(t, id(), true, route, request, vars, host, path, url) | |
| - // Non-match for the same config. | |
| - request, _ = http.NewRequest("GET", "http://aaa.222.ccc/111/222/333", nil) | |
| - testRoute(t, id(), false, route, request, vars, host, path, url) | |
| - | |
| - route = new(Route).Host("aaa.{v1:[a-z]{3}}.ccc").Path("/111/{v2:[0-9]{3}}/333") | |
| - request, _ = http.NewRequest("GET", "http://aaa.bbb.ccc/111/222/333", nil) | |
| - vars = map[string]string{"v1": "bbb", "v2": "222"} | |
| - host = "aaa.bbb.ccc" | |
| - path = "/111/222/333" | |
| - url = host + path | |
| - testRoute(t, id(), true, route, request, vars, host, path, url) | |
| - // Non-match for the same config. | |
| - request, _ = http.NewRequest("GET", "http://aaa.222.ccc/111/222/333", nil) | |
| - testRoute(t, id(), false, route, request, vars, host, path, url) | |
| - | |
| - route = new(Route).Host("{v1:[a-z]{3}}.{v2:[a-z]{3}}.{v3:[a-z]{3}}").Path("/{v4:[0-9]{3}}/{v5:[0-9]{3}}/{v6:[0-9]{3}}") | |
| - request, _ = http.NewRequest("GET", "http://aaa.bbb.ccc/111/222/333", nil) | |
| - vars = map[string]string{"v1": "aaa", "v2": "bbb", "v3": "ccc", "v4": "111", "v5": "222", "v6": "333"} | |
| - host = "aaa.bbb.ccc" | |
| - path = "/111/222/333" | |
| - url = host + path | |
| - testRoute(t, id(), true, route, request, vars, host, path, url) | |
| - // Non-match for the same config. | |
| - request, _ = http.NewRequest("GET", "http://aaa.222.ccc/111/222/333", nil) | |
| - testRoute(t, id(), false, route, request, vars | |
| .... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment