|
| 1 | +package router_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/gin-gonic/gin" |
| 12 | + "github.com/labring/aiproxy/core/common/config" |
| 13 | + corerouter "github.com/labring/aiproxy/core/router" |
| 14 | + "github.com/smartystreets/goconvey/convey" |
| 15 | +) |
| 16 | + |
| 17 | +const testGitHubProjectURL = "https://github.com/labring/aiproxy" |
| 18 | + |
| 19 | +func TestSetStaticFileRouter_DisableWebRoot(t *testing.T) { |
| 20 | + convey.Convey("SetStaticFileRouter with DISABLE_WEB_ROOT", t, func() { |
| 21 | + webPath := writeTestWebFiles(t) |
| 22 | + router := newTestStaticRouter(t, webPath, false, true) |
| 23 | + |
| 24 | + convey.Convey("should redirect root path to github", func() { |
| 25 | + recorder := httptest.NewRecorder() |
| 26 | + req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil) |
| 27 | + |
| 28 | + router.ServeHTTP(recorder, req) |
| 29 | + |
| 30 | + convey.So(recorder.Code, convey.ShouldEqual, http.StatusOK) |
| 31 | + convey.So(recorder.Body.String(), convey.ShouldContainSubstring, testGitHubProjectURL) |
| 32 | + convey.So( |
| 33 | + recorder.Body.String(), |
| 34 | + convey.ShouldContainSubstring, |
| 35 | + "id=\"countdown\">15</div>", |
| 36 | + ) |
| 37 | + }) |
| 38 | + |
| 39 | + convey.Convey("should keep static assets accessible", func() { |
| 40 | + recorder := httptest.NewRecorder() |
| 41 | + req := httptest.NewRequestWithContext( |
| 42 | + context.Background(), |
| 43 | + http.MethodGet, |
| 44 | + "/assets/app.js", |
| 45 | + nil, |
| 46 | + ) |
| 47 | + |
| 48 | + router.ServeHTTP(recorder, req) |
| 49 | + |
| 50 | + convey.So(recorder.Code, convey.ShouldEqual, http.StatusOK) |
| 51 | + convey.So(recorder.Body.String(), convey.ShouldContainSubstring, "console.log('ok');") |
| 52 | + }) |
| 53 | + |
| 54 | + convey.Convey("should keep SPA fallback accessible", func() { |
| 55 | + recorder := httptest.NewRecorder() |
| 56 | + req := httptest.NewRequestWithContext( |
| 57 | + context.Background(), |
| 58 | + http.MethodGet, |
| 59 | + "/dashboard", |
| 60 | + nil, |
| 61 | + ) |
| 62 | + |
| 63 | + router.ServeHTTP(recorder, req) |
| 64 | + |
| 65 | + convey.So(recorder.Code, convey.ShouldEqual, http.StatusOK) |
| 66 | + convey.So(recorder.Body.String(), convey.ShouldContainSubstring, "test-spa") |
| 67 | + }) |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +func TestSetStaticFileRouter_DisableWeb(t *testing.T) { |
| 72 | + convey.Convey("SetStaticFileRouter with DISABLE_WEB", t, func() { |
| 73 | + webPath := writeTestWebFiles(t) |
| 74 | + router := newTestStaticRouter(t, webPath, true, false) |
| 75 | + |
| 76 | + convey.Convey("should serve the github redirect page on root", func() { |
| 77 | + recorder := httptest.NewRecorder() |
| 78 | + req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil) |
| 79 | + |
| 80 | + router.ServeHTTP(recorder, req) |
| 81 | + |
| 82 | + convey.So(recorder.Code, convey.ShouldEqual, http.StatusOK) |
| 83 | + convey.So(recorder.Body.String(), convey.ShouldContainSubstring, testGitHubProjectURL) |
| 84 | + }) |
| 85 | + |
| 86 | + convey.Convey("should not expose static assets", func() { |
| 87 | + recorder := httptest.NewRecorder() |
| 88 | + req := httptest.NewRequestWithContext( |
| 89 | + context.Background(), |
| 90 | + http.MethodGet, |
| 91 | + "/assets/app.js", |
| 92 | + nil, |
| 93 | + ) |
| 94 | + |
| 95 | + router.ServeHTTP(recorder, req) |
| 96 | + |
| 97 | + convey.So(recorder.Code, convey.ShouldEqual, http.StatusNotFound) |
| 98 | + }) |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +func newTestStaticRouter( |
| 103 | + t *testing.T, |
| 104 | + webPath string, |
| 105 | + disableWeb, disableWebRoot bool, |
| 106 | +) *gin.Engine { |
| 107 | + t.Helper() |
| 108 | + |
| 109 | + gin.SetMode(gin.TestMode) |
| 110 | + |
| 111 | + oldWebPath := config.WebPath |
| 112 | + oldDisableWeb := config.DisableWeb |
| 113 | + oldDisableWebRoot := config.DisableWebRoot |
| 114 | + |
| 115 | + t.Cleanup(func() { |
| 116 | + config.WebPath = oldWebPath |
| 117 | + config.DisableWeb = oldDisableWeb |
| 118 | + config.DisableWebRoot = oldDisableWebRoot |
| 119 | + }) |
| 120 | + |
| 121 | + config.WebPath = webPath |
| 122 | + config.DisableWeb = disableWeb |
| 123 | + config.DisableWebRoot = disableWebRoot |
| 124 | + |
| 125 | + router := gin.New() |
| 126 | + corerouter.SetStaticFileRouter(router) |
| 127 | + |
| 128 | + return router |
| 129 | +} |
| 130 | + |
| 131 | +func writeTestWebFiles(t *testing.T) string { |
| 132 | + t.Helper() |
| 133 | + |
| 134 | + webPath := t.TempDir() |
| 135 | + assetsPath := filepath.Join(webPath, "assets") |
| 136 | + |
| 137 | + if err := os.MkdirAll(assetsPath, 0o755); err != nil { |
| 138 | + t.Fatalf("mkdir assets: %v", err) |
| 139 | + } |
| 140 | + |
| 141 | + if err := os.WriteFile( |
| 142 | + filepath.Join(webPath, "index.html"), |
| 143 | + []byte("<!doctype html><html><body>test-spa</body></html>"), |
| 144 | + 0o600, |
| 145 | + ); err != nil { |
| 146 | + t.Fatalf("write index.html: %v", err) |
| 147 | + } |
| 148 | + |
| 149 | + if err := os.WriteFile( |
| 150 | + filepath.Join(assetsPath, "app.js"), |
| 151 | + []byte("console.log('ok');"), |
| 152 | + 0o600, |
| 153 | + ); err != nil { |
| 154 | + t.Fatalf("write app.js: %v", err) |
| 155 | + } |
| 156 | + |
| 157 | + return webPath |
| 158 | +} |
0 commit comments