summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gorilla/mux/mux_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/gorilla/mux/mux_test.go')
-rw-r--r--vendor/github.com/gorilla/mux/mux_test.go145
1 files changed, 143 insertions, 2 deletions
diff --git a/vendor/github.com/gorilla/mux/mux_test.go b/vendor/github.com/gorilla/mux/mux_test.go
index 19ef5a8cc..484fab431 100644
--- a/vendor/github.com/gorilla/mux/mux_test.go
+++ b/vendor/github.com/gorilla/mux/mux_test.go
@@ -11,6 +11,7 @@ import (
"fmt"
"net/http"
"net/url"
+ "reflect"
"strings"
"testing"
)
@@ -35,6 +36,7 @@ type routeTest struct {
scheme string // the expected scheme of the built URL
host string // the expected host of the built URL
path string // the expected path of the built URL
+ query string // the expected query string of the built URL
pathTemplate string // the expected path template of the route
hostTemplate string // the expected host template of the route
methods []string // the expected route methods
@@ -743,6 +745,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{},
host: "",
path: "",
+ query: "foo=bar&baz=ding",
shouldMatch: true,
},
{
@@ -752,6 +755,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{},
host: "",
path: "",
+ query: "foo=bar&baz=ding",
pathTemplate: `/api`,
hostTemplate: `www.example.com`,
shouldMatch: true,
@@ -763,6 +767,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{},
host: "",
path: "",
+ query: "foo=bar&baz=ding",
pathTemplate: `/api`,
hostTemplate: `www.example.com`,
shouldMatch: true,
@@ -783,6 +788,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v1": "bar"},
host: "",
path: "",
+ query: "foo=bar",
shouldMatch: true,
},
{
@@ -792,6 +798,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v1": "bar", "v2": "ding"},
host: "",
path: "",
+ query: "foo=bar&baz=ding",
shouldMatch: true,
},
{
@@ -801,6 +808,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v1": "10"},
host: "",
path: "",
+ query: "foo=10",
shouldMatch: true,
},
{
@@ -819,6 +827,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v1": "1"},
host: "",
path: "",
+ query: "foo=1",
shouldMatch: true,
},
{
@@ -828,6 +837,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v1": "1"},
host: "",
path: "",
+ query: "foo=1",
shouldMatch: true,
},
{
@@ -846,6 +856,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v1": "1a"},
host: "",
path: "",
+ query: "foo=1a",
shouldMatch: true,
},
{
@@ -864,6 +875,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v-1": "bar"},
host: "",
path: "",
+ query: "foo=bar",
shouldMatch: true,
},
{
@@ -873,6 +885,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v-1": "bar", "v-2": "ding"},
host: "",
path: "",
+ query: "foo=bar&baz=ding",
shouldMatch: true,
},
{
@@ -882,6 +895,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v-1": "10"},
host: "",
path: "",
+ query: "foo=10",
shouldMatch: true,
},
{
@@ -891,6 +905,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"v-1": "1a"},
host: "",
path: "",
+ query: "foo=1a",
shouldMatch: true,
},
{
@@ -900,6 +915,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{},
host: "",
path: "",
+ query: "foo=",
shouldMatch: true,
},
{
@@ -918,6 +934,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{},
host: "",
path: "",
+ query: "foo=",
shouldMatch: true,
},
{
@@ -945,6 +962,7 @@ func TestQueries(t *testing.T) {
vars: map[string]string{"foo": ""},
host: "",
path: "",
+ query: "foo=",
shouldMatch: true,
},
{
@@ -956,6 +974,16 @@ func TestQueries(t *testing.T) {
path: "",
shouldMatch: false,
},
+ {
+ title: "Queries route with pattern, match, escaped value",
+ route: new(Route).Queries("foo", "{v1}"),
+ request: newRequest("GET", "http://localhost?foo=%25bar%26%20%2F%3D%3F"),
+ vars: map[string]string{"v1": "%bar& /=?"},
+ host: "",
+ path: "",
+ query: "foo=%25bar%26+%2F%3D%3F",
+ shouldMatch: true,
+ },
}
for _, test := range tests {
@@ -1187,6 +1215,28 @@ func TestSubRouter(t *testing.T) {
pathTemplate: `/{category}`,
shouldMatch: true,
},
+ {
+ title: "Build with scheme on parent router",
+ route: new(Route).Schemes("ftp").Host("google.com").Subrouter().Path("/"),
+ request: newRequest("GET", "ftp://google.com/"),
+ scheme: "ftp",
+ host: "google.com",
+ path: "/",
+ pathTemplate: `/`,
+ hostTemplate: `google.com`,
+ shouldMatch: true,
+ },
+ {
+ title: "Prefer scheme on child route when building URLs",
+ route: new(Route).Schemes("https", "ftp").Host("google.com").Subrouter().Schemes("ftp").Path("/"),
+ request: newRequest("GET", "ftp://google.com/"),
+ scheme: "ftp",
+ host: "google.com",
+ path: "/",
+ pathTemplate: `/`,
+ hostTemplate: `google.com`,
+ shouldMatch: true,
+ },
}
for _, test := range tests {
@@ -1382,14 +1432,58 @@ func TestWalkNested(t *testing.T) {
l2 := l1.PathPrefix("/l").Subrouter()
l2.Path("/a")
- paths := []string{"/g", "/g/o", "/g/o/r", "/g/o/r/i", "/g/o/r/i/l", "/g/o/r/i/l/l", "/g/o/r/i/l/l/a"}
+ testCases := []struct {
+ path string
+ ancestors []*Route
+ }{
+ {"/g", []*Route{}},
+ {"/g/o", []*Route{g.parent.(*Route)}},
+ {"/g/o/r", []*Route{g.parent.(*Route), o.parent.(*Route)}},
+ {"/g/o/r/i", []*Route{g.parent.(*Route), o.parent.(*Route), r.parent.(*Route)}},
+ {"/g/o/r/i/l", []*Route{g.parent.(*Route), o.parent.(*Route), r.parent.(*Route), i.parent.(*Route)}},
+ {"/g/o/r/i/l/l", []*Route{g.parent.(*Route), o.parent.(*Route), r.parent.(*Route), i.parent.(*Route), l1.parent.(*Route)}},
+ {"/g/o/r/i/l/l/a", []*Route{g.parent.(*Route), o.parent.(*Route), r.parent.(*Route), i.parent.(*Route), l1.parent.(*Route), l2.parent.(*Route)}},
+ }
+
idx := 0
err := router.Walk(func(route *Route, router *Router, ancestors []*Route) error {
- path := paths[idx]
+ path := testCases[idx].path
tpl := route.regexp.path.template
if tpl != path {
t.Errorf(`Expected %s got %s`, path, tpl)
}
+ currWantAncestors := testCases[idx].ancestors
+ if !reflect.DeepEqual(currWantAncestors, ancestors) {
+ t.Errorf(`Expected %+v got %+v`, currWantAncestors, ancestors)
+ }
+ idx++
+ return nil
+ })
+ if err != nil {
+ panic(err)
+ }
+ if idx != len(testCases) {
+ t.Errorf("Expected %d routes, found %d", len(testCases), idx)
+ }
+}
+
+func TestWalkSubrouters(t *testing.T) {
+ router := NewRouter()
+
+ g := router.Path("/g").Subrouter()
+ o := g.PathPrefix("/o").Subrouter()
+ o.Methods("GET")
+ o.Methods("PUT")
+
+ // all 4 routes should be matched, but final 2 routes do not have path templates
+ paths := []string{"/g", "/g/o", "", ""}
+ idx := 0
+ err := router.Walk(func(route *Route, router *Router, ancestors []*Route) error {
+ path := paths[idx]
+ tpl, _ := route.GetPathTemplate()
+ if tpl != path {
+ t.Errorf(`Expected %s got %s`, path, tpl)
+ }
idx++
return nil
})
@@ -1492,6 +1586,7 @@ func testRoute(t *testing.T, test routeTest) {
route := test.route
vars := test.vars
shouldMatch := test.shouldMatch
+ query := test.query
shouldRedirect := test.shouldRedirect
uri := url.URL{
Scheme: test.scheme,
@@ -1561,6 +1656,13 @@ func testRoute(t *testing.T, test routeTest) {
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 {
t.Errorf("(%v) Did not redirect", test.title)
return
@@ -1769,3 +1871,42 @@ func newRequest(method, url string) *http.Request {
}
return req
}
+
+func TestNoMatchMethodErrorHandler(t *testing.T) {
+ func1 := func(w http.ResponseWriter, r *http.Request) {}
+
+ r := NewRouter()
+ r.HandleFunc("/", func1).Methods("GET", "POST")
+
+ req, _ := http.NewRequest("PUT", "http://localhost/", nil)
+ match := new(RouteMatch)
+ matched := r.Match(req, match)
+
+ if matched {
+ t.Error("Should not have matched route for methods")
+ }
+
+ if match.MatchErr != ErrMethodMismatch {
+ t.Error("Should get ErrMethodMismatch error")
+ }
+
+ resp := NewRecorder()
+ r.ServeHTTP(resp, req)
+ if resp.Code != 405 {
+ t.Errorf("Expecting code %v", 405)
+ }
+
+ // Add matching route
+ r.HandleFunc("/", func1).Methods("PUT")
+
+ match = new(RouteMatch)
+ matched = r.Match(req, match)
+
+ if !matched {
+ t.Error("Should have matched route for methods")
+ }
+
+ if match.MatchErr != nil {
+ t.Error("Should not have any matching error. Found:", match.MatchErr)
+ }
+}