4 const assert
= require('assert');
5 const sinon
= require('sinon'); // eslint-disable-line node/no-unpublished-require
6 const LinkHelper
= require('../../src/link-helper');
7 const stubLogger
= require('../stub-logger');
8 const testData
= require('../test-data/link-helper');
10 describe('LinkHelper', function () {
12 beforeEach(function () {
15 selfBaseUrl: 'https://example.com/hub/',
18 lh
= new LinkHelper(stubLogger
, options
);
20 afterEach(function () {
23 describe('validHub', function () {
24 let url
, headers
, body
;
25 beforeEach(function () {
26 url
= 'https://example.com/feed/';
30 it('covers success', async
function () {
32 link: '<https://example.com/hub/>; rel="hub"',
34 const expected
= true;
35 const result
= await lh
.validHub(url
, headers
, body
);
36 assert
.strictEqual(result
, expected
);
38 it('covers wrong hub', async
function () {
40 link: '<https://example.com/other/hub/>; rel="hub"',
42 const expected
= false;
43 const result
= await lh
.validHub(url
, headers
, body
);
44 assert
.strictEqual(result
, expected
);
46 it('covers link in Atom body', async
function () {
48 'content-type': 'application/xml',
50 body
= testData
.atomFeedBody
;
51 url
= testData
.atomFeedUrl
;
52 lh
.selfUrl
= 'https://hub.squeep.com/';
53 const expected
= true;
54 const result
= await lh
.validHub(url
, headers
, body
);
55 assert
.strictEqual(result
, expected
);
57 it('covers link in HTML body', async
function () {
59 'content-type': 'text/html',
61 body
= '<html><head><link rel="hub" href="https://example.com/hub/"></head></html>';
62 const expected
= true;
63 const result
= await lh
.validHub(url
, headers
, body
);
64 assert
.strictEqual(result
, expected
);
66 it('covers link in HTML body with charset translation', async
function () {
68 'content-type': 'text/html; charset=ASCII',
70 body
= '<html><head><link rel="hub" href="https://example.com/hub/"></head></html>';
71 const expected
= true;
72 const result
= await lh
.validHub(url
, headers
, body
);
73 assert
.strictEqual(result
, expected
);
75 it('covers parser failure', async
function () {
77 link: 'Invalid Link Header',
79 const expected
= false;
80 const result
= await lh
.validHub(url
, headers
, body
);
81 assert
.strictEqual(result
, expected
);
83 it('covers other failure', async
function () {
84 const expected
= false;
85 const result
= await lh
.validHub(url
, headers
, body
);
86 assert
.strictEqual(result
, expected
);
90 describe('parseContentType', function () {
91 it('handles no data', function () {
93 mediaType: 'application/octet-stream',
96 const result
= LinkHelper
.parseContentType();
97 assert
.deepStrictEqual(result
, expected
);
99 it('handles only media type', function () {
101 mediaType: 'application/json',
104 const result
= LinkHelper
.parseContentType('application/json');
105 assert
.deepStrictEqual(result
, expected
);
107 it('handles parameters', function () {
109 mediaType: 'text/html',
111 charset: 'ISO-8859-4',
114 const result
= LinkHelper
.parseContentType('text/html; charset=ISO-8859-4');
115 assert
.deepStrictEqual(result
, expected
);
117 it('handles more parameters', function () {
119 mediaType: 'multipart/form-data',
125 const result
= LinkHelper
.parseContentType('multipart/form-data; boundary="--123--"; other=foo');
126 assert
.deepStrictEqual(result
, expected
);
128 }); // parseContentType
130 describe('absoluteURI', function () {
131 it('success', function () {
132 const uri
= '../rel';
133 const context
= 'https://example.com/base/';
134 const expected
= 'https://example.com/rel';
135 const result
= lh
.absoluteURI(uri
, context
);
136 assert
.strictEqual(result
, expected
);
138 it('failure', function () {
139 const uri
= '../rel';
140 const context
= '/not/valid';
141 const expected
= '../rel';
142 const result
= lh
.absoluteURI(uri
, context
);
143 assert
.strictEqual(result
, expected
);
147 describe('locateHubTargets', function () {
148 it('covers', function () {
151 target: 'https://example.com/hub1/',
160 target: 'https://example.com/index',
169 target: 'https://example.com/hub2/',
178 const expected
= ['https://example.com/hub1/', 'https://example.com/hub2/'];
179 const result
= LinkHelper
.locateHubTargets(links
);
180 assert
.deepStrictEqual(result
, expected
);
182 }); // locateHubTargets
184 describe('linksFromFeedBody', function () {
185 it('parses rss', async
function () {
186 const feedData
= testData
.rssFeedBody
;
187 const feedUrl
= testData
.rssFeedUrl
;
196 target: 'https://hub.squeep.com/',
199 const result
= await lh
.linksFromFeedBody(feedUrl
, feedData
);
200 assert
.deepStrictEqual(result
, expected
);
202 it('parses more rss', async
function () {
203 const feedData
= testData
.rssFeedBody2
;
204 const feedUrl
= testData
.rssFeedUrl2
;
214 value: 'application/rss+xml',
217 target: 'https://puppetcircuits.wordpress.com/feed/',
227 value: 'application/opensearchdescription+xml',
231 value: 'Puppet Circuits',
234 target: 'https://puppetcircuits.wordpress.com/osd.xml',
243 target: 'https://puppetcircuits.wordpress.com/?pushpress=hub',
246 const result
= await lh
.linksFromFeedBody(feedUrl
, feedData
);
247 assert
.deepStrictEqual(result
, expected
);
249 it('parses atom', async
function () {
250 const feedData
= testData
.atomFeedBody
;
251 const feedUrl
= testData
.atomFeedUrl
;
264 target: 'https://squeep.com/eats/',
274 value: 'application/atom+xml',
277 target: 'https://squeep.com/eats/atom/',
286 target: 'https://hub.squeep.com/',
289 const result
= await lh
.linksFromFeedBody(feedUrl
, feedData
);
290 assert
.deepStrictEqual(result
, expected
);
292 it('does not parse HTML', async
function () {
293 const feedData
= testData
.htmlBody
;
294 const feedUrl
= testData
.htmlUrl
;
296 const result
= await lh
.linksFromFeedBody(feedUrl
, feedData
);
297 assert
.deepStrictEqual(result
, expected
);
299 }); // hubLinksFromFeedBody
301 describe('linksFromHTMLBody', function () {
302 it('parses HTML', function () {
303 const htmlData
= testData
.htmlBody
;
317 value: 'font/opentype',
324 target: 'oldstyle.otf',
346 target: 'https://hub.squeep.com/',
356 value: 'application/atom+xml',
363 target: 'https://squeep.com/eats/atom/',
366 const result
= lh
.linksFromHTMLBody(htmlData
);
367 assert
.deepStrictEqual(result
, expected
);
369 }); // linksFromHTMLBody