diff --git a/src/linkdiscoverer.js b/src/linkdiscoverer.js index e3b2917..6c0c6e2 100644 --- a/src/linkdiscoverer.js +++ b/src/linkdiscoverer.js @@ -39,17 +39,14 @@ async function discover(target) { }) const webmention = format.rels?.webmention?.[0] const pingback = format.rels?.pingback?.[0] - if(!webmention && !pingback) { - throw "no webmention and no pingback found?" - } return { - link: webmention ? webmention : pingback, - type: webmention ? "webmention" : "pingback" + link: webmention ? webmention : (pingback ? pingback : ""), + type: webmention ? "webmention" : (pingback ? "pingback" : "unknown") } } catch(err) { console.warn(` -- whoops, failed to discover ${target}, why: ${err}`) - return undefined + return { type: "unknown" } } } diff --git a/src/webmention/send.js b/src/webmention/send.js index 8de5c97..afe08ec 100644 --- a/src/webmention/send.js +++ b/src/webmention/send.js @@ -22,10 +22,10 @@ async function sendWebmentionToEndpoint(endpoint, source, target) { async function mention(opts) { const { source, target } = opts const endpoint = await discover(target) - if(!endpoint) return const sendMention = { "webmention": sendWebmentionToEndpoint, - "pingback": sendPingbackToEndpoint + "pingback": sendPingbackToEndpoint, + "unknown": async function() {} } await sendMention[endpoint.type](endpoint.link, source, target) } diff --git a/test/__mocks__/index.xml b/test/__mocks__/index.xml index e4e2f44..09afc9b 100644 --- a/test/__mocks__/index.xml +++ b/test/__mocks__/index.xml @@ -31,6 +31,8 @@

this one is a pingback-only single one. Not good!

another cool link: multiple

+ +

last but not least: Nothin! What a shame.

]]> diff --git a/test/linkdiscoverer.test.js b/test/linkdiscoverer.test.js index 4a3bb6d..69ad088 100644 --- a/test/linkdiscoverer.test.js +++ b/test/linkdiscoverer.test.js @@ -3,9 +3,11 @@ const { discover } = require('../src/linkdiscoverer') describe("link discoverer", () => { - test("discover nothing if no link is present", async() => { + test("discover 'unknown' if no link is present", async() => { const result = await discover("https://brainbaking.com/link-discover-test-none.html") - expect(result).toBeUndefined() + expect(result).toEqual({ + type: "unknown" + }) }) test("prefer webmentions over pingbacks if both links are present", async () => { diff --git a/test/webmention/send.test.js b/test/webmention/send.test.js index 241a8d0..514b29f 100644 --- a/test/webmention/send.test.js +++ b/test/webmention/send.test.js @@ -6,13 +6,17 @@ const { send } = require('../../src/webmention/send') describe("webmention send scenarios", () => { test("webmention send integration test that can send both webmentions and pingbacks", async () => { - got.post = jest.fn() + // jest.fn() gives unpredictable and unreadable output if unorderd calledWith... DIY! + let posts = {} + got.post = function(url, opts) { + posts[url] = opts + } // fetches index.xml await send("brainbaking.com", '2021-03-16T16:00:00.000Z') - expect(got.post).toHaveBeenCalledTimes(3) - expect(got.post).toHaveBeenCalledWith("http://aaronpk.example/webmention-endpoint-header", { + expect(Object.keys(posts).length).toBe(3) + expect(posts["http://aaronpk.example/webmention-endpoint-header"]).toEqual({ contentType: "x-www-form-urlencoded", form: { source: "https://brainbaking.com/notes/2021/03/16h17m07s14/", @@ -23,7 +27,7 @@ describe("webmention send scenarios", () => { methods: ["POST"] } }) - expect(got.post).toHaveBeenCalledWith("http://aaronpk.example/pingback-endpoint-body", { + expect(posts["http://aaronpk.example/pingback-endpoint-body"]).toEqual({ contentType: "text/xml", body: ` @@ -42,7 +46,7 @@ describe("webmention send scenarios", () => { methods: ["POST"] } }) - expect(got.post).toHaveBeenCalledWith("http://aaronpk.example/webmention-endpoint-body", { + expect(posts["http://aaronpk.example/webmention-endpoint-body"]).toEqual({ contentType: "x-www-form-urlencoded", form: { source: "https://brainbaking.com/notes/2021/03/16h17m07s14/",