From 1da24232af162d4563ee05fb4138585df7a20678 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 15 Aug 2026 23:43:55 +1000 Subject: [PATCH] fix(tests): add contexts to HTTP test requests --- internal/channels/webhook/webhook_test.go | 12 ++++++------ internal/webui/api_chat_test.go | 14 +++++++------- internal/webui/auth_test.go | 5 +++-- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/internal/channels/webhook/webhook_test.go b/internal/channels/webhook/webhook_test.go index 27e1e52f2..64b020a38 100644 --- a/internal/channels/webhook/webhook_test.go +++ b/internal/channels/webhook/webhook_test.go @@ -63,7 +63,7 @@ func TestWebhookHandlerMethodNotAllowed(t *testing.T) { g := New("", 0, []RouteConfig{{Path: "/hook"}}, slog.Default()) handler := g.WebhookHandler() rec := httptest.NewRecorder() - handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/hook", nil)) + handler.ServeHTTP(rec, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/hook", nil)) if rec.Code != http.StatusMethodNotAllowed { t.Errorf("status = %d, want 405", rec.Code) } @@ -73,7 +73,7 @@ func TestWebhookHandlerEmptyBody(t *testing.T) { g := New("", 0, []RouteConfig{{Path: "/hook"}}, slog.Default()) handler := g.WebhookHandler() rec := httptest.NewRecorder() - handler.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/hook", nil)) + handler.ServeHTTP(rec, httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/hook", nil)) if rec.Code != http.StatusBadRequest { t.Errorf("status = %d, want 400", rec.Code) } @@ -84,7 +84,7 @@ func TestWebhookHandlerNotStarted(t *testing.T) { handler := g.WebhookHandler() rec := httptest.NewRecorder() body := strings.NewReader(`{"text":"hello"}`) - handler.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/hook", body)) + handler.ServeHTTP(rec, httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/hook", body)) if rec.Code != http.StatusServiceUnavailable { t.Errorf("status = %d, want 503", rec.Code) } @@ -98,7 +98,7 @@ func TestWebhookHandlerRoute(t *testing.T) { handler := g.WebhookHandler() payload := `{"text":"hello from webhook"}` rec := httptest.NewRecorder() - handler.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/hook", strings.NewReader(payload))) + handler.ServeHTTP(rec, httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/hook", strings.NewReader(payload))) if rec.Code != http.StatusAccepted { t.Errorf("status = %d, want 202", rec.Code) } @@ -110,7 +110,7 @@ func TestWebhookHandlerInvalidSignature(t *testing.T) { handler := g.WebhookHandler() rec := httptest.NewRecorder() - handler.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/hook", strings.NewReader(`{"text":"x"}`))) + handler.ServeHTTP(rec, httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/hook", strings.NewReader(`{"text":"x"}`))) if rec.Code != http.StatusUnauthorized { t.Errorf("status = %d, want 401", rec.Code) } @@ -127,7 +127,7 @@ func TestWebhookHandlerValidSignature(t *testing.T) { g.router = gateway.NewRouter(nil, nil, "webhook") handler := g.WebhookHandler() - req := httptest.NewRequest(http.MethodPost, "/hook", strings.NewReader(string(body))) + req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/hook", strings.NewReader(string(body))) req.Header.Set("X-Hub-Signature-256", sig) rec := httptest.NewRecorder() diff --git a/internal/webui/api_chat_test.go b/internal/webui/api_chat_test.go index 37e1fb216..e812df51b 100644 --- a/internal/webui/api_chat_test.go +++ b/internal/webui/api_chat_test.go @@ -195,7 +195,7 @@ func TestChatSessionsExposeActiveSelectorState(t *testing.T) { } res := httptest.NewRecorder() - server.Handler().ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/api/chat/sessions", nil)) + server.Handler().ServeHTTP(res, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/chat/sessions", nil)) if res.Code != http.StatusOK { t.Fatalf("status = %d, body = %s", res.Code, res.Body) } @@ -266,7 +266,7 @@ func TestChatUpdateEndpointRejectsTypedNilService(t *testing.T) { server := &Server{Chat: &ChatService{Router: router, Sessions: sessions, Updates: updates}} res := httptest.NewRecorder() - server.Handler().ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/api/chat/update", nil)) + server.Handler().ServeHTTP(res, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/chat/update", nil)) if res.Code != http.StatusNotImplemented { t.Fatalf("typed-nil update service status = %d, body = %s", res.Code, res.Body.String()) } @@ -280,13 +280,13 @@ func TestChatDangerousApprovalEndpoints(t *testing.T) { server := &Server{Chat: &ChatService{Router: router, Sessions: sessions, Dangerous: NewDangerousService(authority)}} state := httptest.NewRecorder() - server.Handler().ServeHTTP(state, httptest.NewRequest(http.MethodGet, "/api/chat/dangerous", nil)) + server.Handler().ServeHTTP(state, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/chat/dangerous", nil)) if state.Code != http.StatusOK || !bytes.Contains(state.Body.Bytes(), []byte(`"Number":3`)) { t.Fatalf("dangerous state = %d, body = %s", state.Code, state.Body.String()) } request := httptest.NewRecorder() - server.Handler().ServeHTTP(request, httptest.NewRequest(http.MethodPost, "/api/chat/dangerous/rollback", bytes.NewBufferString(`{"spec":"3"}`))) + server.Handler().ServeHTTP(request, httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/api/chat/dangerous/rollback", bytes.NewBufferString(`{"spec":"3"}`))) if request.Code != http.StatusOK || len(authority.rollbackCalls) != 0 { t.Fatalf("rollback request = %d, body = %s, calls=%v", request.Code, request.Body.String(), authority.rollbackCalls) } @@ -299,7 +299,7 @@ func TestChatDangerousApprovalEndpoints(t *testing.T) { decisionBody := bytes.NewBufferString(`{"decision":"approve"}`) decision := httptest.NewRecorder() - server.Handler().ServeHTTP(decision, httptest.NewRequest(http.MethodPost, "/api/chat/dangerous/"+pending.Action.ID+"/decision", decisionBody)) + server.Handler().ServeHTTP(decision, httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/api/chat/dangerous/"+pending.Action.ID+"/decision", decisionBody)) if decision.Code != http.StatusOK || len(authority.rollbackCalls) != 1 || authority.rollbackCalls[0] != 3 { t.Fatalf("rollback decision = %d, body = %s, calls=%v", decision.Code, decision.Body.String(), authority.rollbackCalls) } @@ -314,7 +314,7 @@ func TestChatCommandCatalogIncludesEnabledCapabilities(t *testing.T) { Updates: &chatUpdateStub{}, Dangerous: NewDangerousService(&webDangerousStub{}), }} res := httptest.NewRecorder() - server.Handler().ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/api/chat/sessions", nil)) + server.Handler().ServeHTTP(res, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/chat/sessions", nil)) if res.Code != http.StatusOK { t.Fatalf("catalog status = %d, body = %s", res.Code, res.Body.String()) } @@ -357,7 +357,7 @@ func TestChatCancelEndpointStopsSessionTurn(t *testing.T) { }) <-started - req := httptest.NewRequest(http.MethodPost, "/api/chat/cancel", strings.NewReader(`{"session_id":"session-1"}`)) + req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/api/chat/cancel", strings.NewReader(`{"session_id":"session-1"}`)) res := httptest.NewRecorder() server.Handler().ServeHTTP(res, req) if res.Code != http.StatusOK { diff --git a/internal/webui/auth_test.go b/internal/webui/auth_test.go index 8541b30ef..8ac5d9b85 100644 --- a/internal/webui/auth_test.go +++ b/internal/webui/auth_test.go @@ -1,6 +1,7 @@ package webui import ( + "context" "net/http" "net/http/httptest" "strings" @@ -13,7 +14,7 @@ func TestRequireToken_RedirectRejectsProtocolRelativePath(t *testing.T) { w.WriteHeader(http.StatusOK) })) - req := httptest.NewRequest(http.MethodGet, "http://archie.local//evil.example//x?t=secret-token", nil) + req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "http://archie.local//evil.example//x?t=secret-token", nil) rec := httptest.NewRecorder() h.ServeHTTP(rec, req) @@ -59,7 +60,7 @@ func TestRequireToken_RedirectStripsTokenFromCleanURL(t *testing.T) { w.WriteHeader(http.StatusOK) })) - req := httptest.NewRequest(http.MethodGet, "http://archie.local/dashboard?t=secret-token&foo=bar", nil) + req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "http://archie.local/dashboard?t=secret-token&foo=bar", nil) rec := httptest.NewRecorder() h.ServeHTTP(rec, req)