diff --git a/internal/server/routes_static.go b/internal/server/routes_static.go index 132044d61..feaeb68ee 100644 --- a/internal/server/routes_static.go +++ b/internal/server/routes_static.go @@ -13,9 +13,10 @@ import ( // registerStaticRoutes configures serving static assets and templates. func registerStaticRoutes(router *gin.Engine, conf *config.Config) { // Redirects to the PWA for now, can be replaced by a template later. - router.GET(conf.BaseUri("/"), func(c *gin.Context) { + login := func(c *gin.Context) { c.Redirect(http.StatusTemporaryRedirect, conf.LoginUri()) - }) + } + router.Any(conf.BaseUri("/"), login) // Shows "Page Not found" error if no other handler is registered. router.NoRoute(func(c *gin.Context) { @@ -34,31 +35,34 @@ func registerStaticRoutes(router *gin.Engine, conf *config.Config) { }) // Loads Progressive Web App (PWA) on all routes beginning with "library". - router.GET(conf.BaseUri("/library/*path"), func(c *gin.Context) { + pwa := func(c *gin.Context) { values := gin.H{ "signUp": gin.H{"message": config.MsgSponsor, "url": config.SignUpURL}, "config": conf.ClientPublic(), } c.HTML(http.StatusOK, conf.TemplateName(), values) - }) + } + router.Any(conf.BaseUri("/library/*path"), pwa) // Progressive Web App (PWA) Manifest. - router.GET(conf.BaseUri("/manifest.json"), func(c *gin.Context) { + manifest := func(c *gin.Context) { c.Header("Cache-Control", "no-store") c.Header("Content-Type", "application/json") clientConfig := conf.ClientPublic() c.HTML(http.StatusOK, "manifest.json", gin.H{"config": clientConfig}) - }) + } + router.Any(conf.BaseUri("/manifest.json"), manifest) // Progressive Web App (PWA) Service Worker. swWorker := func(c *gin.Context) { c.Header("Cache-Control", "no-store") c.File(filepath.Join(conf.BuildPath(), "sw.js")) } - router.GET("/sw.js", swWorker) + router.Any("/sw.js", swWorker) + if swUri := conf.BaseUri("/sw.js"); swUri != "/sw.js" { - router.GET(swUri, swWorker) + router.Any(swUri, swWorker) } // Serves static favicon. diff --git a/internal/server/routes_test.go b/internal/server/routes_test.go new file mode 100644 index 000000000..ea2c25022 --- /dev/null +++ b/internal/server/routes_test.go @@ -0,0 +1,81 @@ +package server + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" + "github.com/stretchr/testify/assert" + + "github.com/photoprism/photoprism/internal/config" +) + +func TestStaticRoutes(t *testing.T) { + // Create router. + r := gin.Default() + + // Get test config. + conf := config.TestConfig() + + // Find and load templates. + r.LoadHTMLFiles(conf.TemplateFiles()...) + + // Register routes. + registerStaticRoutes(r, conf) + + t.Run("GetHome", func(t *testing.T) { + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/", nil) + r.ServeHTTP(w, req) + assert.Equal(t, 307, w.Code) + assert.Equal(t, "Temporary Redirect.\n\n", w.Body.String()) + }) + t.Run("HeadHome", func(t *testing.T) { + w := httptest.NewRecorder() + req, _ := http.NewRequest("HEAD", "/", nil) + r.ServeHTTP(w, req) + assert.Equal(t, 307, w.Code) + }) + t.Run("GetServiceWorker", func(t *testing.T) { + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/sw.js", nil) + r.ServeHTTP(w, req) + assert.Equal(t, 200, w.Code) + assert.NotEmpty(t, w.Body) + }) + t.Run("HeadServiceWorker", func(t *testing.T) { + w := httptest.NewRecorder() + req, _ := http.NewRequest("HEAD", "/sw.js", nil) + r.ServeHTTP(w, req) + assert.Equal(t, 200, w.Code) + assert.Empty(t, w.Body) + }) + t.Run("GetLibrary", func(t *testing.T) { + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/library/", nil) + r.ServeHTTP(w, req) + assert.Equal(t, 200, w.Code) + assert.NotEmpty(t, w.Body) + }) + t.Run("GetLibrary", func(t *testing.T) { + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/library/", nil) + r.ServeHTTP(w, req) + assert.Equal(t, 200, w.Code) + assert.NotEmpty(t, w.Body) + }) + t.Run("GetLibraryBrowse", func(t *testing.T) { + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/library/browse", nil) + r.ServeHTTP(w, req) + assert.Equal(t, 200, w.Code) + assert.NotEmpty(t, w.Body) + }) + t.Run("HeadLibraryBrowse", func(t *testing.T) { + w := httptest.NewRecorder() + req, _ := http.NewRequest("HEAD", "/library/browse", nil) + r.ServeHTTP(w, req) + assert.Equal(t, 200, w.Code) + }) +}