webmention.rocks test case 23: redirects and relative paths

This commit is contained in:
Wouter Groeneveld 2021-04-25 12:48:05 +02:00
parent 8779eb01ee
commit dd5dca5d98
3 changed files with 21 additions and 5 deletions

View File

@ -2,7 +2,6 @@ package send
import ( import (
"brainbaking.com/go-jamming/rest" "brainbaking.com/go-jamming/rest"
"fmt"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"net/url" "net/url"
"regexp" "regexp"
@ -28,11 +27,12 @@ func (sndr *Sender) discover(target string) (link string, mentionType string) {
return return
} }
link = header.Get(rest.RequestUrl) // default to a possible redirect of the target 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. // prefer links in the header over the html itself.
for _, possibleLink := range header.Values("link") { for _, possibleLink := range header.Values("link") {
if relWebmention.MatchString(possibleLink) { if relWebmention.MatchString(possibleLink) {
return buildWebmentionHeaderLink(possibleLink, rest.BaseUrlOf(link)), typeWebmention return buildWebmentionHeaderLink(possibleLink, baseUrl), typeWebmention
} }
} }
if header.Get("X-Pingback") != "" { 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 // this also complies with w3.org regulations: relative endpoint could be possible
baseUrl, _ := url.Parse(link)
format := microformats.Parse(strings.NewReader(body), baseUrl) format := microformats.Parse(strings.NewReader(body), baseUrl)
if len(format.Rels[typeWebmention]) > 0 { if len(format.Rels[typeWebmention]) > 0 {
mentionType = typeWebmention mentionType = typeWebmention
@ -71,8 +70,9 @@ func buildWebmentionHeaderLink(link string, baseUrl *url.URL) (wm string) {
} }
raw := strings.Split(link, ";")[0][1:] raw := strings.Split(link, ";")[0][1:]
wm = raw[:len(raw)-1] wm = raw[:len(raw)-1]
if strings.HasPrefix(wm, "/") { if !strings.HasPrefix(wm, "http") {
wm = fmt.Sprintf("%s%s", baseUrl, wm) abs, _ := baseUrl.Parse(wm)
wm = abs.String()
} }
return return

View File

@ -2,10 +2,24 @@ package send
import ( import (
"brainbaking.com/go-jamming/mocks" "brainbaking.com/go-jamming/mocks"
"brainbaking.com/go-jamming/rest"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"strings"
"testing" "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) { func TestDiscover(t *testing.T) {
var sender = &Sender{ var sender = &Sender{
RestClient: &mocks.RestClientMock{ RestClient: &mocks.RestClientMock{

View File

@ -39,6 +39,7 @@ func TestGetBodyFollowsRedirect(t *testing.T) {
w.WriteHeader(302) w.WriteHeader(302)
}) })
mux.HandleFunc("/2", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/2", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("link", `<webmention-endpoint/KyetGUioV2x1lJoiw96V>; rel=webmention`)
w.WriteHeader(200) w.WriteHeader(200)
w.Write([]byte("nice!")) w.Write([]byte("nice!"))
}) })
@ -52,6 +53,7 @@ func TestGetBodyFollowsRedirect(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "http://localhost:6666/2", headers.Get(RequestUrl)) assert.Equal(t, "http://localhost:6666/2", headers.Get(RequestUrl))
assert.Equal(t, `<webmention-endpoint/KyetGUioV2x1lJoiw96V>; rel=webmention`, headers.Get("link"))
assert.Equal(t, "nice!", body) assert.Equal(t, "nice!", body)
} }