Skip to content

Commit

Permalink
test: update ProxyAgent unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rossilor95 committed Feb 22, 2024
1 parent 12194a8 commit 902b801
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions test/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ test('using auth in combination with token should throw', (t) => {
)
})

test('should accept string and object as options', (t) => {
t = tspl(t, { plan: 2 })
test('should accept string, URL and object as options', (t) => {
t = tspl(t, { plan: 3 })
t.doesNotThrow(() => new ProxyAgent('http://example.com'))
t.doesNotThrow(() => new ProxyAgent(new URL('http://example.com')))
t.doesNotThrow(() => new ProxyAgent({ uri: 'http://example.com' }))
})

Expand Down Expand Up @@ -148,6 +149,47 @@ test('use proxy-agent to connect through proxy using path with params', async (t
proxyAgent.close()
})

test('use proxy-agent to connect through proxy with basic auth in URL', async (t) => {
t = tspl(t, { plan: 7 })
const server = await buildServer()
const proxy = await buildProxy()

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = new URL(`http://user:pass@localhost:${proxy.address().port}`)
const proxyAgent = new ProxyAgent(proxyUrl)
const parsedOrigin = new URL(serverUrl)

proxy.authenticate = function (req, fn) {
t.ok(true, 'authentication should be called')
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`)
}
proxy.on('connect', () => {
t.ok(true, 'proxy should be called')
})

server.on('request', (req, res) => {
t.strictEqual(req.url, '/hello?foo=bar')
t.strictEqual(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host')
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ hello: 'world' }))
})

const {
statusCode,
headers,
body
} = await request(serverUrl + '/hello?foo=bar', { dispatcher: proxyAgent })
const json = await body.json()

t.strictEqual(statusCode, 200)
t.deepStrictEqual(json, { hello: 'world' })
t.strictEqual(headers.connection, 'keep-alive', 'should remain the connection open')

server.close()
proxy.close()
proxyAgent.close()
})

test('use proxy-agent with auth', async (t) => {
t = tspl(t, { plan: 7 })
const server = await buildServer()
Expand Down

0 comments on commit 902b801

Please sign in to comment.