Exercise error pages as browser navigations
3 files changed,
+21
−3
internal/slidestest/harness_test.go+14 −0internal/slidestest/index_test.go+6 −2internal/slidestest/present_test.go+1 −1
CI passed — run details
diff --git a/internal/slidestest/harness_test.go b/internal/slidestest/harness_test.go| index 4b8ea15..01841e4 100644 |
| --- a/internal/slidestest/harness_test.go |
| +++ b/internal/slidestest/harness_test.go |
| @@ -179,6 +179,20 @@ func get(t *testing.T, h http.Handler, target string, cookies ...*http.Cookie) * |
| return rec |
| } |
| +// getHTML is a browser navigation. Error pages deliberately distinguish this |
| +// from a client fetch without Accept: text/html. |
| +func getHTML(t *testing.T, h http.Handler, target string, cookies ...*http.Cookie) *httptest.ResponseRecorder { |
| + t.Helper() |
| + req := httptest.NewRequest(http.MethodGet, target, nil) |
| + req.Header.Set("Accept", "text/html") |
| + for _, c := range cookies { |
| + req.AddCookie(c) |
| + } |
| + rec := httptest.NewRecorder() |
| + h.ServeHTTP(rec, req) |
| + return rec |
| +} |
| + |
| func post(t *testing.T, h http.Handler, target string, form url.Values, cookies ...*http.Cookie) *httptest.ResponseRecorder { |
| t.Helper() |
| req := httptest.NewRequest(http.MethodPost, target, strings.NewReader(form.Encode())) |
diff --git a/internal/slidestest/index_test.go b/internal/slidestest/index_test.go| index bc0dda2..0ce1a20 100644 |
| --- a/internal/slidestest/index_test.go |
| +++ b/internal/slidestest/index_test.go |
| @@ -66,12 +66,16 @@ func TestStaticAssetsAreReachableSignedOut(t *testing.T) { |
| // a stray net/http default. |
| func TestErrorPageRendersFrameworkPartial(t *testing.T) { |
| rec := httptest.NewRecorder() |
| - slides.ErrorPage(rec, httptest.NewRequest(http.MethodGet, "/", nil), http.StatusInternalServerError, "abc123") |
| + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| + req.Header.Set("Accept", "text/html") |
| + localised(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + slides.ErrorPage(w, r, http.StatusInternalServerError, "abc123") |
| + })).ServeHTTP(rec, req) |
| if rec.Code != http.StatusInternalServerError { |
| t.Fatalf("ErrorPage: status %d, want %d", rec.Code, http.StatusInternalServerError) |
| } |
| body := rec.Body.String() |
| - if !strings.Contains(body, "Something went wrong on our side") { |
| + if !strings.Contains(body, "Something went wrong") { |
| t.Errorf("ErrorPage body missing the 500 title:\n%s", body) |
| } |
| if !strings.Contains(body, "abc123") { |
diff --git a/internal/slidestest/present_test.go b/internal/slidestest/present_test.go| index 121fb0b..1ce2eab 100644 |
| --- a/internal/slidestest/present_test.go |
| +++ b/internal/slidestest/present_test.go |
| @@ -358,7 +358,7 @@ func TestEndedSessionIs410(t *testing.T) { |
| "/p/" + session + "/notes?t=" + token, |
| "/p/" + session + "/remote?t=" + token, |
| } { |
| - rec := get(t, a, target, cookies...) |
| + rec := getHTML(t, a, target, cookies...) |
| if rec.Code != http.StatusGone { |
| t.Errorf("GET %s after the session ended: status %d, want 410", target, rec.Code) |
| } |