Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(deps-dev): bump proxy from 1.0.2 to 2.1.1 #2137

Merged
merged 7 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
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')}`
}
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved

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
34 changes: 18 additions & 16 deletions test/proxy-agent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'

Check failure on line 1 in test/proxy-agent.js

View workflow job for this annotation

GitHub Actions / Test / test (20, ubuntu-latest)

test/proxy-agent.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

Check failure on line 1 in test/proxy-agent.js

View workflow job for this annotation

GitHub Actions / Test / test (18, ubuntu-latest)

test/proxy-agent.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms', code: 'ERR_TEST_FAILURE' }

Check failure on line 1 in test/proxy-agent.js

View workflow job for this annotation

GitHub Actions / Test / test (21, ubuntu-latest, experimental)

test/proxy-agent.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

const { tspl } = require('@matteo.collina/tspl')
const { test, after } = require('node:test')
Expand All @@ -10,7 +10,7 @@
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 @@ -85,12 +85,12 @@
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 @@ -149,7 +149,7 @@
proxyAgent.close()
})

test('use proxy-agent to connect through proxy with basic auth in URL', async (t) => {

Check failure on line 152 in test/proxy-agent.js

View workflow job for this annotation

GitHub Actions / Test / test (20, ubuntu-latest)

use proxy-agent to connect through proxy with basic auth in URL

[Error [ERR_TEST_FAILURE]: Proxy response (500) !== 200 when HTTP Tunneling] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: Error [AbortError]: Proxy response (500) !== 200 when HTTP Tunneling at Client.connect (/home/runner/work/undici/undici/lib/dispatcher/proxy-agent.js:1:17081) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { code: 'UND_ERR_ABORTED' } }

Check failure on line 152 in test/proxy-agent.js

View workflow job for this annotation

GitHub Actions / Test / test (18, ubuntu-latest)

use proxy-agent to connect through proxy with basic auth in URL

[Error [ERR_TEST_FAILURE]: Proxy response (500) !== 200 when HTTP Tunneling] { failureType: 'testCodeFailure', cause: Error [AbortError]: Proxy response (500) !== 200 when HTTP Tunneling at Client.connect (/home/runner/work/undici/undici/lib/dispatcher/proxy-agent.js:1:17081) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { code: 'UND_ERR_ABORTED' }, code: 'ERR_TEST_FAILURE' }

Check failure on line 152 in test/proxy-agent.js

View workflow job for this annotation

GitHub Actions / Test / test (21, ubuntu-latest, experimental)

use proxy-agent to connect through proxy with basic auth in URL

[Error [ERR_TEST_FAILURE]: Proxy response (500) !== 200 when HTTP Tunneling] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: Error [AbortError]: Proxy response (500) !== 200 when HTTP Tunneling at Client.connect (/home/runner/work/undici/undici/lib/dispatcher/proxy-agent.js:1:17081) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { code: 'UND_ERR_ABORTED' } }
t = tspl(t, { plan: 7 })
const server = await buildServer()
const proxy = await buildProxy()
Expand Down Expand Up @@ -203,9 +203,9 @@
})
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 @@
})
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('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('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 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 @@
}

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))
})
}
Loading