@@ -137,35 +137,27 @@ func TestHandler(t *testing.T) {
137
137
}
138
138
139
139
for _ , tt := range tests {
140
- tt := tt
141
140
t .Run (tt .name , func (t * testing.T ) {
142
141
t .Parallel ()
143
142
144
- // Create router with URL parameter handling
143
+ // Setup router with the handler under test.
145
144
r := chi .NewRouter ()
146
145
handler := agentcontainers .New (
147
146
agentcontainers .WithLister (tt .lister ),
148
147
agentcontainers .WithDevcontainerCLI (tt .devcontainerCLI ),
149
148
)
150
-
151
149
r .Post ("/containers/{id}/recreate" , handler .Recreate )
152
150
153
- // Create test request
151
+ // Simulate HTTP request to the recreate endpoint.
154
152
req := httptest .NewRequest (http .MethodPost , "/containers/" + tt .containerID + "/recreate" , nil )
155
153
rec := httptest .NewRecorder ()
156
-
157
- // Run the handler
158
154
r .ServeHTTP (rec , req )
159
155
160
- // Check status code - use require to fail fast if this doesn't match
156
+ // Check the response status code and body.
161
157
require .Equal (t , tt .wantStatus , rec .Code , "status code mismatch" )
162
-
163
- // For non-empty expected responses, check the body contains the expected message
164
158
if tt .wantBody != "" {
165
- // For error responses, contains is appropriate as we don't need to test the entire response
166
159
assert .Contains (t , rec .Body .String (), tt .wantBody , "response body mismatch" )
167
160
} else if tt .wantStatus == http .StatusNoContent {
168
- // For success cases, verify the body is actually empty
169
161
assert .Empty (t , rec .Body .String (), "expected empty response body" )
170
162
}
171
163
})
0 commit comments