Skip to content

Commit

Permalink
parseHTTPResource tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
maxence-charriere committed Oct 10, 2024
1 parent ccd75fb commit 713211b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 53 deletions.
17 changes: 7 additions & 10 deletions pkg/app/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,20 +854,17 @@ func parseHTTPResource(v string) httpResource {
if elem = strings.TrimSpace(elem); elem == "" {
continue
}
lower := strings.ToLower(elem)

switch {
case lower == "crossorigin":
switch e := strings.ToLower(elem); {
case e == "crossorigin":
res.CrossOrigin = "true"

case strings.HasPrefix(lower, "crossorigin="):
res.CrossOrigin = strings.TrimPrefix(lower, "crossorigin=")
case strings.HasPrefix(e, "crossorigin="):
res.CrossOrigin = strings.TrimPrefix(e, "crossorigin=")

case lower == "defer":
res.LoadingMode = "defer"

case lower == "async":
res.LoadingMode = "async"
case e == "defer",
e == "async":
res.LoadingMode = e

default:
res.URL = elem
Expand Down
52 changes: 9 additions & 43 deletions pkg/app/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,49 +72,6 @@ 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{
`<meta http-equiv="refresh" content="30">`,
},
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, `<html lang="en">`)
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, `<meta http-equiv="refresh" content="30">`)
require.Contains(t, body, `<div id="pre-render-ok">`)
require.Contains(t, body, `content="https:///web/test.png"`)
require.Contains(t, body, `<img src="/web/resolve-static-resource-test.jpg">`)

t.Log(body)
}

func TestHandlerServePageWithRemoteBucket(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
Expand Down Expand Up @@ -818,6 +775,15 @@ func TestParseHTTPResource(t *testing.T) {
LoadingMode: "defer",
},
},
{
scenario: "url is case sensitive",
in: "https://Hello.World defer crossorigin",
resource: httpResource{
URL: "https://Hello.World",
CrossOrigin: "true",
LoadingMode: "defer",
},
},
}

for _, u := range utests {
Expand Down

0 comments on commit 713211b

Please sign in to comment.