Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"mitata": "^0.1.8",
"node-fetch": "^3.3.2",
"pre-commit": "^1.2.2",
"proxy": "^1.0.2",
"proxy": "^2.1.1",
"proxyquire": "^2.1.3",
"request": "^2.88.2",
"snazzy": "^9.0.0",
Expand Down
42 changes: 38 additions & 4 deletions test/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { tspl } = require('@matteo.collina/tspl')
const { test } = require('node:test')
const { Client, Pool } = require('..')
const { createServer } = require('node:http')
const proxy = require('proxy')
const { createProxy } = require('proxy')

test('connect through proxy', async (t) => {
t = tspl(t, { plan: 3 })
Expand Down Expand Up @@ -50,8 +50,8 @@ test('connect through proxy with auth', async (t) => {
const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`

proxy.authenticate = function (req, fn) {
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`)
proxy.authenticate = function (req, res) {
return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`
}

server.on('request', (req, res) => {
Expand Down Expand Up @@ -83,6 +83,40 @@ test('connect through proxy with auth', async (t) => {
client.close()
})

test('connect through proxy with auth but invalid credentials', async (t) => {
t = tspl(t, { plan: 1 })

const server = await buildServer()
const proxy = await buildProxy()

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`

proxy.authenticate = function (req, res) {
return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:no-pass').toString('base64')}`
}

server.on('request', (req, res) => {
t.fail('should not be called')
})

const client = new Client(proxyUrl)

const response = await client.request({
method: 'GET',
path: serverUrl + '/hello?foo=bar',
headers: {
'proxy-authorization': `Basic ${Buffer.from('user:pass').toString('base64')}`
}
})

t.strictEqual(response.statusCode, 407)

server.close()
proxy.close()
client.close()
})

test('connect through proxy (with pool)', async (t) => {
t = tspl(t, { plan: 3 })

Expand Down Expand Up @@ -127,7 +161,7 @@ function buildServer () {

function buildProxy () {
return new Promise((resolve, reject) => {
const server = proxy(createServer())
const server = createProxy(createServer())
server.listen(0, () => resolve(server))
})
}