diff --git a/app/webmention/send/discoverer.go b/app/webmention/send/discoverer.go index 94d900f..06de32a 100644 --- a/app/webmention/send/discoverer.go +++ b/app/webmention/send/discoverer.go @@ -2,7 +2,6 @@ package send import ( "brainbaking.com/go-jamming/rest" - "fmt" "github.com/rs/zerolog/log" "net/url" "regexp" @@ -28,11 +27,12 @@ func (sndr *Sender) discover(target string) (link string, mentionType string) { return } link = header.Get(rest.RequestUrl) // default to a possible redirect of the target + baseUrl, _ := url.Parse(link) // prefer links in the header over the html itself. for _, possibleLink := range header.Values("link") { if relWebmention.MatchString(possibleLink) { - return buildWebmentionHeaderLink(possibleLink, rest.BaseUrlOf(link)), typeWebmention + return buildWebmentionHeaderLink(possibleLink, baseUrl), typeWebmention } } if header.Get("X-Pingback") != "" { @@ -40,7 +40,6 @@ func (sndr *Sender) discover(target string) (link string, mentionType string) { } // this also complies with w3.org regulations: relative endpoint could be possible - baseUrl, _ := url.Parse(link) format := microformats.Parse(strings.NewReader(body), baseUrl) if len(format.Rels[typeWebmention]) > 0 { mentionType = typeWebmention @@ -71,8 +70,9 @@ func buildWebmentionHeaderLink(link string, baseUrl *url.URL) (wm string) { } raw := strings.Split(link, ";")[0][1:] wm = raw[:len(raw)-1] - if strings.HasPrefix(wm, "/") { - wm = fmt.Sprintf("%s%s", baseUrl, wm) + if !strings.HasPrefix(wm, "http") { + abs, _ := baseUrl.Parse(wm) + wm = abs.String() } return diff --git a/app/webmention/send/discoverer_test.go b/app/webmention/send/discoverer_test.go index baaaf75..9b9987f 100644 --- a/app/webmention/send/discoverer_test.go +++ b/app/webmention/send/discoverer_test.go @@ -2,10 +2,24 @@ package send import ( "brainbaking.com/go-jamming/mocks" + "brainbaking.com/go-jamming/rest" "github.com/stretchr/testify/assert" + "strings" "testing" ) +func TestDiscoverE2EWithRedirect(t *testing.T) { + t.Skip("Skipping TestDiscoverE2EWithRedirect, webmention.rocks is slow.") + var sender = &Sender{ + RestClient: &rest.HttpClient{}, + } + + link, wmType := sender.discover("https://webmention.rocks/test/23/page") + assert.Equal(t, typeWebmention, wmType) + expectedUrl := "https://webmention.rocks/test/23/page/webmention-endpoint/" + assert.Truef(t, strings.HasPrefix(link, expectedUrl), "should start with %s, but was %s", expectedUrl, link) +} + func TestDiscover(t *testing.T) { var sender = &Sender{ RestClient: &mocks.RestClientMock{ diff --git a/rest/client_test.go b/rest/client_test.go index 3f9576a..313b3ae 100644 --- a/rest/client_test.go +++ b/rest/client_test.go @@ -39,6 +39,7 @@ func TestGetBodyFollowsRedirect(t *testing.T) { w.WriteHeader(302) }) mux.HandleFunc("/2", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("link", `; rel=webmention`) w.WriteHeader(200) w.Write([]byte("nice!")) }) @@ -52,6 +53,7 @@ func TestGetBodyFollowsRedirect(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "http://localhost:6666/2", headers.Get(RequestUrl)) + assert.Equal(t, `; rel=webmention`, headers.Get("link")) assert.Equal(t, "nice!", body) }