use optional objects instead of if()

This commit is contained in:
Wouter Groeneveld 2021-03-24 15:52:30 +01:00
parent 62e49c5c15
commit 1f794e5863
5 changed files with 20 additions and 15 deletions

View File

@ -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" }
}
}

View File

@ -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)
}

View File

@ -31,6 +31,8 @@
<p>this one is a pingback-only <a href="https://brainbaking.com/pingback-discover-test-single.html">single</a> one. Not good!</p>
<p>another cool link: <a href="https://brainbaking.com/link-discover-test-multiple.html">multiple</a></p>
<p>last but not least: <a href="https://brainbaking.com/link-discover-test-none.html">Nothin!</a> What a shame.</p>
]]>
</description>

View File

@ -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 () => {

View File

@ -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: `<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
@ -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/",