@@ -333,6 +333,65 @@ func TestTreeRegexp(t *testing.T) {
333333 }
334334}
335335
336+ func TestTreeSlashParam (t * testing.T ) {
337+ hNested := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {})
338+ hGreedy := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {})
339+ hEmpty := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {})
340+ hEmbeddedEmpty := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {})
341+ hTerminal := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {})
342+ hRegexp := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {})
343+
344+ tr := & node {}
345+ tr .InsertRoute (mGET , "/foo/bar/{other-stuff:*}/fizz/buzz" , hNested )
346+ tr .InsertRoute (mGET , "/greedy/{path:*}/bar" , hGreedy )
347+ tr .InsertRoute (mGET , "/empty/{path:*}/bar" , hEmpty )
348+ tr .InsertRoute (mGET , "/embedded/prefix-{path:*}/bar" , hEmbeddedEmpty )
349+ tr .InsertRoute (mGET , "/files/{path:*}" , hTerminal )
350+ tr .InsertRoute (mGET , "/regexp/{path:.*}/tail" , hRegexp )
351+
352+ tests := []struct {
353+ r string
354+ h http.Handler
355+ k []string
356+ v []string
357+ }{
358+ {r : "/foo/bar/one/two/three/fizz/buzz" , h : hNested , k : []string {"other-stuff" }, v : []string {"one/two/three" }},
359+ {r : "/greedy/one/bar/two/bar" , h : hGreedy , k : []string {"path" }, v : []string {"one/bar/two" }},
360+ {r : "/empty/bar" , h : hEmpty , k : []string {"path" }, v : []string {"" }},
361+ {r : "/empty//bar" , h : hEmpty , k : []string {"path" }, v : []string {"" }},
362+ {r : "/embedded/prefix-bar" , h : hEmbeddedEmpty , k : []string {"path" }, v : []string {"" }},
363+ {r : "/files" , h : hTerminal , k : []string {"path" }, v : []string {"" }},
364+ {r : "/files/" , h : hTerminal , k : []string {"path" }, v : []string {"" }},
365+ {r : "/files/a/b" , h : hTerminal , k : []string {"path" }, v : []string {"a/b" }},
366+ {r : "/regexp/a/b/tail" , h : nil , k : []string {}, v : []string {}},
367+ {r : "/regexp/abc/tail" , h : hRegexp , k : []string {"path" }, v : []string {"abc" }},
368+ }
369+
370+ for i , tt := range tests {
371+ rctx := NewRouteContext ()
372+
373+ _ , handlers , _ := tr .FindRoute (rctx , mGET , tt .r )
374+
375+ var handler http.Handler
376+ if methodHandler , ok := handlers [mGET ]; ok {
377+ handler = methodHandler .handler
378+ }
379+
380+ paramKeys := rctx .routeParams .Keys
381+ paramValues := rctx .routeParams .Values
382+
383+ if fmt .Sprintf ("%v" , tt .h ) != fmt .Sprintf ("%v" , handler ) {
384+ t .Errorf ("input [%d]: find '%s' expecting handler:%v , got:%v" , i , tt .r , tt .h , handler )
385+ }
386+ if ! stringSliceEqual (tt .k , paramKeys ) {
387+ t .Errorf ("input [%d]: find '%s' expecting paramKeys:(%d)%v , got:(%d)%v" , i , tt .r , len (tt .k ), tt .k , len (paramKeys ), paramKeys )
388+ }
389+ if ! stringSliceEqual (tt .v , paramValues ) {
390+ t .Errorf ("input [%d]: find '%s' expecting paramValues:(%d)%v , got:(%d)%v" , i , tt .r , len (tt .v ), tt .v , len (paramValues ), paramValues )
391+ }
392+ }
393+ }
394+
336395func TestTreeRegexpRecursive (t * testing.T ) {
337396 hStub1 := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {})
338397 hStub2 := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {})
0 commit comments