diff --git a/cmd/build/cmd_pwa.go b/cmd/build/cmd_pwa.go index 1c3cd8c..d2fb365 100644 --- a/cmd/build/cmd_pwa.go +++ b/cmd/build/cmd_pwa.go @@ -11,6 +11,7 @@ import ( "io" "net/http" "net/url" + "strings" "dappco.re/go/core" "dappco.re/go/core/build/internal/ax" @@ -118,7 +119,7 @@ func findManifestURL(htmlContent, baseURL string) (string, error) { href = a.Val } } - if rel == "manifest" && href != "" { + if relIncludesManifest(rel) && href != "" { manifestPath = href return } @@ -146,6 +147,17 @@ func findManifestURL(htmlContent, baseURL string) (string, error) { return manifestURL.String(), nil } +// relIncludesManifest reports whether a rel attribute declares a manifest link. +// HTML allows multiple space-separated tokens and case-insensitive values. +func relIncludesManifest(rel string) bool { + for _, token := range strings.Fields(rel) { + if strings.EqualFold(token, "manifest") { + return true + } + } + return false +} + // fetchManifest downloads and parses a PWA manifest. func fetchManifest(ctx context.Context, manifestURL string) (map[string]any, error) { resp, err := getWithContext(ctx, manifestURL) diff --git a/cmd/build/cmd_pwa_test.go b/cmd/build/cmd_pwa_test.go new file mode 100644 index 0000000..9c8c2ca --- /dev/null +++ b/cmd/build/cmd_pwa_test.go @@ -0,0 +1,37 @@ +package buildcmd + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestPwa_FindManifestURL_Good(t *testing.T) { + t.Run("accepts a standard manifest link", func(t *testing.T) { + htmlContent := `` + + got, err := findManifestURL(htmlContent, "https://example.test/app/") + require.NoError(t, err) + assert.Equal(t, "https://example.test/manifest.json", got) + }) + + t.Run("accepts case-insensitive tokenised rel values", func(t *testing.T) { + htmlContent := `` + + got, err := findManifestURL(htmlContent, "https://example.test/app/") + require.NoError(t, err) + assert.Equal(t, "https://example.test/app/manifest.json", got) + }) +} + +func TestPwa_FindManifestURL_Bad(t *testing.T) { + t.Run("returns an error when no manifest link exists", func(t *testing.T) { + htmlContent := `` + + got, err := findManifestURL(htmlContent, "https://example.test/app/") + assert.Error(t, err) + assert.Empty(t, got) + assert.Contains(t, err.Error(), "pwa.findManifestURL") + }) +}