MCPcopy Create free account
hub / github.com/krau/SaveAny-Bot / TestGetTaskHandler

Function TestGetTaskHandler

api/handlers_test.go:177–240  ·  view source on GitHub ↗

TestGetTaskHandler tests the get task endpoint

(t *testing.T)

Source from the content-addressed store, hash-verified

175
176// TestGetTaskHandler tests the get task endpoint
177func TestGetTaskHandler(t *testing.T) {
178 handlers, _ := setupTestServer(t)
179
180 // Register a test task
181 testTaskID := "test-get-task"
182 RegisterTask(testTaskID, "directlinks", "local", "downloads", "Test", "")
183 defer DeleteTask(testTaskID)
184
185 tests := []struct {
186 name string
187 method string
188 path string
189 wantStatus int
190 wantFound bool
191 }{
192 {
193 name: "Method not allowed",
194 method: http.MethodPost,
195 path: "/api/v1/tasks/test-id",
196 wantStatus: http.StatusMethodNotAllowed,
197 },
198 {
199 name: "Missing task ID",
200 method: http.MethodGet,
201 path: "/api/v1/tasks",
202 wantStatus: http.StatusBadRequest,
203 },
204 {
205 name: "Task not found",
206 method: http.MethodGet,
207 path: "/api/v1/tasks/non-existent-task",
208 wantStatus: http.StatusNotFound,
209 },
210 {
211 name: "Task found",
212 method: http.MethodGet,
213 path: "/api/v1/tasks/" + testTaskID,
214 wantStatus: http.StatusOK,
215 wantFound: true,
216 },
217 }
218
219 for _, tt := range tests {
220 t.Run(tt.name, func(t *testing.T) {
221 req := httptest.NewRequest(tt.method, tt.path, nil)
222 rr := httptest.NewRecorder()
223 handlers.GetTaskHandler(rr, req)
224
225 if rr.Code != tt.wantStatus {
226 t.Errorf("expected status %d, got %d", tt.wantStatus, rr.Code)
227 }
228
229 if tt.wantFound {
230 var resp TaskInfoResponse
231 if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
232 t.Errorf("failed to unmarshal response: %v", err)
233 }
234 if resp.TaskID != testTaskID {

Callers

nothing calls this directly

Calls 5

setupTestServerFunction · 0.85
RegisterTaskFunction · 0.85
DeleteTaskFunction · 0.85
RunMethod · 0.80
GetTaskHandlerMethod · 0.80

Tested by

no test coverage detected