From ccd75fbc720e102822e8a4b1eb36f63e06c8a720 Mon Sep 17 00:00:00 2001 From: Alex Suraci Date: Thu, 10 Oct 2024 02:25:35 -0400 Subject: [PATCH] fix asset paths/URLs becoming lowercased (#988) Signed-off-by: Alex Suraci --- pkg/app/http.go | 12 ++++++------ pkg/app/http_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/pkg/app/http.go b/pkg/app/http.go index f45dd3a6..900a581c 100644 --- a/pkg/app/http.go +++ b/pkg/app/http.go @@ -854,19 +854,19 @@ func parseHTTPResource(v string) httpResource { if elem = strings.TrimSpace(elem); elem == "" { continue } - elem = strings.ToLower(elem) + lower := strings.ToLower(elem) switch { - case elem == "crossorigin": + case lower == "crossorigin": res.CrossOrigin = "true" - case strings.HasPrefix(elem, "crossorigin="): - res.CrossOrigin = strings.TrimPrefix(elem, "crossorigin=") + case strings.HasPrefix(lower, "crossorigin="): + res.CrossOrigin = strings.TrimPrefix(lower, "crossorigin=") - case elem == "defer": + case lower == "defer": res.LoadingMode = "defer" - case elem == "async": + case lower == "async": res.LoadingMode = "async" default: diff --git a/pkg/app/http_test.go b/pkg/app/http_test.go index 796e76bc..d6132452 100644 --- a/pkg/app/http_test.go +++ b/pkg/app/http_test.go @@ -72,6 +72,49 @@ func TestHandlerServePageWithLocalDir(t *testing.T) { t.Log(body) } +func TestHandlerPreservesURLCasing(t *testing.T) { + r := httptest.NewRequest(http.MethodGet, "/", nil) + w := httptest.NewRecorder() + + h := Handler{ + Resources: LocalDir(""), + Title: "Handler testing", + Scripts: []string{ + "web/Hello.js", + "http://boo.com/Bar.js", + }, + Styles: []string{ + "web/Foo.css", + "/web/Bar.css", + "https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded", + }, + RawHeaders: []string{ + ``, + }, + Image: "/web/test.png", + } + h.Icon.Maskable = "ios.png" + + h.ServeHTTP(w, r) + + body := w.Body.String() + require.Equal(t, http.StatusOK, w.Code) + require.Contains(t, body, ``) + require.Contains(t, body, `href="/web/Foo.css"`) + require.Contains(t, body, `href="/web/Bar.css"`) + require.Contains(t, body, `href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded"`) + require.Contains(t, body, `src="/web/Hello.js"`) + require.Contains(t, body, `src="http://boo.com/Bar.js"`) + require.Contains(t, body, `href="/manifest.webmanifest"`) + require.Contains(t, body, `href="/app.css"`) + require.Contains(t, body, ``) + require.Contains(t, body, `
`) + require.Contains(t, body, `content="https:///web/test.png"`) + require.Contains(t, body, ``) + + t.Log(body) +} + func TestHandlerServePageWithRemoteBucket(t *testing.T) { r := httptest.NewRequest(http.MethodGet, "/", nil) w := httptest.NewRecorder()