-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable Request and Response in pre-request scripting
- Loading branch information
Showing
6 changed files
with
813 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/insomnia/src/sdk/objects/__tests__/request.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
71
packages/insomnia/src/sdk/objects/__tests__/response.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.