test: add test for certificate

This commit is contained in:
George He
2024-01-15 16:41:52 +08:00
parent 9c81af92ef
commit 4e5602b54d
4 changed files with 67 additions and 6 deletions

15
package-lock.json generated
View File

@@ -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",

View File

@@ -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",

View File

@@ -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');
});
});

View File

@@ -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] };