1
1
package coderd
2
2
3
3
import (
4
+ "context"
4
5
"net/http"
5
6
"net/http/httptest"
6
7
"testing"
8
+
9
+ "github.com/go-chi/chi/v5"
10
+ "github.com/stretchr/testify/assert"
7
11
)
8
12
9
13
func TestStripSlashesMW (t * testing.T ) {
@@ -30,18 +34,26 @@ func TestStripSlashesMW(t *testing.T) {
30
34
31
35
for _ , tt := range tests {
32
36
tt := tt
33
-
34
37
t .Run (tt .name , func (t * testing.T ) {
35
38
t .Parallel ()
36
39
37
40
req := httptest .NewRequest ("GET" , tt .inputPath , nil )
38
41
rec := httptest .NewRecorder ()
39
42
43
+ // Create a chi RouteContext and attach it to the request
44
+ rctx := chi .NewRouteContext ()
45
+ rctx .RoutePath = tt .inputPath // Simulate chi route path
46
+ req = req .WithContext (context .WithValue (req .Context (), chi .RouteCtxKey , rctx ))
47
+
48
+ // Pass the request through the middleware
40
49
stripSlashesMW (handler ).ServeHTTP (rec , req )
41
50
42
- if req .URL .Path != tt .wantPath {
43
- t .Errorf ("stripSlashesMW got path = %q, want %q" , req .URL .Path , tt .wantPath )
44
- }
51
+ // Get the updated chi RouteContext after middleware processing
52
+ updatedCtx := chi .RouteContext (req .Context ())
53
+
54
+ // Validate URL path
55
+ assert .Equal (t , tt .wantPath , req .URL .Path )
56
+ assert .Equal (t , tt .wantPath , updatedCtx .RoutePath )
45
57
})
46
58
}
47
59
}
0 commit comments