@@ -10,22 +10,38 @@ import (
10
10
"testing"
11
11
)
12
12
13
- func TestGetRoutePattern (t * testing.T ) {
13
+ func TestGetRoutePatternIncludingSubrouters (t * testing.T ) {
14
14
r := chi .NewRouter ()
15
+ subRouter := chi .NewRouter ()
15
16
16
- routePattern := "/test/{param}"
17
- r .Get (routePattern , func (w http.ResponseWriter , r * http.Request ) {
17
+ // Configure a test route on the subrouter
18
+ subRoutePattern := "/sub/{subParam}"
19
+ subRouter .Get (subRoutePattern , func (w http.ResponseWriter , r * http.Request ) {
18
20
routePattern := GetRoutePattern (r )
19
-
20
- assert .Equal (t , routePattern , "/test/{param}" , "The returned route pattern should match the one configured in the router." )
21
+ assert .Equal (t , "/test/sub/{subParam}" , routePattern , "The returned route pattern should match the subrouter pattern." )
21
22
})
22
23
23
- req := httptest .NewRequest ("GET" , "/test/123" , nil )
24
- w := httptest .NewRecorder ()
24
+ // Mount the subrouter onto a specific path of the main router
25
+ r .Mount ("/test" , subRouter )
26
+
27
+ // Test request for the subrouter route
28
+ reqSub := httptest .NewRequest ("GET" , "/test/sub/456" , nil )
29
+ wSub := httptest .NewRecorder ()
30
+ r .ServeHTTP (wSub , reqSub )
31
+ assert .Equal (t , http .StatusOK , wSub .Code , "The expected status code for subrouter should be 200 OK." )
25
32
26
- r .ServeHTTP (w , req )
33
+ // Configure a test route on the main router
34
+ mainRoutePattern := "/main/{mainParam}"
35
+ r .Get (mainRoutePattern , func (w http.ResponseWriter , r * http.Request ) {
36
+ routePattern := GetRoutePattern (r )
37
+ assert .Equal (t , "/main/{mainParam}" , routePattern , "The returned route pattern should match the main router pattern." )
38
+ })
27
39
28
- assert .Equal (t , http .StatusOK , w .Code , "The expected status code should be 200 OK." )
40
+ // Test request for the main router route
41
+ reqMain := httptest .NewRequest ("GET" , "/main/123" , nil )
42
+ wMain := httptest .NewRecorder ()
43
+ r .ServeHTTP (wMain , reqMain )
44
+ assert .Equal (t , http .StatusOK , wMain .Code , "The expected status code for main router should be 200 OK." )
29
45
}
30
46
31
47
func TestGetRequestBody (t * testing.T ) {
0 commit comments