Skip to content

Commit 0291008

Browse files
authored
test: fix proxyUrl (#603)
1 parent 77a3ab6 commit 0291008

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

packages/browserless/test/index.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,19 @@ test('pass specific options to a context', async t => {
4141
})
4242
})
4343

44-
const targetUrl = await runServer(t, ({ res }) => res.end())
45-
46-
const { host: proxyServer } = new URL(proxyUrl)
47-
const browserless = await getBrowserContext(t, { proxyServer })
48-
const page = await browserless.page()
49-
t.teardown(() => page.close())
44+
const url = await runServer(t, ({ res }) => {
45+
res.writeHead(200, { 'Content-Type': 'text/html' })
46+
res.end('<html><body><h1>origin server reached</h1></body></html>')
47+
})
5048

51-
await browserless.goto(page, { url: targetUrl })
49+
const browserless = await getBrowserContext(t, { proxyServer: proxyUrl.slice(0, -1) })
50+
const text = await browserless.text(url)
5251

53-
t.deepEqual(proxiedRequestUrls, [targetUrl, new URL('/favicon.ico', targetUrl).toString()])
52+
t.is(text, 'origin server reached')
53+
t.deepEqual(proxiedRequestUrls, [
54+
new URL(url).toString(),
55+
new URL('/favicon.ico', url).toString()
56+
])
5457
})
5558

5659
test('ensure to destroy browser contexts', async t => {

packages/test/util/create.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
'use strict'
22

33
const { default: listen } = require('async-listen')
4-
const { createServer } = require('http')
54
const { onExit } = require('signal-exit')
5+
const { createServer } = require('http')
6+
const os = require('os')
67

78
const closeServer = server => require('util').promisify(server.close.bind(server))()
89

10+
let HOSTNAME = os.hostname()
11+
12+
// Hostname might not be always accessible in environments other than GitHub
13+
// Actions. Therefore, we try to find an external IPv4 address to be used as a
14+
// hostname in these tests.
15+
const networkInterfaces = os.networkInterfaces()
16+
for (const key of Object.keys(networkInterfaces)) {
17+
const interfaces = networkInterfaces[key]
18+
for (const net of interfaces || []) {
19+
if (net.family === 'IPv4' && !net.internal) {
20+
HOSTNAME = net.address
21+
break
22+
}
23+
}
24+
}
25+
926
const runServer = async (t, handler) => {
1027
const server = createServer(async (req, res) => {
1128
try {
@@ -16,7 +33,10 @@ const runServer = async (t, handler) => {
1633
res.end()
1734
}
1835
})
36+
1937
const url = await listen(server)
38+
url.hostname = HOSTNAME
39+
2040
t.teardown(() => closeServer(server))
2141
return url.toString()
2242
}

0 commit comments

Comments
 (0)