Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: some minor fixes for pre-request script sdk - INS-3379 #7225

Merged
merged 3 commits into from
Apr 9, 2024
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
43 changes: 43 additions & 0 deletions packages/insomnia-smoke-test/fixtures/pre-request-collection.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -796,3 +796,46 @@ resources:
text: |-
{}
_type: request
- _id: req_89dade2ee9ee42fbb22d588783a9df2
parentId: fld_01de564274824ecaad272330339ea6b2
modified: 1636707449231
created: 1636141014552
url: http://127.0.0.1:4010/echo
name: get sendRequest response through await or callback
description: ""
method: GET
parameters: []
headers:
- name: 'Content-Type'
value: 'application/json'
authentication: {}
metaSortKey: -1636141014553
isPrivate: false
settingStoreCookies: true
settingSendCookies: true
settingDisableRenderRequestBody: false
settingEncodeUrl: true
settingRebuildPath: true
settingFollowRedirects: global
preRequestScript: |-
let respFromCallback;
const respFromAwait = await insomnia.sendRequest(
'http://127.0.0.1:4010/echo',
(err, resp) => {
if (err != null) {
throw err;
} else {
respFromCallback = resp;
}
}
);
insomnia.environment.set('bodyFromAwait', respFromAwait.body);
insomnia.environment.set('bodyFromCallback', respFromCallback.body);
body:
mimeType: "application/json"
text: |-
{
"bodyFromAwait": {{ _.bodyFromAwait }},
"bodyFromCallback": {{ _.bodyFromCallback }}
}
_type: request
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,16 @@ test.describe('pre-request features tests', async () => {
name: 'require / require classes from insomnia-collection module and init them',
expectedBody: {
propJson: {
'_kind': 'Property',
'disabled': false,
'id': 'pid',
'name': 'pname',
disabled: false,
id: 'pid',
name: 'pname',
},
headerJson: {
'_kind': 'Header',
'key': 'headerKey',
'value': 'headerValue',
'id': '',
'name': '',
'type': '',
key: 'headerKey',
value: 'headerValue',
id: '',
name: '',
type: '',
},
},
},
Expand Down Expand Up @@ -126,6 +124,14 @@ test.describe('pre-request features tests', async () => {
events: true,
},
},
{
name: 'get sendRequest response through await or callback',
customVerify: (bodyJson: any) => {
const requestBody = JSON.parse(bodyJson.data);
expect(requestBody.bodyFromAwait.method).toEqual('GET');
expect(requestBody.bodyFromCallback.method).toEqual('GET');
},
},
];

for (let i = 0; i < testCases.length; i++) {
Expand Down
16 changes: 14 additions & 2 deletions packages/insomnia/src/sdk/objects/__tests__/cookies.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from '@jest/globals';

import { Cookie, CookieJar } from '../cookies';
import { Cookie, CookieJar, CookieList } from '../cookies';

describe('test Cookie object', () => {
it('test basic operations', () => {
Expand Down Expand Up @@ -65,7 +65,6 @@ describe('test Cookie object', () => {
Cookie.unparseSingle(cookie1Opt)
).toEqual(expectedCookieString);
});

});

describe('test CookieJar', () => {
Expand Down Expand Up @@ -142,4 +141,17 @@ describe('test CookieJar', () => {
expect(cookie).toBeUndefined();
});
});

it('CookieList operations', () => {
const cookieList = new CookieList(
[
new Cookie({ key: 'c1', value: 'v1' }),
new Cookie({ key: 'c2', value: 'v2' }),
]
);

const upsertedC1 = new Cookie({ key: 'c1', value: 'v1upserted' });
cookieList.upsert(upsertedC1);
expect(cookieList.one('c1')).toEqual(upsertedC1);
});
});
16 changes: 15 additions & 1 deletion packages/insomnia/src/sdk/objects/__tests__/headers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from '@jest/globals';

import { Header } from '../headers';
import { Header, HeaderList } from '../headers';
// import { QueryParam, setUrlParser, Url, UrlMatchPattern } from '../urls';

describe('test Header object', () => {
Expand All @@ -17,4 +17,18 @@ describe('test Header object', () => {
Header.parse(Header.unparse(headerObjs))
).toEqual(headerObjs);
});

it('HeaderList operations', () => {
const headerList = new HeaderList(
undefined,
[
new Header({ key: 'h1', value: 'v1' }),
new Header({ key: 'h2', value: 'v2' }),
]
);

const upserted = new Header({ key: 'h1', value: 'v1upserted' });
headerList.upsert(upserted);
expect(headerList.one('h1')).toEqual(upserted);
});
});
77 changes: 64 additions & 13 deletions packages/insomnia/src/sdk/objects/__tests__/properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ describe('test Property objects', () => {
const pbase = new PropertyBase('my property');

expect(pbase.toJSON()).toEqual({
_kind: 'PropertyBase',
description: 'my property',
});
expect(pbase.toObject()).toEqual({
_kind: 'PropertyBase',
description: 'my property',
});
});
Expand All @@ -26,14 +24,13 @@ describe('test Property objects', () => {
);

expect(prop.toJSON()).toEqual({
_kind: 'Property',
disabled: false,
id: 'real_id',
name: 'real_name',
});
});

it('PropertyList: basic operations', () => {
it('PropertyList: basic operations: add, append, count, all, clear', () => {
const propList = new PropertyList(
{},
undefined,
Expand All @@ -47,26 +44,32 @@ describe('test Property objects', () => {
expect(propList.count()).toBe(3);
expect(propList.all()).toEqual([
{
_kind: 'Property',
disabled: false,
id: 'id1',
name: 'p1',
},
{
_kind: 'Property',
disabled: false,
id: 'id2',
name: 'p2',
},
{
_kind: 'Property',
disabled: false,
id: 'id3',
name: 'p3',
},
]);

propList.clear();
});

it('PropertyList: basic operations: assimilate, each, filter, find', () => {
const propList = new PropertyList<Property>(
Property,
undefined,
[],
);

propList.assimilate(
[
new Property('id1', 'p1'),
Expand Down Expand Up @@ -96,25 +99,48 @@ describe('test Property objects', () => {
{},
) != null
).toBeTruthy();
});

it('PropertyList: basic operations: one, has, indexOf, insert, insertAfter, prepend, populate, map, reduce', () => {
const propList = new PropertyList<Property>(
Property,
undefined,
[
new Property('id1', 'p1'),
new Property('id2', 'p2'),
],
);

expect(propList.one('id1'))
.toEqual(new Property('id1', 'p1'));
expect(propList.has(new Property('id1', 'p1')))
.toBeTruthy();
expect(propList.indexOf(new Property('id1', 'p1')) >= 0).toBeTruthy();
expect(propList.indexOf(new Property('id1', 'p1')) === 0).toBeTruthy();
propList.clear();

propList.insert(new Property('id0', 'p0'), 0);
propList.insertAfter(new Property('id1', 'p1'), 1);
propList.prepend(new Property('id-1', 'p-1'));
propList.populate([new Property('id2', 'p2')]);
});

it('PropertyList: basic operations: one, has, indexOf, insert, insertAfter, prepend, populate, map, reduce', () => {
const propList = new PropertyList<Property>(
Property,
undefined,
[
new Property('id0', 'p0'),
new Property('id1', 'p1'),
new Property('id2', 'p2'),
],
);

expect(
propList.map(
prop => prop.id,
{},
)
).toEqual([
'id-1',
'id0',
'id1',
'id2',
Expand All @@ -125,23 +151,48 @@ describe('test Property objects', () => {
'',
{},
),
).toEqual('id-1id0id1id2');
).toEqual('id0id1id2');
});

it('PropertyList: basic operations: remove, count, repopulate, toString, get, one, idx, upsert', () => {
const propList = new PropertyList<Property>(
Property,
undefined,
[
new Property('id0', 'p0'),
new Property('id1', 'p1'),
new Property('id2', 'p2'),
],
);

propList.remove(
prop => prop.id === 'id-1',
prop => prop.id === 'id0',
{},
);
expect(
propList.count(),
).toEqual(3);
).toEqual(2);

propList.repopulate([
new Property('id1', 'p1'),
new Property('id2', 'p2'),
]);

expect(propList.toString()).toEqual(
'[{"_kind":"Property","id":"id1","name":"p1","disabled":false}; {"_kind":"Property","id":"id2","name":"p2","disabled":false}]',
'[{"id":"id1","name":"p1","disabled":false}; {"id":"id2","name":"p2","disabled":false}]',
);

const expectedP1 = new Property('id1', 'p1');
const getP1 = propList.get('id1');
const oneP1 = propList.one('id1');
expect(getP1).toEqual(expectedP1);
expect(oneP1).toEqual(expectedP1);

const idxP1 = propList.idx(0);
expect(idxP1).toEqual(expectedP1);

const upsertedP2 = new Property('id2', 'upsertedP2');
propList.upsert(upsertedP2);
expect(propList.one('id2')).toEqual(upsertedP2);
});
});
6 changes: 4 additions & 2 deletions packages/insomnia/src/sdk/objects/__tests__/urls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('test Url object', () => {
query: [
new QueryParam({ key: 'key1', value: 'value1' }),
new QueryParam({ key: 'key2', value: 'value2' }),
new QueryParam({ key: 'key3', value: 'value3' }),
],
variables: [
new Variable({ key: 'varKey', value: 'varValue' }),
Expand All @@ -58,14 +59,15 @@ describe('test Url object', () => {

expect(url.getHost()).toEqual('hostvalue.com');
expect(url.getPath()).toEqual('/pathLevel1/pathLevel2');
expect(url.getQueryString()).toEqual('key1=value1&key2=value2');
expect(url.getPathWithQuery()).toEqual('/pathLevel1/pathLevel2?key1=value1&key2=value2');
expect(url.getQueryString()).toEqual('key1=value1&key2=value2&key3=value3');
expect(url.getPathWithQuery()).toEqual('/pathLevel1/pathLevel2?key1=value1&key2=value2&key3=value3');
expect(url.getRemote(true)).toEqual('hostvalue.com:777');
expect(url.getRemote(false)).toEqual('hostvalue.com:777'); // TODO: add more cases

url.removeQueryParams([
new QueryParam({ key: 'key1', value: 'value1' }),
]);
url.removeQueryParams('key3');
expect(url.getQueryString()).toEqual('key2=value2');
expect(url.toString()).toEqual('https://usernameValue:passwordValue@hostvalue.com:777/pathLevel1/pathLevel2?key2=value2#hashValue');

Expand Down
16 changes: 15 additions & 1 deletion packages/insomnia/src/sdk/objects/__tests__/variables.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from '@jest/globals';

import { Variable } from '../variables';
import { Variable, VariableList } from '../variables';

describe('test Variables object', () => {
it('test basic operations', () => {
Expand All @@ -19,4 +19,18 @@ describe('test Variables object', () => {
expect(variable.get()).toBe('value2');

});

it('VariableList operations', () => {
const varList = new VariableList(
undefined,
[
new Variable({ key: 'h1', value: 'v1' }),
new Variable({ key: 'h2', value: 'v2' }),
]
);

const upserted = new Variable({ key: 'h1', value: 'v1upserted' });
varList.upsert(upserted);
expect(varList.one('h1')).toEqual(upserted);
});
});
5 changes: 4 additions & 1 deletion packages/insomnia/src/sdk/objects/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export class Cookie extends Property {
this.cookie = cookie;
}

static _index = 'key';

static isCookie(obj: Property) {
return '_kind' in obj && obj._kind === 'Cookie';
}
Expand Down Expand Up @@ -134,7 +136,7 @@ export class Cookie extends Property {
return this.cookie.toJSON().value;
};

key = () => {
get key() {
return this.cookie.toJSON().key;
};

Expand Down Expand Up @@ -207,6 +209,7 @@ export class CookieObject extends CookieList {
super(cookies);
const scriptCookieJar = cookieJar ? new CookieJar(cookieJar.name, cookies) : new CookieJar('', []);
this.cookieJar = scriptCookieJar;
this.typeClass = Cookie;
}

jar() {
Expand Down
Loading
Loading