diff --git a/package-lock.json b/package-lock.json index 9559363c66..6b2c037fed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25001,6 +25001,7 @@ "oauth-1.0a": "^2.2.6", "papaparse": "^5.4.1", "prettier": "2.4.1", + "qs": "^6.11.2", "query-string": "^8.1.0", "shell-quote": "^1.8.1", "swagger-ui-dist": "5.0.0-alpha.6", @@ -25891,6 +25892,20 @@ "node": ">=12" } }, + "packages/insomnia/node_modules/qs": { + "version": "6.11.2", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.2.tgz", + "integrity": "sha512-tDNIz22aBzCDxLtVH++VnTfzxlfeK5CbqohpSqpJgj1Wg/cQbStNAz3NuqCs5vV+pjBsK4x4pN9HlVh7rcYRiA==", + "dependencies": { + "side-channel": "^1.0.4" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "packages/insomnia/node_modules/rollup": { "version": "3.26.3", "resolved": "https://registry.npmjs.org/rollup/-/rollup-3.26.3.tgz", diff --git a/packages/insomnia/package.json b/packages/insomnia/package.json index 35af0a501a..84ddd7867a 100644 --- a/packages/insomnia/package.json +++ b/packages/insomnia/package.json @@ -81,6 +81,7 @@ "oauth-1.0a": "^2.2.6", "papaparse": "^5.4.1", "prettier": "2.4.1", + "qs": "^6.11.2", "query-string": "^8.1.0", "shell-quote": "^1.8.1", "swagger-ui-dist": "5.0.0-alpha.6", diff --git a/packages/insomnia/src/renderers/hidden-browser-window/sdk-objects/__tests__/certificates.test.ts b/packages/insomnia/src/renderers/hidden-browser-window/sdk-objects/__tests__/certificates.test.ts new file mode 100644 index 0000000000..afe04824cc --- /dev/null +++ b/packages/insomnia/src/renderers/hidden-browser-window/sdk-objects/__tests__/certificates.test.ts @@ -0,0 +1,41 @@ +import url from 'node:url'; + +import { describe, expect, it } from '@jest/globals'; + +import { Certificate } from '../certificates'; +import { setUrlParser } from '../urls'; + +describe('test Certificate object', () => { + it('test methods', () => { + // make URL work in Node.js + setUrlParser(url.URL); + + const cert = new Certificate({ + name: 'Certificate for example.com', + matches: ['https://example.com'], + key: { src: '/User/path/to/certificate/key' }, + cert: { src: '/User/path/to/certificate' }, + passphrase: 'iampassphrase', + }); + + [ + 'https://example.com', + 'https://example.com/subdomain', + ].forEach(testCase => { + expect(cert.canApplyTo(testCase)).toBeTruthy(); + }); + + cert.update({ + name: 'Certificate for api.com', + matches: ['https://api.com'], + key: { src: '/User/path/to/certificate/key' }, + cert: { src: '/User/path/to/certificate' }, + passphrase: 'iampassphrase', + }); + + expect(cert.name).toEqual('Certificate for api.com'); + expect(cert.key).toEqual({ src: '/User/path/to/certificate/key' }); + expect(cert.cert).toEqual({ src: '/User/path/to/certificate' }); + expect(cert.passphrase).toEqual('iampassphrase'); + }); +}); diff --git a/packages/insomnia/src/renderers/hidden-browser-window/sdk-objects/urls.ts b/packages/insomnia/src/renderers/hidden-browser-window/sdk-objects/urls.ts index 9450994b64..2765c3fb83 100644 --- a/packages/insomnia/src/renderers/hidden-browser-window/sdk-objects/urls.ts +++ b/packages/insomnia/src/renderers/hidden-browser-window/sdk-objects/urls.ts @@ -1,4 +1,4 @@ -import queryString from 'query-string'; +import qs from 'qs'; import { Property, PropertyBase, PropertyList } from './base'; import { Variable, VariableList } from './variables'; @@ -47,6 +47,11 @@ import { Variable, VariableList } from './variables'; // } // } +let urlParser = URL; +export function setUrlParser(urlAlternative: any) { + urlParser = urlAlternative; +} + export class QueryParam extends Property { key: string; value: string; @@ -77,13 +82,13 @@ export class QueryParam extends Property { static parse(queryStr: string) { // this may not always be executed in the browser - return queryString.parse(queryStr); + return qs.parse(queryStr); } // eslint-disable-next-line @typescript-eslint/no-unused-vars static parseSingle(param: string, _idx?: number, _all?: string[]) { // it seems that _idx and _all are not useful - return queryString.parse(param); + return qs.parse(param); } @@ -198,13 +203,12 @@ export class Url extends PropertyBase { } static parse(urlStr: string): UrlObject | undefined { - // TODO: URL requires explicit requiring in Node.js - if (!URL.canParse(urlStr)) { + if (!urlParser.canParse(urlStr)) { console.error(`invalid URL string ${urlStr}`); return undefined; } - const url = new URL(urlStr); + const url = new urlParser(urlStr); const query = Array.from(url.searchParams.entries()) .map((kv: [string, string]) => { return { key: kv[0], value: kv[1] };