From bb947a4eb00410f39d14bd4dd36ea52762ee69cc Mon Sep 17 00:00:00 2001 From: George He Date: Wed, 8 Jan 2025 15:01:37 +0800 Subject: [PATCH] fix: add basic support for insomnia.request.size() --- .../src/objects/__tests__/request.test.ts | 38 ++++++++++++++++++- packages/insomnia-sdk/src/objects/request.ts | 28 ++++++++++++-- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/packages/insomnia-sdk/src/objects/__tests__/request.test.ts b/packages/insomnia-sdk/src/objects/__tests__/request.test.ts index af87ae49ae3..193452c62e2 100644 --- a/packages/insomnia-sdk/src/objects/__tests__/request.test.ts +++ b/packages/insomnia-sdk/src/objects/__tests__/request.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from 'vitest'; -import { mergeRequestBody, Request, RequestBody, toScriptRequestBody } from '../request'; +import { Header, HeaderList } from '../headers'; +import { calculateRequestSize, mergeRequestBody, Request, RequestBody, RequestBodyOptions, toScriptRequestBody } from '../request'; describe('test request and response objects', () => { it('test RequestBody methods', () => { @@ -115,4 +116,39 @@ describe('test request and response objects', () => { expect(mergeRequestBody(scriptReqBody, originalReqBody)).toEqual(originalReqBody); }); }); + + const reqBodyTestCases: { body: RequestBodyOptions; headers: HeaderList
; expectedTotal: number }[] = [ + { + body: { + mode: 'raw', + raw: '1', + }, + headers: new HeaderList
(undefined, []), + expectedTotal: 1, + }, + { + body: { + mode: 'raw', + raw: '😎', + }, + headers: new HeaderList
(undefined, []), + expectedTotal: 4, + }, + { + body: { + mode: 'raw', + raw: '睡', + }, + headers: new HeaderList
(undefined, []), + expectedTotal: 3, + }, + ]; + + reqBodyTestCases.forEach(({ body, headers, expectedTotal }) => { + it(`test calculateRequestSize: ${body.raw}`, () => { + const reqSize = calculateRequestSize(new RequestBody(body), headers); + + expect(reqSize.total).toEqual(expectedTotal); + }); + }); }); diff --git a/packages/insomnia-sdk/src/objects/request.ts b/packages/insomnia-sdk/src/objects/request.ts index 019bc4b927d..0f14e564802 100644 --- a/packages/insomnia-sdk/src/objects/request.ts +++ b/packages/insomnia-sdk/src/objects/request.ts @@ -151,6 +151,10 @@ export class RequestBody extends PropertyBase { } override toString() { + if (this.mode === undefined) { + return ''; + } + try { switch (this.mode) { case 'formdata': @@ -375,9 +379,9 @@ export class Request extends Property { this.url.removeQueryParams(params); } - // TODO: - // size(): RequestSize { - // } + size(): RequestSize { + return calculateRequestSize(this.body, this.headers); + } override toJSON() { return { @@ -657,3 +661,21 @@ export function mergeRequests( ...updatedReqProperties, }; } + +export function calculateRequestSize(body: RequestBody | undefined, headers: HeaderList
): RequestSize { + const bodySize = new Blob([(body || '').toString()]).size; + const headerSize = new Blob([ + headers.reduce( + (acc, header) => (acc + header.toString() + '\n'), + '', + {}, + ), + ]).size; + + return { + body: bodySize, + header: headerSize, + total: bodySize + headerSize, + source: 'COMPUTED', + }; +}