go-jamming/app/webmention/send/discoverer_test.go

135 lines
4.3 KiB
Go

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{
GetBodyFunc: mocks.RelPathGetBodyFunc("../../../mocks/"),
},
}
cases := []struct {
label string
url string
expectedLink string
expectedType string
}{
{
"discover 'unknown' if no link is present",
"https://brainbaking.com/link-discover-test-none.html",
"https://brainbaking.com/link-discover-test-none.html",
typeUnknown,
},
{
"prefer webmentions over pingbacks if both links are present",
"https://brainbaking.com/link-discover-bothtypes.html",
"http://aaronpk.example/webmention-endpoint",
typeWebmention,
},
{
"pingbacks: discover link if present in header",
"https://brainbaking.com/pingback-discover-test.html",
"http://aaronpk.example/pingback-endpoint",
typePingback,
},
{
"pingbacks: discover link if sole entry somewhere in html",
"https://brainbaking.com/pingback-discover-test-single.html",
"http://aaronpk.example/pingback-endpoint-body",
typePingback,
},
{
"pingbacks: use link in header if multiple present in html",
"https://brainbaking.com/pingback-discover-test-multiple.html",
"http://aaronpk.example/pingback-endpoint-header",
typePingback,
},
{
"webmentions: discover link if present in header",
"https://brainbaking.com/link-discover-test.html",
"http://aaronpk.example/webmention-endpoint",
typeWebmention,
},
{
"webmentions: https://webmention.rocks/test/1 relative path in header",
"https://brainbaking.com/webmention-rocks-1.html",
"https://brainbaking.com/test/1/webmention?head=true",
typeWebmention,
},
{
"webmentions: https://webmention.rocks/test/11 prefer links in the header even if in head or body also present",
"https://brainbaking.com/webmention-rocks-11.html",
"https://brainbaking.com/test/11/webmention",
typeWebmention,
},
{
"webmentions: https://webmention.rocks/test/15 empty link rel means it is its own endpoint",
"https://brainbaking.com/webmention-rocks-15.html",
"https://brainbaking.com/webmention-rocks-15.html",
typeWebmention,
},
{
"webmentions: https://webmention.rocks/test/18 discover link if multiple present in multiple headers",
"https://brainbaking.com/webmention-rocks-18.html",
"https://webmention.rocks/test/18/webmention?head=true",
typeWebmention,
},
{
"webmentions: https://webmention.rocks/test/19 discover link if multiple present comma-separated in single header",
"https://brainbaking.com/webmention-rocks-19.html",
"https://webmention.rocks/test/19/webmention?head=true",
typeWebmention,
},
{
"webmentions: https://webmention.rocks/test/20 discover link in body href if header is empty",
"https://brainbaking.com/webmention-rocks-20.html",
"https://brainbaking.com/test/20/webmention",
typeWebmention,
},
{
"webmentions: https://webmention.rocks/test/22 discover link relative to the page instead of the domain",
"https://brainbaking.com/blank/webmention-rocks-22.html",
"https://brainbaking.com/blank/22/webmention",
typeWebmention,
},
{
"webmentions: discover link if sole entry somewhere in html",
"https://brainbaking.com/link-discover-test-single.html",
"http://aaronpk.example/webmention-endpoint-body",
typeWebmention,
},
{
"webmentions: use link in header if multiple present in html",
"https://brainbaking.com/link-discover-test-multiple.html",
"http://aaronpk.example/webmention-endpoint-header",
typeWebmention,
},
}
for _, tc := range cases {
t.Run(tc.label, func(t *testing.T) {
link, mentionType := sender.discover(tc.url)
assert.Equal(t, tc.expectedLink, link)
assert.Equal(t, tc.expectedType, mentionType)
})
}
}