package handlers_test import ( "context" "html/template" "net/http" "net/http/httptest" "testing" "github.com/stretchr/testify/assert" "sneak.berlin/go/webhooker/internal/handlers" ) // TestErrorPage_RenderFailureKeepsStatus proves that an error page // which cannot render answers with the status it was reporting, as // plain text, and is not attempted again: a page whose own render // fails reaches the error page, and the error page failing as well // ends there with the 500. func TestErrorPage_RenderFailureKeepsStatus(t *testing.T) { t.Parallel() var h *handlers.Handlers app := newTestApp(t, &h) app.RequireStart() t.Cleanup(app.RequireStop) // .Status is an int, so asking it for a field fails the render. failing := `{{.Status.Missing}}` h.AddTemplateForTest("error.html", template.Must( template.New("error").Parse(failing), )) h.AddTemplateForTest("failing.html", template.Must( template.New("failing").Parse(`{{.Data.Missing}}`), )) req := httptest.NewRequestWithContext( context.Background(), http.MethodGet, "/", nil, ) w := httptest.NewRecorder() h.HandleErrorPage(http.StatusNotFound).ServeHTTP(w, req) assert.Equal(t, http.StatusNotFound, w.Code) assert.Equal(t, "Not Found\n", w.Body.String()) w = httptest.NewRecorder() h.RenderTemplateForTest(w, req, "failing.html", 0) assert.Equal(t, http.StatusInternalServerError, w.Code) assert.Equal(t, "Internal Server Error\n", w.Body.String()) }