@@ -8,11 +8,10 @@ func catchPanic(f func()) (recv interface{}) {
88 }()
99
1010 f ()
11-
1211 return
1312}
1413
15- type testRouter struct {
14+ type testRoute struct {
1615 path string
1716 conflict bool
1817}
@@ -64,8 +63,71 @@ func TestAddRoute(t *testing.T) {
6463 }
6564}
6665
67- type testRequest []struct {
66+ type testRequests []struct {
6867 path string
6968 match bool
7069 params map [string ]string
7170}
71+
72+ func TestMatchRoute (t * testing.T ) {
73+ tree := createRootNode ()
74+
75+ routes := [... ]string {
76+ "/hi" ,
77+ "/contact" ,
78+ "/users/:id/" ,
79+ "/books/*" ,
80+ "/search/:item1/settings/:item2" ,
81+ "/co" ,
82+ "/c" ,
83+ "/a" ,
84+ "/ab" ,
85+ "/doc/" ,
86+ "/doc/go_faq.html" ,
87+ "/doc/go1.html" ,
88+ "/α" ,
89+ "/β" ,
90+ }
91+ for _ , route := range routes {
92+ tree .addRoute (route , emptyHandlersChain )
93+ }
94+
95+ requests := testRequests {
96+ {"/a" , true , nil },
97+ {"/" , false , nil },
98+ {"/hi" , true , nil },
99+ {"/contact" , true , nil },
100+ {"/co" , true , nil },
101+ {"/con" , false , nil }, // key mismatch
102+ {"/cona" , false , nil }, // key mismatch
103+ {"/no" , false , nil }, // no matching child
104+ {"/ab" , true , nil },
105+ {"/α" , true , nil },
106+ {"/β" , true , nil },
107+ {"/users/test" , true , map [string ]string {"id" : "test" }},
108+ {"/books/title" , true , nil },
109+ {"/search/test1/settings/test2" , true , map [string ]string {"item1" : "test1" , "item2" : "test2" }},
110+ {"/search/test1" , false , nil },
111+ {"test" , false , nil },
112+ }
113+ for _ , request := range requests {
114+ ctx := & context {paramValues : make (map [string ]string )}
115+ handler := tree .matchRoute (request .path , ctx )
116+
117+ if handler == nil {
118+ if request .match {
119+ t .Errorf ("handle mismatch for route '%s': Expected non-nil handle" , request .path )
120+ }
121+ } else if ! request .match {
122+ t .Errorf ("handle mismatch for route '%s': Expected nil handle" , request .path )
123+ }
124+
125+ for expectedKey , expectedValue := range request .params {
126+ actualValue := ctx .Param (expectedKey )
127+ if actualValue != expectedValue {
128+ t .Errorf (" mismatch for route '%s' parameter '%s' actual '%s', expected '%s'" ,
129+ request .path , expectedKey , actualValue , expectedValue )
130+ }
131+ }
132+ }
133+ }
0 commit comments