diff --git a/lib/core/request.js b/lib/core/request.js index 688777e6135..376b360dfdb 100644 --- a/lib/core/request.js +++ b/lib/core/request.js @@ -14,7 +14,7 @@ const { isFormDataLike, isIterable, isBlobLike, - buildURL, + serializePathWithQuery, validateHandler, getServerName, normalizedMethodRecords @@ -135,7 +135,7 @@ class Request { this.upgrade = upgrade || null - this.path = query ? buildURL(path, query) : path + this.path = query ? serializePathWithQuery(path, query) : path this.origin = origin diff --git a/lib/core/util.js b/lib/core/util.js index d37e0d41b12..714f3874314 100644 --- a/lib/core/util.js +++ b/lib/core/util.js @@ -89,7 +89,12 @@ function isBlobLike (object) { } } -function buildURL (url, queryParams) { +/** + * @param {string} url The URL to add the query params to + * @param {import('node:querystring').ParsedUrlQueryInput} queryParams The object to serialize into a URL query string + * @returns {string} The URL with the query params added + */ +function serializePathWithQuery (url, queryParams) { if (url.includes('?') || url.includes('#')) { throw new Error('Query params cannot be passed when url already contains "?" or "#".') } @@ -689,7 +694,7 @@ module.exports = { validateHandler, getSocketInfo, isFormDataLike, - buildURL, + serializePathWithQuery, addAbortListener, isValidHTTPToken, isValidHeaderValue, diff --git a/lib/mock/mock-interceptor.js b/lib/mock/mock-interceptor.js index c6b16b35f9c..4304c8fb8ec 100644 --- a/lib/mock/mock-interceptor.js +++ b/lib/mock/mock-interceptor.js @@ -10,7 +10,7 @@ const { kMockDispatch } = require('./mock-symbols') const { InvalidArgumentError } = require('../core/errors') -const { buildURL } = require('../core/util') +const { serializePathWithQuery } = require('../core/util') /** * Defines the scope API for an interceptor reply @@ -72,7 +72,7 @@ class MockInterceptor { // fragments to servers when they retrieve a document, if (typeof opts.path === 'string') { if (opts.query) { - opts.path = buildURL(opts.path, opts.query) + opts.path = serializePathWithQuery(opts.path, opts.query) } else { // Matches https://github.com/nodejs/undici/blob/main/lib/web/fetch/index.js#L1811 const parsedURL = new URL(opts.path, 'data://') diff --git a/lib/mock/mock-utils.js b/lib/mock/mock-utils.js index f3c284d7891..56be2c8a06e 100644 --- a/lib/mock/mock-utils.js +++ b/lib/mock/mock-utils.js @@ -8,7 +8,7 @@ const { kOrigin, kGetNetConnect } = require('./mock-symbols') -const { buildURL } = require('../core/util') +const { serializePathWithQuery } = require('../core/util') const { STATUS_CODES } = require('node:http') const { types: { @@ -126,7 +126,7 @@ function getResponseData (data) { } function getMockDispatch (mockDispatches, key) { - const basePath = key.query ? buildURL(key.path, key.query) : key.path + const basePath = key.query ? serializePathWithQuery(key.path, key.query) : key.path const resolvedPath = typeof basePath === 'string' ? safeUrl(basePath) : basePath // Match path diff --git a/test/node-test/util.js b/test/node-test/util.js index fa1c6c50eb4..0e5f6e3bdee 100644 --- a/test/node-test/util.js +++ b/test/node-test/util.js @@ -92,7 +92,7 @@ test('parseRawHeaders', () => { assert.deepEqual(util.parseRawHeaders(['content-length', 'value', 'content-disposition', 'form-data; name="fieldName"']), ['content-length', 'value', 'content-disposition', 'form-data; name="fieldName"']) }) -test('buildURL', () => { +test('serializePathWithQuery', () => { const tests = [ [{ id: BigInt(123456) }, 'id=123456'], [{ date: new Date() }, 'date='], @@ -111,7 +111,7 @@ test('buildURL', () => { for (const [input, output] of tests) { const expected = `${base}${output ? `?${output}` : output}` - assert.deepEqual(util.buildURL(base, input), expected) + assert.deepEqual(util.serializePathWithQuery(base, input), expected) } })