535d4c9d6e94a0e9553d6878ebd7860d98212a0b
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 parser failure', async
function () {
68 link: 'Invalid Link Header',
70 const expected
= false;
71 const result
= await lh
.validHub(url
, headers
, body
);
72 assert
.strictEqual(result
, expected
);
74 it('covers other failure', async
function () {
75 const expected
= false;
76 const result
= await lh
.validHub(url
, headers
, body
);
77 assert
.strictEqual(result
, expected
);
81 describe('absoluteURI', function () {
82 it('success', function () {
84 const context
= 'https://example.com/base/';
85 const expected
= 'https://example.com/rel';
86 const result
= lh
.absoluteURI(uri
, context
);
87 assert
.strictEqual(result
, expected
);
89 it('failure', function () {
91 const context
= '/not/valid';
92 const expected
= '../rel';
93 const result
= lh
.absoluteURI(uri
, context
);
94 assert
.strictEqual(result
, expected
);
98 describe('locateHubTargets', function () {
99 it('covers', function () {
102 target: 'https://example.com/hub1/',
111 target: 'https://example.com/index',
120 target: 'https://example.com/hub2/',
129 const expected
= ['https://example.com/hub1/', 'https://example.com/hub2/'];
130 const result
= LinkHelper
.locateHubTargets(links
);
131 assert
.deepStrictEqual(result
, expected
);
133 }); // locateHubTargets
135 describe('linksFromFeedBody', function () {
136 it('parses rss', async
function () {
137 const feedData
= testData
.rssFeedBody
;
138 const feedUrl
= testData
.rssFeedUrl
;
148 value: 'application/rss+xml',
151 target: 'https://puppetcircuits.wordpress.com/feed/',
161 value: 'application/opensearchdescription+xml',
165 value: 'Puppet Circuits',
168 target: 'https://puppetcircuits.wordpress.com/osd.xml',
177 target: 'https://puppetcircuits.wordpress.com/?pushpress=hub',
180 const result
= await lh
.linksFromFeedBody(feedUrl
, feedData
);
181 assert
.deepStrictEqual(result
, expected
);
183 it('parses atom', async
function () {
184 const feedData
= testData
.atomFeedBody
;
185 const feedUrl
= testData
.atomFeedUrl
;
198 target: 'https://squeep.com/eats/',
208 value: 'application/atom+xml',
211 target: 'https://squeep.com/eats/atom/',
220 target: 'https://hub.squeep.com/',
223 const result
= await lh
.linksFromFeedBody(feedUrl
, feedData
);
224 assert
.deepStrictEqual(result
, expected
);
226 it('does not parse HTML', async
function () {
227 const feedData
= testData
.htmlBody
;
228 const feedUrl
= testData
.htmlUrl
;
230 const result
= await lh
.linksFromFeedBody(feedUrl
, feedData
);
231 assert
.deepStrictEqual(result
, expected
);
233 }); // hubLinksFromFeedBody
235 describe('linksFromHTMLBody', function () {
236 it('parses HTML', function () {
237 const htmlData
= testData
.htmlBody
;
251 value: 'font/opentype',
258 target: 'oldstyle.otf',
280 target: 'https://hub.squeep.com/',
290 value: 'application/atom+xml',
297 target: 'https://squeep.com/eats/atom/',
300 const result
= lh
.linksFromHTMLBody(htmlData
);
301 assert
.deepStrictEqual(result
, expected
);
303 }); // linksFromHTMLBody