Skip to content

Commit

Permalink
fix: add _index for making method behavior (such as upsert) consisten…
Browse files Browse the repository at this point in the history
…t with existing ones
  • Loading branch information
ihexxa committed Apr 3, 2024
1 parent 25c30cf commit 1ebabdc
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ resources:
modified: 1636707449231
created: 1636141014552
url: http://127.0.0.1:4010/echo
name: sendRequest simple mode
name: get sendRequest response through await or callback
description: ""
method: GET
parameters: []
Expand Down
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
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
7 changes: 2 additions & 5 deletions packages/insomnia/src/sdk/objects/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export class Header extends Property {
}
}

static _index: string = 'key';

static create(input?: { key: string; value: string } | string, name?: string): Header {
return new Header(input || { key: '', value: '' }, name);
}
Expand Down Expand Up @@ -113,11 +115,6 @@ export class HeaderList<T extends Header> extends PropertyList<T> {
throw unsupportedError('eachParent');
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
toObject(_excludeDisabled?: boolean, _caseSensitive?: boolean, _multiValue?: boolean, _sanitizeKeys?: boolean) {
throw unsupportedError('toObject');
}

contentSize(): number {
return this.list
.map(header => header.toString())
Expand Down
Loading

0 comments on commit 1ebabdc

Please sign in to comment.