Skip to content

Commit

Permalink
feat: enable Request and Response in pre-request scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
ihexxa committed Feb 28, 2024
1 parent 153512f commit 5bf3194
Show file tree
Hide file tree
Showing 6 changed files with 813 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ test.describe('pre-request UI tests', async () => {
},
},
{
name: 'require / require classes from insomnia-collection module',
name: 'require / require classes from insomnia-collection module and init them',
preReqScript: `
const { Property, Header, Variable, QueryParam, Url, Cookie, RequestAuth, Certificate } = require('insomnia-collection');
const { Property, Header, Variable, QueryParam, Url, Cookie, RequestAuth, Certificate, RequestBody, Request, Response } = require('insomnia-collection');
const prop = new Property('pid', 'pname');
const header = new Header({ key: 'headerKey', value: 'headerValue' });
const variable = new Variable({ key: 'headerKey', value: 'headerValue' });
Expand All @@ -97,6 +97,54 @@ test.describe('pre-request UI tests', async () => {
cert: { src: '/User/path/to/certificate' },
passphrase: 'iampassphrase',
});
const reqBody = new RequestBody({
mode: 'urlencoded',
urlencoded: [
{ key: 'urlencodedKey', value: 'urlencodedValue' },
],
options: {},
});
const req = new Request({
url: 'https://hostname.com/path',
method: 'GET',
header: [
{ key: 'header1', value: 'val1' },
{ key: 'header2', value: 'val2' },
],
body: {
mode: 'raw',
raw: 'body content',
},
auth: {
type: 'basic',
basic: [
{ key: 'username', value: 'myname' },
{ key: 'password', value: 'mypwd' },
],
},
proxy: undefined,
certificate: undefined,
});
const resp = new Response({
code: 200,
reason: 'OK',
header: [
{ key: 'header1', value: 'val1' },
{ key: 'header2', value: 'val2' },
{ key: 'Content-Length', value: '100' },
{ key: 'Content-Disposition', value: 'attachment; filename="filename.txt"' },
{ key: 'Content-Type', value: 'text/plain; charset=utf-8' },
],
cookie: [
{ key: 'header1', value: 'val1' },
{ key: 'header2', value: 'val2' },
],
body: '{"key": 888}',
stream: undefined,
responseTime: 100,
status: 'OK',
originalRequest: req,
});
// set part of values
insomnia.environment.set('propJson', JSON.stringify(prop.toJSON()));
insomnia.environment.set('headerJson', JSON.stringify(header.toJSON()));
Expand Down
75 changes: 75 additions & 0 deletions packages/insomnia/src/sdk/objects/__tests__/request.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import url from 'node:url';

import { describe, expect, it } from '@jest/globals';

import { Request, RequestBody } from '../request';
import { setUrlParser } from '../urls';

describe('test request and response objects', () => {
setUrlParser(url.URL);

it('test RequestBody methods', () => {
const reqBody = new RequestBody({
mode: 'urlencoded',
formdata: [
{ key: 'formDataKey', value: 'formDataValue' },
],
urlencoded: [
{ key: 'urlencodedKey', value: 'urlencodedValue' },
],
options: {},
});

expect(reqBody.toString()).toEqual('urlencodedKey=urlencodedValue');

reqBody.update({ mode: 'file', file: 'file content here' });
expect(reqBody.toString()).toEqual('file content here');
});

it('test Request methods', () => {
const req = new Request({
url: 'https://hostname.com/path',
method: 'GET',
header: [
{ key: 'header1', value: 'val1' },
{ key: 'header2', value: 'val2' },
],
body: {
mode: 'raw',
raw: 'body content',
},
auth: {
type: 'basic',
basic: [
{ key: 'username', value: 'myname' },
{ key: 'password', value: 'mypwd' },
],
},
proxy: undefined,
certificate: undefined,
});

req.addHeader({ key: 'newHeader', value: 'newValue' });
expect(req.headers.count()).toEqual(3);
req.removeHeader('notExist', { ignoreCase: false });
expect(req.headers.count()).toEqual(3);
req.removeHeader('NEWHEADER', { ignoreCase: false });
expect(req.headers.count()).toEqual(3);
req.removeHeader('NEWHEADER', { ignoreCase: true });
expect(req.headers.count()).toEqual(2);

req.upsertHeader({ key: 'header1', value: 'new_val1' });
expect(req.getHeaders({
ignoreCase: true,
enabled: true,
multiValue: true,
sanitizeKeys: true,
})).toEqual({
header1: ['new_val1'],
header2: ['val2'],
});

const req2 = req.clone();
expect(req2.toJSON()).toEqual(req.toJSON());
});
});
71 changes: 71 additions & 0 deletions packages/insomnia/src/sdk/objects/__tests__/response.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import url from 'node:url';

import { describe, expect, it } from '@jest/globals';

import { Request } from '../request';
import { Response } from '../response';
import { setUrlParser } from '../urls';

describe('test request and response objects', () => {
setUrlParser(url.URL);

it('test Response methods', () => {
const req = new Request({
url: 'https://hostname.com/path',
method: 'GET',
header: [
{ key: 'header1', value: 'val1' },
{ key: 'header2', value: 'val2' },
],
body: {
mode: 'raw',
raw: '{"key": 888}',
},
auth: {
type: 'basic',
basic: [
{ key: 'username', value: 'myname' },
{ key: 'password', value: 'mypwd' },
],
},
proxy: undefined,
certificate: undefined,
});

const resp = new Response({
code: 200,
reason: 'OK',
header: [
{ key: 'header1', value: 'val1' },
{ key: 'header2', value: 'val2' },
{ key: 'Content-Length', value: '100' },
{ key: 'Content-Disposition', value: 'attachment; filename="filename.txt"' },
{ key: 'Content-Type', value: 'text/plain; charset=utf-8' },
],
cookie: [
{ key: 'header1', value: 'val1' },
{ key: 'header2', value: 'val2' },
],
body: '{"key": 888}',
stream: undefined,
responseTime: 100,
status: 'OK',
originalRequest: req,
});

// TODO: this will work after PropertyList.one is improved
// expect(resp.size()).toBe(100);

expect(resp.json()).toEqual({
key: 888,
});
expect(resp.contentInfo()).toEqual({
charset: 'utf-8',
contentType: 'text/plain; charset=utf-8',
fileExtension: 'txt',
fileName: 'filename',
mimeFormat: '',
mimeType: 'text/plain',
});
});
});
2 changes: 2 additions & 0 deletions packages/insomnia/src/sdk/objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export { ProxyConfig, ProxyConfigList } from './proxy-configs';
export { RequestAuth } from './auth';
export { Cookie, CookieList } from './cookies';
export { Certificate } from './certificates';
export { FormParam, RequestBody, Request } from './request';
export { Response } from './response';
Loading

0 comments on commit 5bf3194

Please sign in to comment.