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

feat: enable cookieJar manipulation in pre-request script - INS-3379 #7228

Merged
merged 3 commits into from
Apr 3, 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
3 changes: 3 additions & 0 deletions packages/insomnia/src/hidden-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'node:fs';
import * as _ from 'lodash';

import { invariant } from '../src/utils/invariant';
import { mergeCookieJar } from './sdk/objects/cookies';
import { initInsomniaObject, InsomniaObject } from './sdk/objects/insomnia';
import { RequestContext } from './sdk/objects/interfaces';
import { mergeClientCertificates, mergeRequests, mergeSettings } from './sdk/objects/request';
Expand Down Expand Up @@ -83,6 +84,7 @@ const runPreRequestScript = async (
const updatedRequest = mergeRequests(context.request, mutatedContextObject.request);
const updatedSettings = mergeSettings(context.settings, mutatedContextObject.request);
const updatedCertificates = mergeClientCertificates(context.clientCertificates, mutatedContextObject.request);
const updatedCookieJar = mergeCookieJar(context.cookieJar, mutatedContextObject.cookieJar);

await fs.promises.writeFile(context.timelinePath, log.join('\n'));

Expand All @@ -96,5 +98,6 @@ const runPreRequestScript = async (
request: updatedRequest,
settings: updatedSettings,
clientCertificates: updatedCertificates,
cookieJar: updatedCookieJar,
};
};
2 changes: 2 additions & 0 deletions packages/insomnia/src/network/cancellation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CurlRequestOptions, CurlRequestOutput } from '../main/network/libcurl-promise';
import { CookieJar } from '../models/cookie-jar';
import { Request } from '../models/request';
import { RequestContext } from '../sdk/objects/interfaces';

Expand Down Expand Up @@ -33,6 +34,7 @@ export const cancellableRunPreRequestScript = async (options: { script: string;
request: Request;
environment: object;
baseEnvironment: object;
cookieJar: CookieJar;
};
} catch (err) {
if (err.name === 'AbortError') {
Expand Down
5 changes: 5 additions & 0 deletions packages/insomnia/src/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { HeaderResult, ResponsePatch } from '../main/network/libcurl-promis
import * as models from '../models';
import { CaCertificate } from '../models/ca-certificate';
import { ClientCertificate } from '../models/client-certificate';
import { CookieJar } from '../models/cookie-jar';
import { Environment } from '../models/environment';
import type { Request, RequestAuthentication, RequestParameter } from '../models/request';
import type { Settings } from '../models/settings';
Expand Down Expand Up @@ -80,6 +81,7 @@ export const tryToExecutePreRequestScript = async (
responseId: string,
baseEnvironment: Environment,
clientCertificates: ClientCertificate[],
cookieJar: CookieJar,
) => {
if (!request.preRequestScript) {
return {
Expand Down Expand Up @@ -111,6 +113,7 @@ export const tryToExecutePreRequestScript = async (
baseEnvironment: baseEnvironment?.data || {},
clientCertificates,
settings,
cookieJar,
},
});
const output = await Promise.race([timeoutPromise, preRequestPromise]) as {
Expand All @@ -119,6 +122,7 @@ export const tryToExecutePreRequestScript = async (
baseEnvironment: Record<string, any>;
settings: Settings;
clientCertificates: ClientCertificate[];
cookieJar: CookieJar;
};
console.log('[network] Pre-request script succeeded', output);

Expand All @@ -144,6 +148,7 @@ export const tryToExecutePreRequestScript = async (
baseEnvironment,
settings: output.settings,
clientCertificates: output.clientCertificates,
cookieJar: output.cookieJar,
};
} catch (err) {
await fs.promises.appendFile(timelinePath, JSON.stringify({ value: err.message, name: 'Text', timestamp: Date.now() }) + '\n');
Expand Down
79 changes: 78 additions & 1 deletion 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 } from '../cookies';
import { Cookie, CookieJar } from '../cookies';

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

});

describe('test CookieJar', () => {
it('basic operations', () => {
const cookieOptBase = {
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 jar = new CookieJar(
'my jar',
[
new Cookie({ ...cookieOptBase, key: 'c1', value: 'c1' }),
new Cookie({ ...cookieOptBase, key: 'c2', value: 'c2' }),
],
);

jar.set('domain.com', 'c1', { ...cookieOptBase, key: 'c1', value: 'c1Updated' }, (error, cookie) => {
expect(error).toBeUndefined();
expect(cookie?.toJSON()).toEqual(
new Cookie({ ...cookieOptBase, key: 'c1', value: 'c1Updated' }).toJSON()
);
});

jar.set('domain2.com', 'c2', { ...cookieOptBase, key: 'c2', value: 'c2' }, (error, cookie) => {
expect(error).toBeUndefined();
expect(cookie?.toJSON()).toEqual(
new Cookie({ ...cookieOptBase, key: 'c2', value: 'c2' }).toJSON()
);
});

jar.get('domain.com', 'c1', (err, cookie) => {
expect(err).toBeUndefined();
expect(
cookie?.toJSON(),
).toEqual(
new Cookie({ ...cookieOptBase, key: 'c1', value: 'c1Updated' }).toJSON(),
);
});

jar.get('domain2.com', 'c2', (err, cookie) => {
expect(err).toBeUndefined();
expect(
cookie?.toJSON(),
).toEqual(
new Cookie({ ...cookieOptBase, key: 'c2', value: 'c2' }).toJSON(),
);
});

jar.unset('domain.com', 'c1', err => {
expect(err).toBeUndefined();
});

jar.get('domain.com', 'c1', (err, cookie) => {
expect(err).toBeUndefined();
expect(cookie).toBeUndefined();
});

jar.clear('domain2.com', err => {
expect(err).toBeUndefined();
});

jar.get('domain2.com', 'c2', (err, cookie) => {
expect(err).toBeUndefined();
expect(cookie).toBeUndefined();
});
});
});
Loading
Loading