Skip to content

Commit

Permalink
build(deps-dev): bump proxy from 1.0.2 to 2.1.1 (#2137)
Browse files Browse the repository at this point in the history
  • Loading branch information
dependabot[bot] committed Feb 25, 2024
1 parent 95bd929 commit a2baaf5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 29 deletions.
12 changes: 6 additions & 6 deletions docs/docs/best-practices/proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ If you proxy requires basic authentication, you can send it via the `proxy-autho
```js
import { Client } from 'undici'
import { createServer } from 'http'
import proxy from 'proxy'
import { createProxy } from 'proxy'

const server = await buildServer()
const proxyServer = await buildProxy()
Expand Down Expand Up @@ -59,7 +59,7 @@ function buildServer () {

function buildProxy () {
return new Promise((resolve, reject) => {
const server = proxy(createServer())
const server = createProxy(createServer())
server.listen(0, () => resolve(server))
})
}
Expand All @@ -70,16 +70,16 @@ function buildProxy () {
```js
import { Client } from 'undici'
import { createServer } from 'http'
import proxy from 'proxy'
import { createProxy } from 'proxy'

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

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

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

server.on('request', (req, res) => {
Expand Down Expand Up @@ -119,7 +119,7 @@ function buildServer () {

function buildProxy () {
return new Promise((resolve, reject) => {
const server = proxy(createServer())
const server = createProxy(createServer())
server.listen(0, () => resolve(server))
})
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"jsdom": "^24.0.0",
"jsfuzz": "^1.0.15",
"pre-commit": "^1.2.2",
"proxy": "^1.0.2",
"proxy": "^2.1.1",
"proxyquire": "^2.1.3",
"snazzy": "^9.0.0",
"standard": "^17.0.0",
Expand Down
38 changes: 20 additions & 18 deletions test/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ProxyAgent = require('../lib/dispatcher/proxy-agent')
const Pool = require('../lib/dispatcher/pool')
const { createServer } = require('node:http')
const https = require('node:https')
const proxy = require('proxy')
const { createProxy } = require('proxy')

test('should throw error when no uri is provided', (t) => {
t = tspl(t, { plan: 2 })
Expand Down Expand Up @@ -81,16 +81,16 @@ test('use proxy agent to connect through proxy using Pool', async (t) => {
let resolveFirstConnect
let connectCount = 0

proxy.authenticate = async function (req, fn) {
proxy.authenticate = async function (req) {
if (++connectCount === 2) {
t.ok(true, 'second connect should arrive while first is still inflight')
resolveFirstConnect()
fn(null, true)
return true
} else {
await new Promise((resolve) => {
resolveFirstConnect = resolve
})
fn(null, true)
return true
}
}

Expand Down Expand Up @@ -161,7 +161,7 @@ test('use proxy-agent to connect through proxy with basic auth in URL', async (t

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')}`)
return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`
}
proxy.on('connect', () => {
t.ok(true, 'proxy should be called')
Expand Down Expand Up @@ -203,9 +203,9 @@ test('use proxy-agent with auth', async (t) => {
})
const parsedOrigin = new URL(serverUrl)

proxy.authenticate = function (req, fn) {
proxy.authenticate = function (req) {
t.ok(true, 'authentication should be called')
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`)
return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`
}
proxy.on('connect', () => {
t.ok(true, 'proxy should be called')
Expand Down Expand Up @@ -247,9 +247,9 @@ test('use proxy-agent with token', async (t) => {
})
const parsedOrigin = new URL(serverUrl)

proxy.authenticate = function (req, fn) {
proxy.authenticate = function (req) {
t.ok(true, 'authentication should be called')
fn(null, req.headers['proxy-authorization'] === `Bearer ${Buffer.from('user:pass').toString('base64')}`)
return req.headers['proxy-authorization'] === `Bearer ${Buffer.from('user:pass').toString('base64')}`
}
proxy.on('connect', () => {
t.ok(true, 'proxy should be called')
Expand Down Expand Up @@ -460,16 +460,17 @@ test('ProxyAgent correctly sends headers when using fetch - #1355, #1623', async
})

test('should throw when proxy does not return 200', async (t) => {
t = tspl(t, { plan: 2 })
t = tspl(t, { plan: 3 })

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, fn) {
fn(null, false)
proxy.authenticate = function (_req) {
t.ok(true, 'should call authenticate')
return false
}

const proxyAgent = new ProxyAgent(proxyUrl)
Expand All @@ -488,15 +489,16 @@ test('should throw when proxy does not return 200', async (t) => {
})

test('pass ProxyAgent proxy status code error when using fetch - #2161', async (t) => {
t = tspl(t, { plan: 1 })
t = tspl(t, { plan: 2 })
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, fn) {
fn(null, false)
proxy.authenticate = function (_req) {
t.ok(true, 'should call authenticate')
return false
}

const proxyAgent = new ProxyAgent(proxyUrl)
Expand Down Expand Up @@ -742,8 +744,8 @@ function buildSSLServer () {
function buildProxy (listener) {
return new Promise((resolve) => {
const server = listener
? proxy(createServer(listener))
: proxy(createServer())
? createProxy(createServer(listener))
: createProxy(createServer())
server.listen(0, () => resolve(server))
})
}
Expand All @@ -758,7 +760,7 @@ function buildSSLProxy () {
}

return new Promise((resolve) => {
const server = proxy(https.createServer(serverOptions))
const server = createProxy(https.createServer(serverOptions))
server.listen(0, () => resolve(server))
})
}
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) {
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) {
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))
})
}

0 comments on commit a2baaf5

Please sign in to comment.