@@ -22,22 +22,43 @@ import (
22
22
"github.com/stretchr/testify/assert"
23
23
)
24
24
25
- func testRequest (t * testing.T , url string ) {
25
+ // params[0]=url example:http://127.0.0.1:8080/index (cannot be empty)
26
+ // params[1]=response status (custom compare status) default:"200 OK"
27
+ // params[2]=response body (custom compare content) default:"it worked"
28
+ func testRequest (t * testing.T , params ... string ) {
29
+
30
+ if len (params ) == 0 {
31
+ t .Fatal ("url cannot be empty" )
32
+ }
33
+
26
34
tr := & http.Transport {
27
35
TLSClientConfig : & tls.Config {
28
36
InsecureSkipVerify : true ,
29
37
},
30
38
}
31
39
client := & http.Client {Transport : tr }
32
40
33
- resp , err := client .Get (url )
41
+ resp , err := client .Get (params [ 0 ] )
34
42
assert .NoError (t , err )
35
43
defer resp .Body .Close ()
36
44
37
45
body , ioerr := ioutil .ReadAll (resp .Body )
38
46
assert .NoError (t , ioerr )
39
- assert .Equal (t , "it worked" , string (body ), "resp body should match" )
40
- assert .Equal (t , "200 OK" , resp .Status , "should get a 200" )
47
+
48
+ var responseStatus = "200 OK"
49
+ if len (params ) > 1 && params [1 ] != "" {
50
+ responseStatus = params [1 ]
51
+ }
52
+
53
+ var responseBody = "it worked"
54
+ if len (params ) > 2 && params [2 ] != "" {
55
+ responseBody = params [2 ]
56
+ }
57
+
58
+ assert .Equal (t , responseStatus , resp .Status , "should get a " + responseStatus )
59
+ if responseStatus == "200 OK" {
60
+ assert .Equal (t , responseBody , string (body ), "resp body should match" )
61
+ }
41
62
}
42
63
43
64
func TestRunEmpty (t * testing.T ) {
@@ -312,3 +333,52 @@ func testGetRequestHandler(t *testing.T, h http.Handler, url string) {
312
333
assert .Equal (t , "it worked" , w .Body .String (), "resp body should match" )
313
334
assert .Equal (t , 200 , w .Code , "should get a 200" )
314
335
}
336
+
337
+ func TestTreeRunDynamicRouting (t * testing.T ) {
338
+ router := New ()
339
+ router .GET ("/aa/*xx" , func (c * Context ) { c .String (http .StatusOK , "/aa/*xx" ) })
340
+ router .GET ("/ab/*xx" , func (c * Context ) { c .String (http .StatusOK , "/ab/*xx" ) })
341
+ router .GET ("/" , func (c * Context ) { c .String (http .StatusOK , "home" ) })
342
+ router .GET ("/:cc" , func (c * Context ) { c .String (http .StatusOK , "/:cc" ) })
343
+ router .GET ("/:cc/cc" , func (c * Context ) { c .String (http .StatusOK , "/:cc/cc" ) })
344
+ router .GET ("/:cc/:dd/ee" , func (c * Context ) { c .String (http .StatusOK , "/:cc/:dd/ee" ) })
345
+ router .GET ("/:cc/:dd/:ee/ff" , func (c * Context ) { c .String (http .StatusOK , "/:cc/:dd/:ee/ff" ) })
346
+ router .GET ("/:cc/:dd/:ee/:ff/gg" , func (c * Context ) { c .String (http .StatusOK , "/:cc/:dd/:ee/:ff/gg" ) })
347
+ router .GET ("/:cc/:dd/:ee/:ff/:gg/hh" , func (c * Context ) { c .String (http .StatusOK , "/:cc/:dd/:ee/:ff/:gg/hh" ) })
348
+ router .GET ("/get/test/abc/" , func (c * Context ) { c .String (http .StatusOK , "/get/test/abc/" ) })
349
+ router .GET ("/get/:param/abc/" , func (c * Context ) { c .String (http .StatusOK , "/get/:param/abc/" ) })
350
+ router .GET ("/something/:paramname/thirdthing" , func (c * Context ) { c .String (http .StatusOK , "/something/:paramname/thirdthing" ) })
351
+ router .GET ("/something/secondthing/test" , func (c * Context ) { c .String (http .StatusOK , "/something/secondthing/test" ) })
352
+
353
+ ts := httptest .NewServer (router )
354
+ defer ts .Close ()
355
+
356
+ testRequest (t , ts .URL + "/" , "" , "home" )
357
+ testRequest (t , ts .URL + "/aa/aa" , "" , "/aa/*xx" )
358
+ testRequest (t , ts .URL + "/ab/ab" , "" , "/ab/*xx" )
359
+ testRequest (t , ts .URL + "/all" , "" , "/:cc" )
360
+ testRequest (t , ts .URL + "/all/cc" , "" , "/:cc/cc" )
361
+ testRequest (t , ts .URL + "/a/cc" , "" , "/:cc/cc" )
362
+ testRequest (t , ts .URL + "/c/d/ee" , "" , "/:cc/:dd/ee" )
363
+ testRequest (t , ts .URL + "/c/d/e/ff" , "" , "/:cc/:dd/:ee/ff" )
364
+ testRequest (t , ts .URL + "/c/d/e/f/gg" , "" , "/:cc/:dd/:ee/:ff/gg" )
365
+ testRequest (t , ts .URL + "/c/d/e/f/g/hh" , "" , "/:cc/:dd/:ee/:ff/:gg/hh" )
366
+ testRequest (t , ts .URL + "/a" , "" , "/:cc" )
367
+ testRequest (t , ts .URL + "/get/test/abc/" , "" , "/get/test/abc/" )
368
+ testRequest (t , ts .URL + "/get/te/abc/" , "" , "/get/:param/abc/" )
369
+ testRequest (t , ts .URL + "/get/xx/abc/" , "" , "/get/:param/abc/" )
370
+ testRequest (t , ts .URL + "/get/tt/abc/" , "" , "/get/:param/abc/" )
371
+ testRequest (t , ts .URL + "/get/a/abc/" , "" , "/get/:param/abc/" )
372
+ testRequest (t , ts .URL + "/get/t/abc/" , "" , "/get/:param/abc/" )
373
+ testRequest (t , ts .URL + "/get/aa/abc/" , "" , "/get/:param/abc/" )
374
+ testRequest (t , ts .URL + "/get/abas/abc/" , "" , "/get/:param/abc/" )
375
+ testRequest (t , ts .URL + "/something/secondthing/test" , "" , "/something/secondthing/test" )
376
+ testRequest (t , ts .URL + "/something/abcdad/thirdthing" , "" , "/something/:paramname/thirdthing" )
377
+ testRequest (t , ts .URL + "/something/se/thirdthing" , "" , "/something/:paramname/thirdthing" )
378
+ testRequest (t , ts .URL + "/something/s/thirdthing" , "" , "/something/:paramname/thirdthing" )
379
+ testRequest (t , ts .URL + "/something/secondthing/thirdthing" , "" , "/something/:paramname/thirdthing" )
380
+ // 404 not found
381
+ testRequest (t , ts .URL + "/a/dd" , "404 Not Found" )
382
+ testRequest (t , ts .URL + "/addr/dd/aa" , "404 Not Found" )
383
+ testRequest (t , ts .URL + "/something/secondthing/121" , "404 Not Found" )
384
+ }
0 commit comments