Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions internal/router/router_static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,68 @@ import (
"github.com/stretchr/testify/require"
)

func TestFrontendStaticServesEmbedEntryForEmbedRoute(t *testing.T) {
gin.SetMode(gin.TestMode)
webDir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(webDir, "index.html"), []byte("main spa"), 0o600))
require.NoError(t, os.WriteFile(filepath.Join(webDir, "embed.html"), []byte("embed spa"), 0o600))
t.Setenv("WEKNORA_WEB_DIR", webDir)

r := gin.New()
serveFrontendStatic(r)

for _, method := range []string{http.MethodGet, http.MethodHead} {
t.Run(method, func(t *testing.T) {
recorder := httptest.NewRecorder()
request := httptest.NewRequest(method, "/embed/channel-123?locale=zh-CN", nil)
r.ServeHTTP(recorder, request)

require.Equal(t, http.StatusOK, recorder.Code)
require.Equal(t, "no-cache, must-revalidate", recorder.Header().Get("Cache-Control"))
if method == http.MethodGet {
require.Equal(t, "embed spa", recorder.Body.String())
} else {
require.Empty(t, recorder.Body.String())
}
})
}
}

func TestFrontendStaticKeepsMainSPAFallbackForNonEmbedRoute(t *testing.T) {
gin.SetMode(gin.TestMode)
webDir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(webDir, "index.html"), []byte("main spa"), 0o600))
require.NoError(t, os.WriteFile(filepath.Join(webDir, "embed.html"), []byte("embed spa"), 0o600))
t.Setenv("WEKNORA_WEB_DIR", webDir)

r := gin.New()
serveFrontendStatic(r)

recorder := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodGet, "/settings/profile", nil)
r.ServeHTTP(recorder, request)

require.Equal(t, http.StatusOK, recorder.Code)
require.Equal(t, "main spa", recorder.Body.String())
}

func TestFrontendStaticReturnsNotFoundWhenEmbedEntryIsMissing(t *testing.T) {
gin.SetMode(gin.TestMode)
webDir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(webDir, "index.html"), []byte("main spa"), 0o600))
t.Setenv("WEKNORA_WEB_DIR", webDir)

r := gin.New()
serveFrontendStatic(r)

recorder := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodGet, "/embed/channel-123", nil)
r.ServeHTTP(recorder, request)

require.Equal(t, http.StatusNotFound, recorder.Code)
require.NotContains(t, recorder.Body.String(), "main spa")
}

func TestFrontendStaticDoesNotInterceptResourceGrant(t *testing.T) {
gin.SetMode(gin.TestMode)
webDir := t.TempDir()
Expand Down
17 changes: 17 additions & 0 deletions internal/router/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func serveFrontendStatic(r *gin.Engine) {
if _, err := os.Stat(indexPath); err != nil {
return
}
embedIndexPath := filepath.Join(absDir, "embed.html")
embedIndexInfo, embedIndexErr := os.Stat(embedIndexPath)
hasEmbedIndex := embedIndexErr == nil && !embedIndexInfo.IsDir()
if !hasEmbedIndex {
logger.Warnf(context.Background(), "[Router] Embed entry %s is unavailable; /embed/* requests will return 404", embedIndexPath)
}

logger.Infof(context.Background(), "[Router] Serving frontend static files from %s", absDir)

Expand All @@ -49,6 +55,17 @@ func serveFrontendStatic(r *gin.Engine) {
c.Abort()
return
}
if strings.HasPrefix(path, "/embed/") {
if !hasEmbedIndex {
c.Status(http.StatusNotFound)
c.Abort()
return
}
setFrontendCacheHeaders(c.Writer, "/embed.html")
c.File(embedIndexPath)
c.Abort()
return
}
setFrontendCacheHeaders(c.Writer, "/index.html")
c.File(indexPath)
c.Abort()
Expand Down