TestGetTaskHandler tests the get task endpoint
(t *testing.T)
| 175 | |
| 176 | // TestGetTaskHandler tests the get task endpoint |
| 177 | func 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 { |
nothing calls this directly
no test coverage detected