check / check (push) In progress
Every 400, 403, 404 and 500 on an admin page now answers with an error page in the normal layout: the status, one fixed line explaining it, and a link back to the webhook list, or to sign-in when nobody is signed in. Unknown paths reach it through the router's not-found handler, a refused form token through the CSRF middleware, and a panic through a recoverer each admin page route group installs first. Status codes are unchanged, and the page always sends Cache-Control: no-store. If the error page fails to render, the answer is the same status in plain text; if it panics, the answer is a 500. The receiver, the healthcheck and /metrics keep their plain answers. Model: opus-5-5
54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
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())
|
|
}
|