Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a parser to get endpoints from htmx attributes #994

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 43 additions & 0 deletions pkg/engine/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var responseParsers = []responseParser{
{bodyParser, bodyMetaContentTagParser},
{bodyParser, bodyHtmlManifestTagParser},
{bodyParser, bodyHtmlDoctypeTagParser},
{bodyParser, bodyHtmxAttrParser},

// custom field regex parser
{bodyParser, customFieldRegexParser},
Expand Down Expand Up @@ -600,6 +601,48 @@ func bodyMetaContentTagParser(resp *navigation.Response) (navigationRequests []*
return
}

func bodyHtmxAttrParser(resp *navigation.Response) (navigationRequests []*navigation.Request) {
// exclude hx-delete
resp.Reader.Find("[hx-get],[hx-post],[hx-put],[hx-patch]").Each(func(i int, item *goquery.Selection) {
req := &navigation.Request{
RootHostname: resp.RootHostname,
Depth: resp.Depth,
Source: resp.Resp.Request.URL.String(),
Tag: "htmx",
}

hxGet, ok := item.Attr("hx-get")
if ok && hxGet != "" {
req.Method = "GET"
req.URL = resp.AbsoluteURL(hxGet)
req.Attribute = "hx-get"
navigationRequests = append(navigationRequests, req)
}
hxPost, ok := item.Attr(("hx-post"))
if ok && hxPost != "" {
req.Method = "POST"
req.URL = resp.AbsoluteURL(hxPost)
req.Attribute = "hx-post"
navigationRequests = append(navigationRequests, req)
}
hxPut, ok := item.Attr(("hx-put"))
if ok && hxPut != "" {
req.Method = "PUT"
req.URL = resp.AbsoluteURL(hxPut)
req.Attribute = "hx-put"
navigationRequests = append(navigationRequests, req)
}
hxPatch, ok := item.Attr(("hx-patch"))
if ok && hxPatch != "" {
req.Method = "PATCH"
req.URL = resp.AbsoluteURL(hxPatch)
req.Attribute = "hx-patch"
navigationRequests = append(navigationRequests, req)
}
})
return
}

// -------------------------------------------------------------------------
// Begin JS Regex based parsers
// -------------------------------------------------------------------------
Expand Down
34 changes: 34 additions & 0 deletions pkg/engine/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,37 @@ func TestRegexBodyParsers(t *testing.T) {
require.Equal(t, requireFields, navigationRequests[0].CustomFields, "could not get correct url")
})
}

func TestHtmxBodyParser(t *testing.T) {
parsed, _ := urlutil.Parse("https://htmx.org/examples/")

t.Run("hx-get", func(t *testing.T) {
documentReader, _ := goquery.NewDocumentFromReader(strings.NewReader(`<button hx-get="/contact/1/edit" class="btn primary">Click To Edit</button>`))
resp := &navigation.Response{Resp: &http.Response{Request: &http.Request{URL: parsed.URL}}, Reader: documentReader}
navigationRequests := bodyHtmxAttrParser(resp)
require.Equal(t, "https://htmx.org/contact/1/edit", navigationRequests[0].URL, "could not get correct url")
require.Equal(t, "GET", navigationRequests[0].Method, "could not get correct method")
})
t.Run("hx-post", func(t *testing.T) {
documentReader, _ := goquery.NewDocumentFromReader(strings.NewReader(`<form id="checked-contacts" hx-post="/users" hx-swap="outerHTML settle:3s" hx-target="#toast">`))
resp := &navigation.Response{Resp: &http.Response{Request: &http.Request{URL: parsed.URL}}, Reader: documentReader}
navigationRequests := bodyHtmxAttrParser(resp)
require.Equal(t, "https://htmx.org/users", navigationRequests[0].URL, "could not get correct url")
require.Equal(t, "POST", navigationRequests[0].Method, "could not get correct method")
})
t.Run("hx-put", func(t *testing.T) {
documentReader, _ := goquery.NewDocumentFromReader(strings.NewReader(`<button hx-put="/account" hx-target="body">`))
resp := &navigation.Response{Resp: &http.Response{Request: &http.Request{URL: parsed.URL}}, Reader: documentReader}
navigationRequests := bodyHtmxAttrParser(resp)
require.Equal(t, "https://htmx.org/account", navigationRequests[0].URL, "could not get correct url")
require.Equal(t, "PUT", navigationRequests[0].Method, "could not get correct method")

})
t.Run("hx-patch", func(t *testing.T) {
documentReader, _ := goquery.NewDocumentFromReader(strings.NewReader(`<button hx-patch="/account" hx-target="body">`))
resp := &navigation.Response{Resp: &http.Response{Request: &http.Request{URL: parsed.URL}}, Reader: documentReader}
navigationRequests := bodyHtmxAttrParser(resp)
require.Equal(t, "https://htmx.org/account", navigationRequests[0].URL, "could not get correct url")
require.Equal(t, "PATCH", navigationRequests[0].Method, "could not get correct method")
})
}
Loading