Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions deps/undici/src/docs/api/MockAgent.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ for await (const data of result2.body) {
console.log('data', data.toString('utf8')) // data hello
}
```
#### Example - Mock different requests within the same file
```js
const { MockAgent, setGlobalDispatcher } = require('undici');
const agent = new MockAgent();
agent.disableNetConnect();
setGlobalDispatcher(agent);
describe('Test', () => {
it('200', async () => {
const mockAgent = agent.get('http://test.com');
// your test
});
it('200', async () => {
const mockAgent = agent.get('http://testing.com');
// your test
});
});
```

#### Example - Mocked request with query body, headers and trailers

Expand Down
7 changes: 6 additions & 1 deletion deps/undici/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ function makeDispatcher (fn) {
throw new InvalidArgumentError('invalid opts.path')
}

url = new URL(opts.path, util.parseOrigin(url))
let path = opts.path
if (!opts.path.startsWith('/')) {
path = `/${path}`
}

url = new URL(util.parseOrigin(url).origin + path)
} else {
if (!opts) {
opts = typeof url === 'object' ? url : {}
Expand Down
3 changes: 2 additions & 1 deletion deps/undici/src/lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ function processHeader (request, key, val) {
} else if (
request.contentType === null &&
key.length === 12 &&
key.toLowerCase() === 'content-type'
key.toLowerCase() === 'content-type' &&
headerCharRegex.exec(val) === null
) {
request.contentType = val
request.headers += `${key}: ${val}\r\n`
Expand Down
17 changes: 14 additions & 3 deletions deps/undici/src/lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,25 @@ function parseURL (url) {
const port = url.port != null
? url.port
: (url.protocol === 'https:' ? 443 : 80)
const origin = url.origin != null
let origin = url.origin != null
? url.origin
: `${url.protocol}//${url.hostname}:${port}`
const path = url.path != null
let path = url.path != null
? url.path
: `${url.pathname || ''}${url.search || ''}`

url = new URL(path, origin)
if (origin.endsWith('/')) {
origin = origin.substring(0, origin.length - 1)
}

if (path && !path.startsWith('/')) {
path = `/${path}`
}
// new URL(path, origin) is unsafe when `path` contains an absolute URL
// From https://developer.mozilla.org/en-US/docs/Web/API/URL/URL:
// If first parameter is a relative URL, second param is required, and will be used as the base URL.
// If first parameter is an absolute URL, a given second param will be ignored.
url = new URL(origin + path)
}

return url
Expand Down
15 changes: 9 additions & 6 deletions deps/undici/src/lib/fetch/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,6 @@ webidl.converters.DOMString = function (V, opts = {}) {
return String(V)
}

// Check for 0 or more characters outside of the latin1 range.
// eslint-disable-next-line no-control-regex
const isLatin1 = /^[\u0000-\u00ff]{0,}$/

// https://webidl.spec.whatwg.org/#es-ByteString
webidl.converters.ByteString = function (V) {
// 1. Let x be ? ToString(V).
Expand All @@ -400,8 +396,15 @@ webidl.converters.ByteString = function (V) {

// 2. If the value of any element of x is greater than
// 255, then throw a TypeError.
if (!isLatin1.test(x)) {
throw new TypeError('Argument is not a ByteString')
for (let index = 0; index < x.length; index++) {
const charCode = x.charCodeAt(index)

if (charCode > 255) {
throw new TypeError(
'Cannot convert argument to a ByteString because the character at' +
`index ${index} has a value of ${charCode} which is greater than 255.`
)
}
}

// 3. Return an IDL ByteString value whose length is the
Expand Down
2 changes: 1 addition & 1 deletion deps/undici/src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "undici",
"version": "5.8.1",
"version": "5.8.2",
"description": "An HTTP/1.1 client, written from scratch for Node.js",
"homepage": "https://undici.nodejs.org",
"bugs": {
Expand Down
22 changes: 15 additions & 7 deletions deps/undici/undici.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,15 @@ var require_util = __commonJS({
}
if (!(url instanceof URL)) {
const port = url.port != null ? url.port : url.protocol === "https:" ? 443 : 80;
const origin = url.origin != null ? url.origin : `${url.protocol}//${url.hostname}:${port}`;
const path = url.path != null ? url.path : `${url.pathname || ""}${url.search || ""}`;
url = new URL(path, origin);
let origin = url.origin != null ? url.origin : `${url.protocol}//${url.hostname}:${port}`;
let path = url.path != null ? url.path : `${url.pathname || ""}${url.search || ""}`;
if (origin.endsWith("/")) {
origin = origin.substring(0, origin.length - 1);
}
if (path && !path.startsWith("/")) {
path = `/${path}`;
}
url = new URL(origin + path);
}
return url;
}
Expand Down Expand Up @@ -1281,11 +1287,13 @@ var require_webidl = __commonJS({
}
return String(V);
};
var isLatin1 = /^[\u0000-\u00ff]{0,}$/;
webidl.converters.ByteString = function(V) {
const x = webidl.converters.DOMString(V);
if (!isLatin1.test(x)) {
throw new TypeError("Argument is not a ByteString");
for (let index = 0; index < x.length; index++) {
const charCode = x.charCodeAt(index);
if (charCode > 255) {
throw new TypeError(`Cannot convert argument to a ByteString because the character atindex ${index} has a value of ${charCode} which is greater than 255.`);
}
}
return x;
};
Expand Down Expand Up @@ -2580,7 +2588,7 @@ var require_request = __commonJS({
if (!Number.isFinite(request.contentLength)) {
throw new InvalidArgumentError("invalid content-length header");
}
} else if (request.contentType === null && key.length === 12 && key.toLowerCase() === "content-type") {
} else if (request.contentType === null && key.length === 12 && key.toLowerCase() === "content-type" && headerCharRegex.exec(val) === null) {
request.contentType = val;
request.headers += `${key}: ${val}\r
`;
Expand Down