Browse Source

Add tests for support for queries in URL reversing.

pull/251/merge
Pavel Ivanov 9 years ago committed by Kamil Kisiel
parent
commit
f9aa23a02b
  1. 25
      mux_test.go

25
mux_test.go

@ -36,6 +36,7 @@ type routeTest struct {
scheme string // the expected scheme of the built URL scheme string // the expected scheme of the built URL
host string // the expected host of the built URL host string // the expected host of the built URL
path string // the expected path of the built URL path string // the expected path of the built URL
query string // the expected query string to match
pathTemplate string // the expected path template of the route pathTemplate string // the expected path template of the route
hostTemplate string // the expected host template of the route hostTemplate string // the expected host template of the route
methods []string // the expected route methods methods []string // the expected route methods
@ -744,6 +745,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{}, vars: map[string]string{},
host: "", host: "",
path: "", path: "",
query: "foo=bar&baz=ding",
shouldMatch: true, shouldMatch: true,
}, },
{ {
@ -753,6 +755,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{}, vars: map[string]string{},
host: "", host: "",
path: "", path: "",
query: "foo=bar&baz=ding",
pathTemplate: `/api`, pathTemplate: `/api`,
hostTemplate: `www.example.com`, hostTemplate: `www.example.com`,
shouldMatch: true, shouldMatch: true,
@ -764,6 +767,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{}, vars: map[string]string{},
host: "", host: "",
path: "", path: "",
query: "foo=bar&baz=ding",
pathTemplate: `/api`, pathTemplate: `/api`,
hostTemplate: `www.example.com`, hostTemplate: `www.example.com`,
shouldMatch: true, shouldMatch: true,
@ -784,6 +788,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v1": "bar"}, vars: map[string]string{"v1": "bar"},
host: "", host: "",
path: "", path: "",
query: "foo=bar",
shouldMatch: true, shouldMatch: true,
}, },
{ {
@ -793,6 +798,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v1": "bar", "v2": "ding"}, vars: map[string]string{"v1": "bar", "v2": "ding"},
host: "", host: "",
path: "", path: "",
query: "foo=bar&baz=ding",
shouldMatch: true, shouldMatch: true,
}, },
{ {
@ -802,6 +808,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v1": "10"}, vars: map[string]string{"v1": "10"},
host: "", host: "",
path: "", path: "",
query: "foo=10",
shouldMatch: true, shouldMatch: true,
}, },
{ {
@ -820,6 +827,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v1": "1"}, vars: map[string]string{"v1": "1"},
host: "", host: "",
path: "", path: "",
query: "foo=1",
shouldMatch: true, shouldMatch: true,
}, },
{ {
@ -829,6 +837,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v1": "1"}, vars: map[string]string{"v1": "1"},
host: "", host: "",
path: "", path: "",
query: "foo=1",
shouldMatch: true, shouldMatch: true,
}, },
{ {
@ -847,6 +856,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v1": "1a"}, vars: map[string]string{"v1": "1a"},
host: "", host: "",
path: "", path: "",
query: "foo=1a",
shouldMatch: true, shouldMatch: true,
}, },
{ {
@ -865,6 +875,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v-1": "bar"}, vars: map[string]string{"v-1": "bar"},
host: "", host: "",
path: "", path: "",
query: "foo=bar",
shouldMatch: true, shouldMatch: true,
}, },
{ {
@ -874,6 +885,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v-1": "bar", "v-2": "ding"}, vars: map[string]string{"v-1": "bar", "v-2": "ding"},
host: "", host: "",
path: "", path: "",
query: "foo=bar&baz=ding",
shouldMatch: true, shouldMatch: true,
}, },
{ {
@ -883,6 +895,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v-1": "10"}, vars: map[string]string{"v-1": "10"},
host: "", host: "",
path: "", path: "",
query: "foo=10",
shouldMatch: true, shouldMatch: true,
}, },
{ {
@ -892,6 +905,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v-1": "1a"}, vars: map[string]string{"v-1": "1a"},
host: "", host: "",
path: "", path: "",
query: "foo=1a",
shouldMatch: true, shouldMatch: true,
}, },
{ {
@ -901,6 +915,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{}, vars: map[string]string{},
host: "", host: "",
path: "", path: "",
query: "foo=",
shouldMatch: true, shouldMatch: true,
}, },
{ {
@ -919,6 +934,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{}, vars: map[string]string{},
host: "", host: "",
path: "", path: "",
query: "foo=",
shouldMatch: true, shouldMatch: true,
}, },
{ {
@ -946,6 +962,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"foo": ""}, vars: map[string]string{"foo": ""},
host: "", host: "",
path: "", path: "",
query: "foo=",
shouldMatch: true, shouldMatch: true,
}, },
{ {
@ -1537,6 +1554,7 @@ func testRoute(t *testing.T, test routeTest) {
route := test.route route := test.route
vars := test.vars vars := test.vars
shouldMatch := test.shouldMatch shouldMatch := test.shouldMatch
query := test.query
shouldRedirect := test.shouldRedirect shouldRedirect := test.shouldRedirect
uri := url.URL{ uri := url.URL{
Scheme: test.scheme, Scheme: test.scheme,
@ -1606,6 +1624,13 @@ func testRoute(t *testing.T, test routeTest) {
return return
} }
} }
if query != "" {
u, _ := route.URL(mapToPairs(match.Vars)...)
if query != u.RawQuery {
t.Errorf("(%v) URL query not equal: expected %v, got %v", test.title, query, u.RawQuery)
return
}
}
if shouldRedirect && match.Handler == nil { if shouldRedirect && match.Handler == nil {
t.Errorf("(%v) Did not redirect", test.title) t.Errorf("(%v) Did not redirect", test.title)
return return

Loading…
Cancel
Save