Skip to content

Commit

Permalink
parseHTTPResource tweak (#997)
Browse files Browse the repository at this point in the history
* parseHTTPResource tweak

* rename variable
  • Loading branch information
maxence-charriere authored Oct 10, 2024
1 parent ccd75fb commit 25ff169
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 56 deletions.
23 changes: 10 additions & 13 deletions pkg/app/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,27 +850,24 @@ func (r httpResource) toScript() HTMLScript {

func parseHTTPResource(v string) httpResource {
var res httpResource
for _, elem := range strings.Split(v, " ") {
if elem = strings.TrimSpace(elem); elem == "" {
for _, part := range strings.Split(v, " ") {
if part = strings.TrimSpace(part); part == "" {
continue
}
lower := strings.ToLower(elem)

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

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

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

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

default:
res.URL = elem
res.URL = part
}
}
return res
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 25ff169

Please sign in to comment.