2023-06-21 11:56:59 +03:00
const assert = require ( 'assert/strict' ) ;
2023-05-02 23:43:47 +03:00
const linkReplacer = require ( '../lib/link-replacer' ) ;
2023-03-08 18:30:54 +03:00
const cheerio = require ( 'cheerio' ) ;
const sinon = require ( 'sinon' ) ;
2022-09-19 18:12:54 +03:00
describe ( 'LinkReplacementService' , function ( ) {
2023-03-08 18:30:54 +03:00
afterEach ( function ( ) {
sinon . restore ( ) ;
} ) ;
2022-09-19 18:12:54 +03:00
it ( 'exported' , function ( ) {
assert . equal ( require ( '../index' ) , linkReplacer ) ;
} ) ;
describe ( 'replace' , function ( ) {
it ( 'Can replace to URL' , async function ( ) {
const html = '<a href="http://localhost:2368/dir/path">link</a>' ;
const expected = '<a href="https://google.com/test-dir?test-query">link</a>' ;
const replaced = await linkReplacer . replace ( html , ( ) => new URL ( 'https://google.com/test-dir?test-query' ) ) ;
assert . equal ( replaced , expected ) ;
} ) ;
2023-08-08 14:22:56 +03:00
it ( 'Can replace relative URLs' , async function ( ) {
const html = '<a href="dir/path">link</a>' ;
const expected = '<a href="https://google.com/test-dir/dir/path">link</a>' ;
const replaced = await linkReplacer . replace ( html , u => u , { base : 'https://google.com/test-dir/' } ) ;
assert . equal ( replaced , expected ) ;
} ) ;
it ( 'Can replace relative URLs relative to root domain' , async function ( ) {
const html = '<a href="/dir/path">link</a>' ;
const expected = '<a href="https://google.com/dir/path">link</a>' ;
const replaced = await linkReplacer . replace ( html , u => u , { base : 'https://google.com/test-dir/' } ) ;
assert . equal ( replaced , expected ) ;
} ) ;
it ( 'Can replace fragments relative to base' , async function ( ) {
const html = '<a href="#support">link</a>' ;
const expected = '<a href="https://google.com/test-dir/#support">link</a>' ;
const replaced = await linkReplacer . replace ( html , u => u , { base : 'https://google.com/test-dir/' } ) ;
assert . equal ( replaced , expected ) ;
} ) ;
2023-03-08 18:30:54 +03:00
it ( 'Doesn\'t break weird &map' , async function ( ) {
// Refs https://github.com/TryGhost/Team/issues/2666: somehow this gets replaced with https://example.com/test.jpg?test=true↦id=de76 if decoding entities is enabled
const html = '<img src="https://example.com/test.jpg?test=true&map_id=test">' ;
const expected = '<img src="https://example.com/test.jpg?test=true&map_id=test">' ;
const replaced = await linkReplacer . replace ( html , ( ) => new URL ( 'https://google.com/test-dir?test-query' ) ) ;
assert . equal ( replaced , expected ) ;
} ) ;
it ( 'Does not escape HTML characters' , async function ( ) {
const html = 'This is a test & this \'should\' not "be" escaped' ;
const replaced = await linkReplacer . replace ( html , ( ) => new URL ( 'https://google.com/test-dir?test-query' ) ) ;
assert . equal ( replaced , html ) ;
} ) ;
2023-03-28 13:29:15 +03:00
it ( 'Does escape HTML characters within a link\'s href' , async function ( ) {
const html = '<a href="https://www.google.com/maps/d/u/0/viewer?mid=1kQUV2O5QQOigaxJUorLUC9LBP4Ibppg&ll=37.87151888616819%2C-122.27759691003418&z=13">link</a>' ;
const expected = '<a href="https://www.google.com/maps/d/u/0/viewer?mid=1kQUV2O5QQOigaxJUorLUC9LBP4Ibppg&ll=37.87151888616819%2C-122.27759691003418&z=13">link</a>' ;
const replaced = await linkReplacer . replace ( html , url => new URL ( url ) ) ;
assert . equal ( replaced , expected ) ;
} ) ;
2022-09-19 18:12:54 +03:00
it ( 'Can replace to string' , async function ( ) {
const html = '<a href="http://localhost:2368/dir/path">link</a>' ;
const expected = '<a href="#valid-string">link</a>' ;
const replaced = await linkReplacer . replace ( html , ( ) => '#valid-string' ) ;
assert . equal ( replaced , expected ) ;
} ) ;
it ( 'Ignores invalid links' , async function ( ) {
const html = '<a href="invalid">link</a>' ;
const expected = '<a href="invalid">link</a>' ;
const replaced = await linkReplacer . replace ( html , ( ) => 'valid' ) ;
assert . equal ( replaced , expected ) ;
} ) ;
2023-03-08 18:30:54 +03:00
it ( 'Ignores cheerio errors' , async function ( ) {
sinon . stub ( cheerio , 'load' ) . throws ( new Error ( 'test' ) ) ;
const html = '<a href="http://localhost:2368/dir/path">link</a>' ;
const replaced = await linkReplacer . replace ( html , ( ) => 'valid' ) ;
assert . equal ( replaced , html ) ;
} ) ;
2022-09-19 18:12:54 +03:00
} ) ;
} ) ;