3 const assert
= require('node:assert');
4 const sinon
= require('sinon');
5 const LinkHelper
= require('../../src/link-helper');
6 const stubLogger
= require('../stub-logger');
7 const testData
= require('../test-data/link-helper');
9 describe('LinkHelper', function () {
11 beforeEach(function () {
14 selfBaseUrl: 'https://example.com/hub/',
17 lh
= new LinkHelper(stubLogger
, options
);
19 afterEach(function () {
22 describe('validHub', function () {
23 let url
, headers
, body
;
24 beforeEach(function () {
25 url
= 'https://example.com/feed/';
29 it('covers success', async
function () {
31 link: '<https://example.com/hub/>; rel="hub"',
33 const expected
= true;
34 const result
= await lh
.validHub(url
, headers
, body
);
35 assert
.strictEqual(result
, expected
);
37 it('covers wrong hub', async
function () {
39 link: '<https://example.com/other/hub/>; rel="hub"',
41 const expected
= false;
42 const result
= await lh
.validHub(url
, headers
, body
);
43 assert
.strictEqual(result
, expected
);
45 it('covers link in Atom body', async
function () {
47 'content-type': 'application/xml',
49 body
= testData
.atomFeedBody
;
50 url
= testData
.atomFeedUrl
;
51 lh
.selfUrl
= 'https://hub.squeep.com/';
52 const expected
= true;
53 const result
= await lh
.validHub(url
, headers
, body
);
54 assert
.strictEqual(result
, expected
);
56 it('covers link in HTML body', async
function () {
58 'content-type': 'text/html',
60 body
= '<html><head><link rel="hub" href="https://example.com/hub/"></head></html>';
61 const expected
= true;
62 const result
= await lh
.validHub(url
, headers
, body
);
63 assert
.strictEqual(result
, expected
);
65 it('covers link in HTML body with charset translation', async
function () {
67 'content-type': 'text/html; charset=ASCII',
69 body
= '<html><head><link rel="hub" href="https://example.com/hub/"></head></html>';
70 const expected
= true;
71 const result
= await lh
.validHub(url
, headers
, body
);
72 assert
.strictEqual(result
, expected
);
74 it('covers parser failure', async
function () {
76 link: 'Invalid Link Header',
78 const expected
= false;
79 const result
= await lh
.validHub(url
, headers
, body
);
80 assert
.strictEqual(result
, expected
);
82 it('covers other failure', async
function () {
83 const expected
= false;
84 const result
= await lh
.validHub(url
, headers
, body
);
85 assert
.strictEqual(result
, expected
);
89 describe('parseContentType', function () {
90 it('handles no data', function () {
92 mediaType: 'application/octet-stream',
95 const result
= LinkHelper
.parseContentType();
96 assert
.deepStrictEqual(result
, expected
);
98 it('handles only media type', function () {
100 mediaType: 'application/json',
103 const result
= LinkHelper
.parseContentType('application/json');
104 assert
.deepStrictEqual(result
, expected
);
106 it('handles parameters', function () {
108 mediaType: 'text/html',
110 charset: 'ISO-8859-4',
113 const result
= LinkHelper
.parseContentType('text/html; charset=ISO-8859-4');
114 assert
.deepStrictEqual(result
, expected
);
116 it('handles more parameters', function () {
118 mediaType: 'multipart/form-data',
124 const result
= LinkHelper
.parseContentType('multipart/form-data; boundary="--123--"; other=foo');
125 assert
.deepStrictEqual(result
, expected
);
127 }); // parseContentType
129 describe('absoluteURI', function () {
130 it('success', function () {
131 const uri
= '../rel';
132 const context
= 'https://example.com/base/';
133 const expected
= 'https://example.com/rel';
134 const result
= lh
.absoluteURI(uri
, context
);
135 assert
.strictEqual(result
, expected
);
137 it('failure', function () {
138 const uri
= '../rel';
139 const context
= '/not/valid';
140 const expected
= '../rel';
141 const result
= lh
.absoluteURI(uri
, context
);
142 assert
.strictEqual(result
, expected
);
146 describe('locateHubTargets', function () {
147 it('covers', function () {
150 target: 'https://example.com/hub1/',
159 target: 'https://example.com/index',
168 target: 'https://example.com/hub2/',
177 const expected
= ['https://example.com/hub1/', 'https://example.com/hub2/'];
178 const result
= LinkHelper
.locateHubTargets(links
);
179 assert
.deepStrictEqual(result
, expected
);
181 }); // locateHubTargets
183 describe('linksFromFeedBody', function () {
184 it('parses rss', async
function () {
185 const feedData
= testData
.rssFeedBody
;
186 const feedUrl
= testData
.rssFeedUrl
;
195 target: 'https://hub.squeep.com/',
198 const result
= await lh
.linksFromFeedBody(feedUrl
, feedData
);
199 assert
.deepStrictEqual(result
, expected
);
201 it('parses more rss', async
function () {
202 const feedData
= testData
.rssFeedBody2
;
203 const feedUrl
= testData
.rssFeedUrl2
;
213 value: 'application/rss+xml',
216 target: 'https://puppetcircuits.wordpress.com/feed/',
226 value: 'application/opensearchdescription+xml',
230 value: 'Puppet Circuits',
233 target: 'https://puppetcircuits.wordpress.com/osd.xml',
242 target: 'https://puppetcircuits.wordpress.com/?pushpress=hub',
245 const result
= await lh
.linksFromFeedBody(feedUrl
, feedData
);
246 assert
.deepStrictEqual(result
, expected
);
248 it('parses atom', async
function () {
249 const feedData
= testData
.atomFeedBody
;
250 const feedUrl
= testData
.atomFeedUrl
;
263 target: 'https://squeep.com/eats/',
273 value: 'application/atom+xml',
276 target: 'https://squeep.com/eats/atom/',
285 target: 'https://hub.squeep.com/',
288 const result
= await lh
.linksFromFeedBody(feedUrl
, feedData
);
289 assert
.deepStrictEqual(result
, expected
);
291 it('does not parse HTML', async
function () {
292 const feedData
= testData
.htmlBody
;
293 const feedUrl
= testData
.htmlUrl
;
295 const result
= await lh
.linksFromFeedBody(feedUrl
, feedData
);
296 assert
.deepStrictEqual(result
, expected
);
298 }); // hubLinksFromFeedBody
300 describe('linksFromHTMLBody', function () {
301 it('parses HTML', function () {
302 const htmlData
= testData
.htmlBody
;
316 value: 'font/opentype',
323 target: 'oldstyle.otf',
345 target: 'https://hub.squeep.com/',
355 value: 'application/atom+xml',
362 target: 'https://squeep.com/eats/atom/',
365 const result
= lh
.linksFromHTMLBody(htmlData
);
366 assert
.deepStrictEqual(result
, expected
);
368 }); // linksFromHTMLBody