Skip to content

Commit dee0dd4

Browse files
authored
test: allow Node 24 url.parse/punycode deprecations and refactor
Add url.parse() and punycode to global deprecation warning exceptions in test setup so tests pass on Node.js 24.13.1+. Add a request-scoped exception in the express-session spec for when [email protected] calls url.parse(). Replace url.parse() with the WHATWG URL API in the microgateway-core test proxy and in both Next test server helpers.
1 parent 3d1e60e commit dee0dd4

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

packages/datadog-plugin-microgateway-core/test/proxy.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict'
22

3-
const http = require('http')
4-
const net = require('net')
5-
const tls = require('tls')
6-
const url = require('url')
7-
const util = require('util')
3+
const http = require('node:http')
4+
const net = require('node:net')
5+
const tls = require('node:tls')
6+
const { URL } = require('node:url')
87

98
const axios = require('axios')
109

@@ -38,8 +37,7 @@ module.exports = http.createServer((req, res) => {
3837
cltSocket.pipe(targetConnection)
3938
}
4039

41-
// eslint-disable-next-line n/no-deprecated-api
42-
const targetUrl = url.parse(util.format('%s://%s', proto, req.url))
40+
const targetUrl = new URL(`${proto}://${req.url}`)
4341

4442
let targetConnection
4543

packages/datadog-plugin-next/test/server.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
'use strict'
22

3-
const { PORT, HOSTNAME } = process.env
3+
const { createServer } = require('node:http')
4+
const { URL } = require('node:url')
45

5-
const { createServer } = require('http')
6-
// eslint-disable-next-line n/no-deprecated-api
7-
const { parse } = require('url')
86
const next = require('next')
97

8+
const { PORT, HOSTNAME } = process.env
9+
1010
const app = next({ dir: __dirname, dev: false, quiet: true, hostname: HOSTNAME, port: PORT })
1111
const handle = app.getRequestHandler()
1212

1313
app.prepare().then(() => {
1414
const server = createServer((req, res) => {
15-
const parsedUrl = parse(req.url, true)
15+
const base = `http://${req.headers.host || 'localhost'}`
16+
const url = new URL(req.url || '/', base)
1617

17-
if (parsedUrl.path === '/exit') {
18+
if (url.pathname === '/exit') {
1819
server.close()
1920
} else {
20-
handle(req, res, parsedUrl)
21+
handle(req, res, {
22+
pathname: url.pathname,
23+
path: url.pathname + url.search,
24+
query: Object.fromEntries(url.searchParams),
25+
search: url.search,
26+
})
2127
}
2228
}).listen(PORT, HOSTNAME, () => {
2329
console.log(server.address()) // eslint-disable-line no-console

packages/dd-trace/test/appsec/next/pages-dir/server.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
const { PORT, HOSTNAME } = process.env
1+
const { createServer } = require('node:http')
2+
const { URL } = require('node:url')
23

3-
const { createServer } = require('http')
4-
// eslint-disable-next-line n/no-deprecated-api
5-
const { parse } = require('url')
64
const next = require('next')
75

6+
const { PORT, HOSTNAME } = process.env
7+
88
const app = next({ dir: __dirname, dev: false, quiet: true, hostname: HOSTNAME, port: PORT })
99
const handle = app.getRequestHandler()
1010

1111
app.prepare().then(() => {
1212
const server = createServer((req, res) => {
13-
const parsedUrl = parse(req.url, true)
13+
const base = `http://${req.headers.host || 'localhost'}`
14+
const url = new URL(req.url || '/', base)
1415

15-
if (parsedUrl.path === '/exit') {
16+
if (url.pathname === '/exit') {
1617
server.close()
1718
} else {
18-
handle(req, res, parsedUrl)
19+
handle(req, res, {
20+
pathname: url.pathname,
21+
path: url.pathname + url.search,
22+
query: Object.fromEntries(url.searchParams),
23+
search: url.search,
24+
})
1925
}
2026
}).listen(PORT, HOSTNAME, () => {
2127
console.log(server.address()) // eslint-disable-line no-console

packages/dd-trace/test/setup/core.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ const warningExceptions = new Set([
4141
// Node.js core warnings. Ignore them.
4242
'OutgoingMessage.prototype._headers is deprecated',
4343
"Access to process.binding('http_parser') is deprecated.",
44+
'`url.parse()` behavior is not standardized and prone to errors that have security implications. Use the WHATWG ' +
45+
'URL API instead. CVEs are not issued for `url.parse()` vulnerabilities.',
46+
'The `punycode` module is deprecated. Please use a userland alternative instead.',
4447
// TODO: We should not be throwing warnings in the first place. Fix the following warnings instead.
4548
"Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html",
4649
'collection.count is deprecated, and will be removed in a future version. ' +

0 commit comments

Comments
 (0)