-
-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathload-iframe.js
More file actions
83 lines (70 loc) · 2.76 KB
/
Copy pathload-iframe.js
File metadata and controls
83 lines (70 loc) · 2.76 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
'use strict'
const cheerio = require('cheerio')
const { spawn } = require('child_process')
const path = require('path')
const test = require('ava').default
const { loadIframe } = require('..')
test('timeout support', async t => {
const url =
'https://accounts.google.com/gsi/iframe/select?client_id=1005640118348-amh5tgkq641oru4fbhr3psm3gt2tcc94.apps.googleusercontent.com&ux_mode=popup&ui_mode=card&as=GAUOzT7W7w8RiyH1fhs9TQ&channel_id=c8d85ad52a58747f6547a90cd4bb19047262e93029f574d126cf2095a7a80f9b&origin=https%3A%2F%2Fwww.nytimes.com'
const $ = cheerio.load(`<iframe src="${url}"></iframe>`)
const $iframe = await loadIframe(url, $)
t.is($iframe.html(), '<html><head></head><body></body></html>')
})
test('wait `load` event', async t => {
const url =
'https://wbez-rss.streamguys1.com/player/player21011316001810372.html'
const $ = cheerio.load(`<iframe src="${url}"></iframe>`)
const $iframe = await loadIframe(url, $)
t.true($iframe.html().includes('twitter:player'))
})
test('markup is correct', async t => {
const url =
'https://saas.transistor.fm/episodes/paul-jarvis-gaining-freedom-by-building-an-indie-business'
const src = 'https://share.transistor.fm/e/e83b42d0'
const $ = await loadIframe(
url,
cheerio.load(`<iframe src="${src}"></iframe>`)
)
const html = $.html()
t.true(html.includes('<html'), 'should contain html element')
t.true(html.includes('<audio'), 'should contain audio element')
t.true(html.includes('transistor.fm'), 'should reference transistor.fm')
t.true(html.includes('Paul Jarvis'), 'should contain episode title')
})
test('worker does not keep process alive after resolving', async t => {
const script = `
const cheerio = require('cheerio')
const { loadIframe } = require('./src')
;(async () => {
const src = 'data:text/html,<html><body><script>setInterval(() => {}, 1000)<\\\\/script></body></html>'
const $ = cheerio.load(\`<iframe src="\${src}"></iframe>\`)
await loadIframe('https://example.com', $, { timeout: 200 })
})().catch(error => {
console.error(error)
process.exit(1)
})
`
await new Promise((resolve, reject) => {
const child = spawn(process.execPath, ['-e', script], {
cwd: path.resolve(__dirname, '..'),
stdio: ['ignore', 'ignore', 'pipe']
})
let stderr = ''
child.stderr.on('data', chunk => {
stderr += String(chunk)
})
const timeoutId = setTimeout(() => {
child.kill('SIGKILL')
reject(
new Error('Child process did not exit in time after loadIframe resolve')
)
}, 3000)
child.once('exit', code => {
clearTimeout(timeoutId)
if (code === 0) return resolve()
reject(new Error(`Child process failed with code ${code}: ${stderr}`))
})
})
t.pass()
})