summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-06-13 15:51:50 -0600
committermo khan <mo@mokhan.ca>2025-06-13 15:51:50 -0600
commit0275f5dca7c66640587294b95ce396dd6d6a02c8 (patch)
treee43b8e162ecebad3884ccafe5283f94539c01e59 /app
parent65dd3e8e7e3234e5451bba9bf65bc12fdbf36425 (diff)
fix: fix double write header issue
Diffstat (limited to 'app')
-rw-r--r--app/controllers/dashboard/controller.go2
-rw-r--r--app/controllers/dashboard/controller_test.go26
2 files changed, 25 insertions, 3 deletions
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(), "<html")
})
})
+
+ t.Run("prevents double WriteHeader when template rendering fails", func(t *testing.T) {
+ ctx := cfg.CurrentUser.With(t.Context(), &domain.User{ID: domain.ID("1")})
+ request, response := test.RequestResponse("GET", "/dashboard", test.WithContext(ctx))
+
+ controller.Show(&FailingResponseWriter{T: t, ResponseRecorder: response}, request)
+ })
})
t.Run("GET /dashboard/nav", func(t *testing.T) {
@@ -68,5 +93,4 @@ func TestController(t *testing.T) {
})
})
})
-
}