github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/middleware/frontend_test.go (about)

     1  package middleware
     2  
     3  import (
     4  	"errors"
     5  	"github.com/cloudreve/Cloudreve/v3/bootstrap"
     6  	"github.com/cloudreve/Cloudreve/v3/pkg/cache"
     7  	"github.com/cloudreve/Cloudreve/v3/pkg/util"
     8  	"github.com/gin-gonic/gin"
     9  	"github.com/stretchr/testify/assert"
    10  	testMock "github.com/stretchr/testify/mock"
    11  	"net/http"
    12  	"net/http/httptest"
    13  	"os"
    14  	"testing"
    15  )
    16  
    17  type StaticMock struct {
    18  	testMock.Mock
    19  }
    20  
    21  func (m StaticMock) Open(name string) (http.File, error) {
    22  	args := m.Called(name)
    23  	return args.Get(0).(http.File), args.Error(1)
    24  }
    25  
    26  func (m StaticMock) Exists(prefix string, filepath string) bool {
    27  	args := m.Called(prefix, filepath)
    28  	return args.Bool(0)
    29  }
    30  
    31  func TestFrontendFileHandler(t *testing.T) {
    32  	asserts := assert.New(t)
    33  	rec := httptest.NewRecorder()
    34  
    35  	// 静态资源未加载
    36  	{
    37  		TestFunc := FrontendFileHandler()
    38  
    39  		c, _ := gin.CreateTestContext(rec)
    40  		c.Params = []gin.Param{}
    41  		c.Request, _ = http.NewRequest("GET", "/", nil)
    42  		TestFunc(c)
    43  		asserts.False(c.IsAborted())
    44  	}
    45  
    46  	// index.html 不存在
    47  	{
    48  		testStatic := &StaticMock{}
    49  		bootstrap.StaticFS = testStatic
    50  		testStatic.On("Open", "/index.html").
    51  			Return(&os.File{}, errors.New("error"))
    52  		TestFunc := FrontendFileHandler()
    53  
    54  		c, _ := gin.CreateTestContext(rec)
    55  		c.Params = []gin.Param{}
    56  		c.Request, _ = http.NewRequest("GET", "/", nil)
    57  		TestFunc(c)
    58  		asserts.False(c.IsAborted())
    59  	}
    60  
    61  	// index.html 读取失败
    62  	{
    63  		file, _ := util.CreatNestedFile("tests/index.html")
    64  		file.Close()
    65  		testStatic := &StaticMock{}
    66  		bootstrap.StaticFS = testStatic
    67  		testStatic.On("Open", "/index.html").
    68  			Return(file, nil)
    69  		TestFunc := FrontendFileHandler()
    70  
    71  		c, _ := gin.CreateTestContext(rec)
    72  		c.Params = []gin.Param{}
    73  		c.Request, _ = http.NewRequest("GET", "/", nil)
    74  		TestFunc(c)
    75  		asserts.False(c.IsAborted())
    76  	}
    77  
    78  	// 成功且命中
    79  	{
    80  		file, _ := util.CreatNestedFile("tests/index.html")
    81  		defer file.Close()
    82  		testStatic := &StaticMock{}
    83  		bootstrap.StaticFS = testStatic
    84  		testStatic.On("Open", "/index.html").
    85  			Return(file, nil)
    86  		TestFunc := FrontendFileHandler()
    87  
    88  		c, _ := gin.CreateTestContext(rec)
    89  		c.Params = []gin.Param{}
    90  		c.Request, _ = http.NewRequest("GET", "/", nil)
    91  
    92  		cache.Set("setting_siteName", "cloudreve", 0)
    93  		cache.Set("setting_siteKeywords", "cloudreve", 0)
    94  		cache.Set("setting_siteScript", "cloudreve", 0)
    95  		cache.Set("setting_pwa_small_icon", "cloudreve", 0)
    96  
    97  		TestFunc(c)
    98  		asserts.True(c.IsAborted())
    99  	}
   100  
   101  	// 成功且命中静态文件
   102  	{
   103  		file, _ := util.CreatNestedFile("tests/index.html")
   104  		defer file.Close()
   105  		testStatic := &StaticMock{}
   106  		bootstrap.StaticFS = testStatic
   107  		testStatic.On("Open", "/index.html").
   108  			Return(file, nil)
   109  		testStatic.On("Exists", "/", "/2").
   110  			Return(true)
   111  		testStatic.On("Open", "/2").
   112  			Return(file, nil)
   113  		TestFunc := FrontendFileHandler()
   114  
   115  		c, _ := gin.CreateTestContext(rec)
   116  		c.Params = []gin.Param{}
   117  		c.Request, _ = http.NewRequest("GET", "/2", nil)
   118  
   119  		TestFunc(c)
   120  		asserts.True(c.IsAborted())
   121  		testStatic.AssertExpectations(t)
   122  	}
   123  
   124  	// API 相关跳过
   125  	{
   126  		for _, reqPath := range []string{"/api/user", "/manifest.json", "/dav/path"} {
   127  			file, _ := util.CreatNestedFile("tests/index.html")
   128  			defer file.Close()
   129  			testStatic := &StaticMock{}
   130  			bootstrap.StaticFS = testStatic
   131  			testStatic.On("Open", "/index.html").
   132  				Return(file, nil)
   133  			TestFunc := FrontendFileHandler()
   134  
   135  			c, _ := gin.CreateTestContext(rec)
   136  			c.Params = []gin.Param{}
   137  			c.Request, _ = http.NewRequest("GET", reqPath, nil)
   138  
   139  			TestFunc(c)
   140  			asserts.False(c.IsAborted())
   141  		}
   142  	}
   143  
   144  }