gitea源码

errpage_test.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package common
  4. import (
  5. "errors"
  6. "net/http"
  7. "net/http/httptest"
  8. "net/url"
  9. "testing"
  10. "code.gitea.io/gitea/models/unittest"
  11. "code.gitea.io/gitea/modules/reqctx"
  12. "code.gitea.io/gitea/modules/test"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestRenderPanicErrorPage(t *testing.T) {
  16. w := httptest.NewRecorder()
  17. req := &http.Request{URL: &url.URL{}}
  18. req = req.WithContext(reqctx.NewRequestContextForTest(t.Context()))
  19. RenderPanicErrorPage(w, req, errors.New("fake panic error (for test only)"))
  20. respContent := w.Body.String()
  21. assert.Contains(t, respContent, `class="page-content status-page-500"`)
  22. assert.Contains(t, respContent, `</html>`)
  23. assert.Contains(t, respContent, `lang="en-US"`) // make sure the locale work
  24. // the 500 page doesn't have normal pages footer, it makes it easier to distinguish a normal page and a failed page.
  25. // especially when a sub-template causes page error, the HTTP response code is still 200,
  26. // the different "footer" is the only way to know whether a page is fully rendered without error.
  27. assert.False(t, test.IsNormalPageCompleted(respContent))
  28. }
  29. func TestMain(m *testing.M) {
  30. unittest.MainTest(m)
  31. }