diff --git a/internal/util/http.go b/internal/util/http.go index df54aa9..eba5f75 100644 --- a/internal/util/http.go +++ b/internal/util/http.go @@ -100,15 +100,7 @@ func ExtractProperty(resp *http.Response, itemType, key string) []interface{} { Host: string(resp.Header.Peek(http.HeaderHost)), }) - for _, item := range data.Items { - if !contains(item.Type, itemType) { - continue - } - - return item.Properties[key] - } - - return nil + return findProperty(data.Items, itemType, key) } func contains(src []string, find string) bool { @@ -122,3 +114,20 @@ func contains(src []string, find string) bool { return false } + +func findProperty(src []*microformats.Microformat, itemType, key string) []interface{} { + for _, item := range src { + if contains(item.Type, itemType) { + return item.Properties[key] + } + + result := findProperty(item.Children, itemType, key) + if result == nil { + continue + } + + return result + } + + return nil +} diff --git a/internal/util/http_test.go b/internal/util/http_test.go new file mode 100644 index 0000000..d46763f --- /dev/null +++ b/internal/util/http_test.go @@ -0,0 +1,30 @@ +package util_test + +import ( + "testing" + + http "github.com/valyala/fasthttp" + + "source.toby3d.me/website/indieauth/internal/util" +) + +const testBody = ` + +
+

Sample Name

+
+ +` + +func TestExtractProperty(t *testing.T) { + t.Parallel() + + resp := http.AcquireResponse() + defer http.ReleaseResponse(resp) + resp.SetBodyString(testBody) + + results := util.ExtractProperty(resp, "h-card", "name") + if results == nil || results[0] != "Sample Name" { + t.Errorf(`ExtractProperty(resp, "h-card", "name") = %+s, want %+s`, results, []string{"Sample Name"}) + } +}