summaryrefslogtreecommitdiff
path: root/app/controllers/dashboard/controller_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers/dashboard/controller_test.go')
-rw-r--r--app/controllers/dashboard/controller_test.go26
1 files changed, 25 insertions, 1 deletions
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) {
})
})
})
-
}