forked from Kong/insomnia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable Request and Response in pre-request scripting [INS-3379] (…
…Kong#7128) * feat: enable ProxyConfig in pre-request scripting * feat: enable RequestAuth for pre-request scripting * feat: enable Cookie in pre-request scripting * feat: enable Certificate in pre-request script * feat: enable Request and Response in pre-request scripting
- Loading branch information
Showing
16 changed files
with
2,065 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
|
||
import { RequestAuth } from '../auth'; | ||
import { Variable, VariableList } from '../variables'; | ||
|
||
const varListToObject = (obj: VariableList<Variable> | undefined) => { | ||
if (!obj) { | ||
return undefined; | ||
} | ||
|
||
return obj.map( | ||
(optVar: Variable) => ({ | ||
// type: 'any', // TODO: fix type | ||
key: optVar.key, | ||
value: optVar.value, | ||
}), | ||
{} | ||
); | ||
}; | ||
|
||
describe('test sdk objects', () => { | ||
it('test RequestAuth methods', () => { | ||
expect(RequestAuth.isValidType('noauth')).toBeTruthy(); | ||
|
||
const basicAuthOptions = { | ||
type: 'basic', | ||
basic: [ | ||
{ key: 'username', value: 'user1' }, | ||
{ key: 'password', value: 'pwd1' }, | ||
], | ||
}; | ||
|
||
const authObj = new RequestAuth(basicAuthOptions); | ||
|
||
const basicAuthOptsFromAuth = varListToObject(authObj.parameters()); | ||
expect(basicAuthOptsFromAuth).toEqual(basicAuthOptions.basic); | ||
|
||
const basicAuthOptions2 = { | ||
type: 'basic', | ||
basic: [ | ||
{ key: 'username', value: 'user2' }, | ||
{ key: 'password', value: 'pwd2' }, | ||
], | ||
}; | ||
const bearerAuthOptions = { | ||
type: 'bearer', | ||
bearer: [ | ||
{ key: 'token', value: 'mytoken' }, | ||
], | ||
}; | ||
|
||
authObj.update(basicAuthOptions2); | ||
const basicAuthOpt2FromAuth = varListToObject(authObj.parameters()); | ||
expect(basicAuthOpt2FromAuth).toEqual(basicAuthOptions2.basic); | ||
|
||
authObj.use('bearer', bearerAuthOptions); | ||
const beareerAuthOptFromAuth = varListToObject(authObj.parameters()); | ||
expect(beareerAuthOptFromAuth).toEqual(bearerAuthOptions.bearer); | ||
|
||
authObj.clear('bearer'); | ||
expect(authObj.parameters()).toBeUndefined(); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
packages/insomnia/src/sdk/objects/__tests__/certificates.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); |
68 changes: 68 additions & 0 deletions
68
packages/insomnia/src/sdk/objects/__tests__/cookies.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
|
||
import { Cookie } from '../cookies'; | ||
|
||
describe('test Cookie object', () => { | ||
it('test basic operations', () => { | ||
const cookieStr1 = 'key=value; Domain=inso.com; Path=/; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Max-Age=0;Secure;HttpOnly;HostOnly;Session'; | ||
|
||
expect( | ||
Cookie.parse(cookieStr1) | ||
).toEqual({ | ||
key: 'key', | ||
value: 'value', | ||
domain: 'inso.com', | ||
expires: new Date('2015-10-21T07:28:00.000Z'), | ||
maxAge: '0', | ||
path: '/', | ||
secure: true, | ||
httpOnly: true, | ||
hostOnly: true, | ||
session: true, | ||
extensions: [], | ||
}); | ||
|
||
const cookie1Opt = { | ||
key: 'myCookie', | ||
value: 'myCookie', | ||
expires: '01 Jan 1970 00:00:01 GMT', | ||
maxAge: '7', | ||
domain: 'domain.com', | ||
path: '/', | ||
secure: true, | ||
httpOnly: true, | ||
hostOnly: true, | ||
session: true, | ||
extensions: [{ key: 'Ext', value: 'ExtValue' }], | ||
}; | ||
const cookie1 = new Cookie(cookie1Opt); | ||
|
||
const expectedCookieString = 'myCookie=myCookie; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Max-Age=7; Path=/; Secure; HttpOnly; HostOnly; Ext=ExtValue'; | ||
|
||
expect(cookie1.toString()).toEqual(expectedCookieString); | ||
expect(Cookie.stringify(cookie1)).toEqual(expectedCookieString); | ||
|
||
const cookie2 = new Cookie(expectedCookieString); | ||
expect(cookie2.toString()).toEqual(expectedCookieString); | ||
expect(Cookie.stringify(cookie2)).toEqual(expectedCookieString); | ||
|
||
const c1 = new Cookie({ | ||
key: 'c1', | ||
value: 'c1', | ||
maxAge: '1', | ||
}); | ||
const c2 = new Cookie({ | ||
key: 'c2', | ||
value: 'c2', | ||
maxAge: '2', | ||
}); | ||
const CookieListStr = Cookie.unparse([c1, c2]); | ||
expect(CookieListStr).toEqual( | ||
'c1=c1; Max-Age=1; c2=c2; Max-Age=2' | ||
); | ||
|
||
expect( | ||
Cookie.unparseSingle(cookie1Opt) | ||
).toEqual(expectedCookieString); | ||
}); | ||
}); |
57 changes: 57 additions & 0 deletions
57
packages/insomnia/src/sdk/objects/__tests__/proxy-configs.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
|
||
import { ProxyConfig, ProxyConfigList } from '../proxy-configs'; | ||
import { Url } from '../urls'; | ||
|
||
describe('test ProxyConfig object', () => { | ||
it('test basic operations', () => { | ||
|
||
const proxyConfig = new ProxyConfig({ | ||
match: 'http+https://*.example.com:80/*', | ||
host: 'proxy.com', | ||
port: 8080, | ||
tunnel: true, | ||
disabled: false, | ||
authenticate: true, | ||
username: 'proxy_username', | ||
password: 'proxy_password', | ||
}); | ||
|
||
expect( | ||
proxyConfig.getProtocols() | ||
).toEqual( | ||
['http', 'https'] | ||
); | ||
|
||
proxyConfig.updateProtocols(['http']); | ||
expect( | ||
proxyConfig.getProtocols() | ||
).toEqual( | ||
['http'] | ||
); | ||
|
||
expect(proxyConfig.getProxyUrl()).toEqual( | ||
'http://proxy_username:proxy_password@proxy.com:8080' | ||
); | ||
|
||
expect( | ||
proxyConfig.test('http://a.example.com:80/a') | ||
).toBeTruthy(); | ||
|
||
const configList = new ProxyConfigList<ProxyConfig>(undefined, []); | ||
configList.add(proxyConfig); | ||
configList.add(new ProxyConfig({ | ||
match: 'https://*.example.com:80/*', | ||
host: 'proxy.com', | ||
port: 8080, | ||
tunnel: true, | ||
disabled: false, | ||
authenticate: true, | ||
username: 'proxy_username', | ||
password: 'proxy_password', | ||
})); | ||
|
||
const matchedProxyConfigDef = configList.resolve(new Url('http://sub.example.com:80/path')); | ||
expect(matchedProxyConfigDef?.host).toEqual('proxy.com'); | ||
}); | ||
}); |
Oops, something went wrong.