Skip to content

Commit

Permalink
test: fix proxyUrl (microlinkhq#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats authored Feb 12, 2025
1 parent 77a3ab6 commit 0291008
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
19 changes: 11 additions & 8 deletions packages/browserless/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,19 @@ test('pass specific options to a context', async t => {
})
})

const targetUrl = await runServer(t, ({ res }) => res.end())

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

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

t.deepEqual(proxiedRequestUrls, [targetUrl, new URL('/favicon.ico', targetUrl).toString()])
t.is(text, 'origin server reached')
t.deepEqual(proxiedRequestUrls, [
new URL(url).toString(),
new URL('/favicon.ico', url).toString()
])
})

test('ensure to destroy browser contexts', async t => {
Expand Down
22 changes: 21 additions & 1 deletion packages/test/util/create.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
'use strict'

const { default: listen } = require('async-listen')
const { createServer } = require('http')
const { onExit } = require('signal-exit')
const { createServer } = require('http')
const os = require('os')

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

let HOSTNAME = os.hostname()

// Hostname might not be always accessible in environments other than GitHub
// Actions. Therefore, we try to find an external IPv4 address to be used as a
// hostname in these tests.
const networkInterfaces = os.networkInterfaces()
for (const key of Object.keys(networkInterfaces)) {
const interfaces = networkInterfaces[key]
for (const net of interfaces || []) {
if (net.family === 'IPv4' && !net.internal) {
HOSTNAME = net.address
break
}
}
}

const runServer = async (t, handler) => {
const server = createServer(async (req, res) => {
try {
Expand All @@ -16,7 +33,10 @@ const runServer = async (t, handler) => {
res.end()
}
})

const url = await listen(server)
url.hostname = HOSTNAME

t.teardown(() => closeServer(server))
return url.toString()
}
Expand Down

0 comments on commit 0291008

Please sign in to comment.