-
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathiframe.js
More file actions
88 lines (76 loc) · 2.66 KB
/
Copy pathiframe.js
File metadata and controls
88 lines (76 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict'
const cheerio = require('cheerio')
const test = require('ava').default
const { runServer } = require('./helpers')
const createMetascraper = (...args) =>
require('metascraper')([require('../src')(...args)])
test('absolute http', async t => {
const url = await runServer(t, ({ res }) => {
res.setHeader('Content-Type', 'text/html')
res.end(
'<meta property="og:audio" content="https://cdn.microlink.io/file-examples/sample.mp3">'
)
})
const html = `<iframe src="${url}">`
const metascraper = createMetascraper()
const metadata = await metascraper({ html, url })
t.is(metadata.audio, 'https://cdn.microlink.io/file-examples/sample.mp3')
})
test('relative http', async t => {
const url = await runServer(t, ({ res }) => {
res.setHeader('Content-Type', 'text/html')
res.end('<meta property="og:audio" content="/file-examples/sample.mp3">')
})
const html = '<iframe src="/">'
const metascraper = createMetascraper()
const metadata = await metascraper({ html, url })
t.is(metadata.audio, url + 'file-examples/sample.mp3')
})
test('ignore non http urls', async t => {
const url = await runServer(t, ({ res }) => {
res.setHeader('Content-Type', 'text/html')
res.end(
'<meta property="og:audio" content="tg://join?invite=n3gS0R7pjFJhMWM0">'
)
})
const html = `<iframe src="${url}">`
const metascraper = createMetascraper()
const metadata = await metascraper({ html, url })
t.is(metadata.audio, null)
})
test('stop iframe probing after first audio match', async t => {
const calls = []
const metascraper = createMetascraper({
getIframe: async (url, $, { src }) => {
calls.push(src)
if (src.endsWith('/ok')) {
return cheerio.load(
'<meta property="og:audio" content="https://cdn.microlink.io/file-examples/sample.mp3">'
)
}
throw new Error('should not be called')
}
})
const metadata = await metascraper({
url: 'https://example.com',
html: '<iframe src="/ok"></iframe><iframe src="/skip"></iframe>'
})
t.is(metadata.audio, 'https://cdn.microlink.io/file-examples/sample.mp3')
t.deepEqual(calls, ['https://example.com/ok'])
})
test('dedupe normalized iframe urls while probing', async t => {
let calls = 0
const metascraper = createMetascraper({
getIframe: async (url, $, { src }) => {
calls += 1
t.is(src, 'https://example.com/dup')
return cheerio.load('<meta property="og:title" content="No audio">')
}
})
const metadata = await metascraper({
url: 'https://example.com',
html: '<iframe src="/dup"></iframe><iframe src="https://example.com/dup"></iframe>'
})
t.is(metadata.audio, null)
t.is(calls, 1)
})