Skip to content

Commit bac1372

Browse files
committed
Convert TestStrictSlash to a table driven test
1 parent 033224c commit bac1372

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

mux_test.go

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ import (
1313
)
1414

1515
type routeTest struct {
16-
title string // title of the test
17-
route *Route // the route being tested
18-
request *http.Request // a request to test the route
19-
vars map[string]string // the expected vars of the match
20-
host string // the expected host of the match
21-
path string // the expected path of the match
22-
shouldMatch bool // whether the request is expected to match the route at all
16+
title string // title of the test
17+
route *Route // the route being tested
18+
request *http.Request // a request to test the route
19+
vars map[string]string // the expected vars of the match
20+
host string // the expected host of the match
21+
path string // the expected path of the match
22+
shouldMatch bool // whether the request is expected to match the route at all
23+
shouldRedirect bool // whether the request should result in a redirect
2324
}
2425

2526
func TestHost(t *testing.T) {
@@ -615,26 +616,23 @@ func TestNamedRoutes(t *testing.T) {
615616
}
616617

617618
func TestStrictSlash(t *testing.T) {
618-
var r *Router
619-
var req *http.Request
620-
var route *Route
621-
var match *RouteMatch
622-
var matched bool
623-
624-
// StrictSlash should be ignored for path prefix.
625-
// So we register a route ending in slash but it doesn't attempt to add
626-
// the slash for a path not ending in slash.
627-
r = NewRouter()
619+
r := NewRouter()
628620
r.StrictSlash(true)
629-
route = r.NewRoute().PathPrefix("/static/")
630-
req, _ = http.NewRequest("GET", "http://localhost/static/logo.png", nil)
631-
match = new(RouteMatch)
632-
matched = r.Match(req, match)
633-
if !matched {
634-
t.Errorf("Should match request %q -- %v", req.URL.Path, getRouteTemplate(route))
621+
622+
tests := []routeTest{
623+
{
624+
title: "Ignore StrictSlash for path prefix",
625+
route: r.NewRoute().PathPrefix("/static/"),
626+
request: newRequest("GET", "http://localhost/static/logo.png"),
627+
vars: map[string]string{},
628+
path: "/static/",
629+
shouldMatch: true,
630+
shouldRedirect: false,
631+
},
635632
}
636-
if match.Handler != nil {
637-
t.Errorf("Should not redirect")
633+
634+
for _, test := range tests {
635+
testRoute(t, test)
638636
}
639637
}
640638

@@ -663,6 +661,7 @@ func testRoute(t *testing.T, test routeTest) {
663661
host := test.host
664662
path := test.path
665663
url := test.host + test.path
664+
shouldRedirect := test.shouldRedirect
666665

667666
var match RouteMatch
668667
ok := route.Match(request, &match)
@@ -700,6 +699,14 @@ func testRoute(t *testing.T, test routeTest) {
700699
return
701700
}
702701
}
702+
if shouldRedirect && match.Handler == nil {
703+
t.Errorf("(%v) Did not redirect", test.title)
704+
return
705+
}
706+
if !shouldRedirect && match.Handler != nil {
707+
t.Errorf("(%v) Unexpected redirect", test.title)
708+
return
709+
}
703710
}
704711
}
705712

0 commit comments

Comments
 (0)