diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/dashboard/controller.go | 2 | ||||
| -rw-r--r-- | app/controllers/dashboard/controller_test.go | 26 |
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) { }) }) }) - } |
