webmention.rocks test case 23: redirects and relative paths
parent
8779eb01ee
commit
dd5dca5d98
|
@ -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
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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", `<webmention-endpoint/KyetGUioV2x1lJoiw96V>; 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, `<webmention-endpoint/KyetGUioV2x1lJoiw96V>; rel=webmention`, headers.Get("link"))
|
||||
assert.Equal(t, "nice!", body)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue