code.gitea.io/gitea@v1.21.7/routers/common/errpage_test.go (about)

     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  	assert.Contains(t, respContent, `lang="en-US"`) // make sure the locale work
    31  
    32  	// the 500 page doesn't have normal pages footer, it makes it easier to distinguish a normal page and a failed page.
    33  	// especially when a sub-template causes page error, the HTTP response code is still 200,
    34  	// the different "footer" is the only way to know whether a page is fully rendered without error.
    35  	assert.False(t, test.IsNormalPageCompleted(respContent))
    36  }
    37  
    38  func TestMain(m *testing.M) {
    39  	unittest.MainTest(m, &unittest.TestOptions{
    40  		GiteaRootPath: filepath.Join("..", ".."),
    41  	})
    42  }