@ -29,11 +29,6 @@ type testRequests []struct {
ps Params
}
func getParams ( ) * Params {
ps := make ( Params , 0 , 20 )
return & ps
}
func checkRequests ( t * testing . T , tree * Node , requests testRequests , unescapes ... bool ) {
unescape := false
if len ( unescapes ) >= 1 {
@ -41,7 +36,7 @@ func checkRequests(t *testing.T, tree *Node, requests testRequests, unescapes ..
}
for _ , request := range requests {
value := tree . g etValue( request . path , getParams ( ) , unescape )
value := tree . G etValue( request . path , unescape )
if value . Handler == nil {
if ! request . nilHandler {
@ -114,7 +109,7 @@ func TestTreeAddAndGet(t *testing.T) {
"/β" ,
}
for _ , route := range routes {
tree . a ddRoute( route , fakeHandler ( route ) )
tree . A ddRoute( route , fakeHandler ( route ) )
}
checkRequests ( t , tree , testRequests {
@ -161,6 +156,8 @@ func TestTreeWildcard(t *testing.T) {
"/aa/*xx" ,
"/ab/*xx" ,
"/:cc" ,
"/c1/:dd/e" ,
"/c1/:dd/e1" ,
"/:cc/cc" ,
"/:cc/:dd/ee" ,
"/:cc/:dd/:ee/ff" ,
@ -191,7 +188,7 @@ func TestTreeWildcard(t *testing.T) {
"/get/abc/123abfff/:param" ,
}
for _ , route := range routes {
tree . a ddRoute( route , fakeHandler ( route ) )
tree . A ddRoute( route , fakeHandler ( route ) )
}
checkRequests ( t , tree , testRequests {
@ -242,6 +239,9 @@ func TestTreeWildcard(t *testing.T) {
{ "/alldd" , false , "/:cc" , Params { Param { Key : "cc" , Value : "alldd" } } } ,
{ "/all/cc" , false , "/:cc/cc" , Params { Param { Key : "cc" , Value : "all" } } } ,
{ "/a/cc" , false , "/:cc/cc" , Params { Param { Key : "cc" , Value : "a" } } } ,
{ "/c1/d/e" , false , "/c1/:dd/e" , Params { Param { Key : "dd" , Value : "d" } } } ,
{ "/c1/d/e1" , false , "/c1/:dd/e1" , Params { Param { Key : "dd" , Value : "d" } } } ,
{ "/c1/d/ee" , false , "/:cc/:dd/ee" , Params { Param { Key : "cc" , Value : "c1" } , Param { Key : "dd" , Value : "d" } } } ,
{ "/cc/cc" , false , "/:cc/cc" , Params { Param { Key : "cc" , Value : "cc" } } } ,
{ "/ccc/cc" , false , "/:cc/cc" , Params { Param { Key : "cc" , Value : "ccc" } } } ,
{ "/deedwjfs/cc" , false , "/:cc/cc" , Params { Param { Key : "cc" , Value : "deedwjfs" } } } ,
@ -328,7 +328,7 @@ func TestUnescapeParameters(t *testing.T) {
"/info/:user" ,
}
for _ , route := range routes {
tree . a ddRoute( route , fakeHandler ( route ) )
tree . A ddRoute( route , fakeHandler ( route ) )
}
checkRequests ( t , tree , testRequests {
@ -369,7 +369,7 @@ func testRoutes(t *testing.T, routes []testRoute) {
for _ , route := range routes {
recv := catchPanic ( func ( ) {
tree . a ddRoute( route . path , nil )
tree . A ddRoute( route . path , nil )
} )
if route . conflict {
@ -452,7 +452,7 @@ func TestTreeDuplicatePath(t *testing.T) {
}
for _ , route := range routes {
recv := catchPanic ( func ( ) {
tree . a ddRoute( route , fakeHandler ( route ) )
tree . A ddRoute( route , fakeHandler ( route ) )
} )
if recv != nil {
t . Fatalf ( "panic inserting route '%s': %v" , route , recv )
@ -460,7 +460,7 @@ func TestTreeDuplicatePath(t *testing.T) {
// Add again
recv = catchPanic ( func ( ) {
tree . a ddRoute( route , nil )
tree . A ddRoute( route , nil )
} )
if recv == nil {
t . Fatalf ( "no panic while inserting duplicate route '%s" , route )
@ -489,7 +489,7 @@ func TestEmptyWildcardName(t *testing.T) {
}
for _ , route := range routes {
recv := catchPanic ( func ( ) {
tree . a ddRoute( route , nil )
tree . A ddRoute( route , nil )
} )
if recv == nil {
t . Fatalf ( "no panic while inserting route with empty wildcard name '%s" , route )
@ -519,7 +519,7 @@ func TestTreeCatchAllConflictRoot(t *testing.T) {
func TestTreeCatchMaxParams ( t * testing . T ) {
tree := & Node { }
var route = "/cmd/*filepath"
tree . a ddRoute( route , fakeHandler ( route ) )
tree . A ddRoute( route , fakeHandler ( route ) )
}
func TestTreeDoubleWildcard ( t * testing . T ) {
@ -534,7 +534,7 @@ func TestTreeDoubleWildcard(t *testing.T) {
for _ , route := range routes {
tree := & Node { }
recv := catchPanic ( func ( ) {
tree . a ddRoute( route , nil )
tree . A ddRoute( route , nil )
} )
if rs , ok := recv . ( string ) ; ! ok || ! strings . HasPrefix ( rs , panicMsg ) {
@ -580,11 +580,18 @@ func TestTreeTrailingSlashRedirect(t *testing.T) {
"/doc/go1.html" ,
"/no/a" ,
"/no/b" ,
"/api/hello/:name" ,
"/api/:page/:name" ,
"/api/hello/:name/bar/" ,
"/api/bar/:name" ,
"/api/baz/foo" ,
"/api/baz/foo/bar" ,
"/blog/:p" ,
"/posts/:b/:c" ,
"/posts/b/:c/d/" ,
}
for _ , route := range routes {
recv := catchPanic ( func ( ) {
tree . addRoute ( route , fakeHandler ( route ) )
tree . A ddRoute( route , fakeHandler ( route ) )
} )
if recv != nil {
t . Fatalf ( "panic inserting route '%s': %v" , route , recv )
@ -606,9 +613,20 @@ func TestTreeTrailingSlashRedirect(t *testing.T) {
"/admin/config/" ,
"/admin/config/permissions/" ,
"/doc/" ,
"/admin/static/" ,
"/admin/cfg/" ,
"/admin/cfg/users/" ,
"/api/hello/x/bar" ,
"/api/baz/foo/" ,
"/api/baz/bax/" ,
"/api/bar/huh/" ,
"/api/baz/foo/bar/" ,
"/api/world/abc/" ,
"/blog/pp/" ,
"/posts/b/c/d" ,
}
for _ , route := range tsrRoutes {
value := tree . getValue ( route , nil , false )
value := tree . G etValue( route , false )
if value . Handler != nil {
t . Fatalf ( "non-nil handler for TSR route '%s" , route )
} else if ! value . Tsr {
@ -622,10 +640,14 @@ func TestTreeTrailingSlashRedirect(t *testing.T) {
"/no/" ,
"/_" ,
"/_/" ,
"/api/world/abc" ,
"/api" ,
"/api/" ,
"/api/hello/x/foo" ,
"/api/baz/foo/bad" ,
"/foo/p/p" ,
}
for _ , route := range noTsrRoutes {
value := tree . getValue ( route , nil , false )
value := tree . G etValue( route , false )
if value . Handler != nil {
t . Fatalf ( "non-nil handler for No-TSR route '%s" , route )
} else if value . Tsr {
@ -638,13 +660,13 @@ func TestTreeRootTrailingSlashRedirect(t *testing.T) {
tree := & Node { }
recv := catchPanic ( func ( ) {
tree . a ddRoute( "/:test" , fakeHandler ( "/:test" ) )
tree . A ddRoute( "/:test" , fakeHandler ( "/:test" ) )
} )
if recv != nil {
t . Fatalf ( "panic inserting test route: %v" , recv )
}
value := tree . g etValue( "/" , nil , false )
value := tree . G etValue( "/" , false )
if value . Handler != nil {
t . Fatalf ( "non-nil handler" )
} else if value . Tsr {
@ -696,7 +718,7 @@ func TestTreeFindCaseInsensitivePath(t *testing.T) {
for _ , route := range routes {
recv := catchPanic ( func ( ) {
tree . a ddRoute( route , fakeHandler ( route ) )
tree . A ddRoute( route , fakeHandler ( route ) )
} )
if recv != nil {
t . Fatalf ( "panic inserting route '%s': %v" , route , recv )
@ -816,15 +838,15 @@ func TestTreeInvalidNodeType(t *testing.T) {
const panicMsg = "invalid Node type"
tree := & Node { }
tree . a ddRoute( "/" , fakeHandler ( "/" ) )
tree . a ddRoute( "/:page" , fakeHandler ( "/:page" ) )
tree . A ddRoute( "/" , fakeHandler ( "/" ) )
tree . A ddRoute( "/:page" , fakeHandler ( "/:page" ) )
// set invalid Node type
tree . children [ 0 ] . nType = 42
// normal lookup
recv := catchPanic ( func ( ) {
tree . g etValue( "/test" , nil , false )
tree . G etValue( "/test" , false )
} )
if rs , ok := recv . ( string ) ; ! ok || rs != panicMsg {
t . Fatalf ( "Expected panic '" + panicMsg + "', got '%v'" , recv )
@ -845,11 +867,8 @@ func TestTreeInvalidParamsType(t *testing.T) {
tree . children = append ( tree . children , & Node { } )
tree . children [ 0 ] . nType = 2
// set invalid Params type
params := make ( Params , 0 )
// try to trigger slice bounds out of range with capacity 0
tree . g etValue( "/test" , & params , false )
tree . GetValue ( "/test" , false )
}
func TestTreeWildcardConflictEx ( t * testing . T ) {
@ -868,7 +887,7 @@ func TestTreeWildcardConflictEx(t *testing.T) {
for _ , conflict := range conflicts {
// I have to re-create a 'tree', because the 'tree' will be
// in an inconsistent state when the loop recovers from the
// panic which threw by 'a ddRoute' function.
// panic which threw by 'A ddRoute' function.
tree := & Node { }
routes := [ ... ] string {
"/con:tact" ,
@ -877,11 +896,11 @@ func TestTreeWildcardConflictEx(t *testing.T) {
}
for _ , route := range routes {
tree . a ddRoute( route , fakeHandler ( route ) )
tree . A ddRoute( route , fakeHandler ( route ) )
}
recv := catchPanic ( func ( ) {
tree . a ddRoute( conflict . route , fakeHandler ( conflict . route ) )
tree . A ddRoute( conflict . route , fakeHandler ( conflict . route ) )
} )
if ! regexp . MustCompile ( fmt . Sprintf ( "'%s' in new path .* conflicts with existing wildcard '%s' in existing prefix '%s'" , conflict . segPath , conflict . existSegPath , conflict . existPath ) ) . MatchString ( fmt . Sprint ( recv ) ) {