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