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: integrate post-request script to the engine - INS-3785,INS-3786 #7329

Merged
merged 10 commits into from
May 15, 2024
46 changes: 35 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/insomnia-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "insomnia-sdk",
"version": "9.0.0",
"version": "0.1.0",
"description": "",
"main": "src/objects/index.ts",
"types": "src/objects/index.ts",
Expand All @@ -20,13 +20,15 @@
},
"homepage": "https://github.com/Kong/insomnia#readme",
"dependencies": {
"@types/deep-equal": "^1.0.4",
"@types/tv4": "^1.2.33",
"@types/xml2js": "^0.4.14",
"ajv": "^8.12.0",
"chai": "^5.1.0",
"cheerio": "^1.0.0-rc.12",
"crypto-js": "^4.2.0",
"csv-parse": "^5.5.5",
"deep-equal": "^2.2.3",
"lodash": "^4.17.21",
"moment": "^2.30.1",
"tv4": "^1.3.0",
Expand Down
7 changes: 7 additions & 0 deletions packages/insomnia-sdk/src/objects/__tests__/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,12 @@ describe('test request and response objects', () => {
mimeFormat: '',
mimeType: 'text/plain',
});

// extended assertion chains
resp.to.have.status(200);
resp.to.have.status('OK');
resp.to.have.header('header1');
resp.to.have.jsonBody({ 'key': 888 });
resp.to.have.body('{"key": 888}');
});
});
11 changes: 10 additions & 1 deletion packages/insomnia-sdk/src/objects/insomnia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { unsupportedError } from './properties';
import { Request as ScriptRequest, RequestOptions, toScriptRequestBody } from './request';
import { RequestInfo } from './request-info';
import { Response as ScriptResponse } from './response';
import { readBodyFromPath, toScriptResponse } from './response';
import { sendRequest } from './send-request';
import { test } from './test';
import { toUrlObject } from './urls';
Expand All @@ -23,6 +24,7 @@ export class InsomniaObject {
public request: ScriptRequest;
public cookies: CookieObject;
public info: RequestInfo;
public response?: ScriptResponse;

private clientCertificates: ClientCertificate[];
private _expect = expect;
Expand All @@ -47,6 +49,7 @@ export class InsomniaObject {
clientCertificates: ClientCertificate[];
cookies: CookieObject;
requestInfo: RequestInfo;
response?: ScriptResponse;
},
log: (...msgs: any[]) => void,
) {
Expand All @@ -57,6 +60,7 @@ export class InsomniaObject {
this._iterationData = rawObj.iterationData;
this.variables = rawObj.variables;
this.cookies = rawObj.cookies;
this.response = rawObj.response;

this.info = rawObj.requestInfo;
this.request = rawObj.request;
Expand Down Expand Up @@ -108,11 +112,12 @@ export class InsomniaObject {
clientCertificates: this.clientCertificates,
cookieJar: this.cookies.jar().toInsomniaCookieJar(),
info: this.info.toObject(),
response: this.response ? this.response.toObject() : undefined,
};
};
}

export function initInsomniaObject(
export async function initInsomniaObject(
rawObj: RequestContext,
log: (...args: any[]) => void,
) {
Expand Down Expand Up @@ -206,6 +211,9 @@ export function initInsomniaObject(
};
const request = new ScriptRequest(reqOpt);

const responseBody = await readBodyFromPath(rawObj.response);
const response = rawObj.response ? toScriptResponse(request, rawObj.response, responseBody) : undefined;

return new InsomniaObject(
{
globals,
Expand All @@ -218,6 +226,7 @@ export function initInsomniaObject(
clientCertificates: rawObj.clientCertificates,
cookies,
requestInfo,
response,
},
log,
);
Expand Down
3 changes: 3 additions & 0 deletions packages/insomnia-sdk/src/objects/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CookieJar as InsomniaCookieJar } from 'insomnia/src//models/cookie-jar'
import { ClientCertificate } from 'insomnia/src/models/client-certificate';
import type { Request } from 'insomnia/src/models/request';
import { Settings } from 'insomnia/src/models/settings';
import { sendCurlAndWriteTimelineError, sendCurlAndWriteTimelineResponse } from 'insomnia/src/network/network';

export interface RequestContext {
request: Request;
Expand All @@ -17,4 +18,6 @@ export interface RequestContext {
settings: Settings;
clientCertificates: ClientCertificate[];
cookieJar: InsomniaCookieJar;
// only for the post-request script
response?: sendCurlAndWriteTimelineResponse | sendCurlAndWriteTimelineError;
}
2 changes: 1 addition & 1 deletion packages/insomnia-sdk/src/objects/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ export function mergeRequestBody(

try {
const textContent = updatedReqBody?.raw ? updatedReqBody?.raw :
updatedReqBody?.graphql ? JSON.stringify(updatedReqBody?.graphql) : '';
updatedReqBody?.graphql ? JSON.stringify(updatedReqBody?.graphql) : undefined;

return {
mimeType: mimeType,
Expand Down