From 0275f5dca7c66640587294b95ce396dd6d6a02c8 Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 13 Jun 2025 15:51:50 -0600 Subject: fix: fix double write header issue --- app/controllers/dashboard/controller.go | 2 -- app/controllers/dashboard/controller_test.go | 26 +++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/dashboard/controller.go b/app/controllers/dashboard/controller.go index d279930..c722503 100644 --- a/app/controllers/dashboard/controller.go +++ b/app/controllers/dashboard/controller.go @@ -35,7 +35,6 @@ func (c *Controller) Show(w http.ResponseWriter, r *http.Request) { dto := &ViewDashboardDTO{CurrentUser: currentUser} if err := views.Render(w, "dashboard/show", dto); err != nil { pls.LogError(r.Context(), err) - w.WriteHeader(http.StatusInternalServerError) return } } @@ -50,7 +49,6 @@ func (c *Controller) Navigation(w http.ResponseWriter, r *http.Request) { } if err := views.Render(w, "dashboard/nav", dto); err != nil { pls.LogError(r.Context(), err) - w.WriteHeader(http.StatusInternalServerError) return } } diff --git a/app/controllers/dashboard/controller_test.go b/app/controllers/dashboard/controller_test.go index ddbfd34..3472565 100644 --- a/app/controllers/dashboard/controller_test.go +++ b/app/controllers/dashboard/controller_test.go @@ -1,7 +1,9 @@ package dashboard import ( + "errors" "net/http" + "net/http/httptest" "testing" "github.com/stretchr/testify/assert" @@ -11,6 +13,22 @@ import ( "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" ) +type FailingResponseWriter struct { + *testing.T + *httptest.ResponseRecorder + headerWritten bool +} + +func (f *FailingResponseWriter) WriteHeader(statusCode int) { + require.False(f.T, f.headerWritten) + f.headerWritten = true + f.ResponseRecorder.WriteHeader(statusCode) +} + +func (f *FailingResponseWriter) Write([]byte) (int, error) { + return 0, errors.New("write failed") +} + func TestController(t *testing.T) { mux := http.NewServeMux() controller := New() @@ -38,6 +56,13 @@ func TestController(t *testing.T) { assert.Contains(t, w.Body.String(), "