mirror of
https://github.com/Kong/insomnia
synced 2024-11-07 22:30:15 +00:00
Expose the request body for plugins (#2003)
* Expose more request body for plugins Support the following api for plugins: - get/set the content type of the request; - get/add/set the form parameters of the reuqest body; adding file form item will be ignored if the mime type is not multipath form. - get/set the uploaded filename of the request; setting uploading file name will be ignored if the mime type is not octet stream. * Add some test cases * Add simple request body api for plugins * Mark (get|set)BodyText() as deprecated
This commit is contained in:
parent
b7d97a071b
commit
28e7d928a8
@ -1,6 +1,7 @@
|
|||||||
import * as plugin from '../request';
|
import * as plugin from '../request';
|
||||||
import * as models from '../../../models';
|
import * as models from '../../../models';
|
||||||
import { globalBeforeEach } from '../../../__jest__/before-each';
|
import { globalBeforeEach } from '../../../__jest__/before-each';
|
||||||
|
import { CONTENT_TYPE_FORM_URLENCODED } from '../../../common/constants';
|
||||||
|
|
||||||
const CONTEXT = {
|
const CONTEXT = {
|
||||||
user_key: 'my_user_key',
|
user_key: 'my_user_key',
|
||||||
@ -28,6 +29,7 @@ describe('init()', () => {
|
|||||||
'addHeader',
|
'addHeader',
|
||||||
'addParameter',
|
'addParameter',
|
||||||
'getAuthentication',
|
'getAuthentication',
|
||||||
|
'getBody',
|
||||||
'getBodyText',
|
'getBodyText',
|
||||||
'getEnvironment',
|
'getEnvironment',
|
||||||
'getEnvironmentVariable',
|
'getEnvironmentVariable',
|
||||||
@ -44,6 +46,7 @@ describe('init()', () => {
|
|||||||
'removeHeader',
|
'removeHeader',
|
||||||
'removeParameter',
|
'removeParameter',
|
||||||
'setAuthenticationParameter',
|
'setAuthenticationParameter',
|
||||||
|
'setBody',
|
||||||
'setBodyText',
|
'setBodyText',
|
||||||
'setCookie',
|
'setCookie',
|
||||||
'setHeader',
|
'setHeader',
|
||||||
@ -63,6 +66,7 @@ describe('init()', () => {
|
|||||||
expect(Object.keys(result)).toEqual(['request']);
|
expect(Object.keys(result)).toEqual(['request']);
|
||||||
expect(Object.keys(result.request).sort()).toEqual([
|
expect(Object.keys(result.request).sort()).toEqual([
|
||||||
'getAuthentication',
|
'getAuthentication',
|
||||||
|
'getBody',
|
||||||
'getBodyText',
|
'getBodyText',
|
||||||
'getEnvironment',
|
'getEnvironment',
|
||||||
'getEnvironmentVariable',
|
'getEnvironmentVariable',
|
||||||
@ -223,4 +227,20 @@ describe('request.*', () => {
|
|||||||
expect(result.request.getAuthentication()).toEqual({ foo: 'baz' });
|
expect(result.request.getAuthentication()).toEqual({ foo: 'baz' });
|
||||||
expect(request.authentication).toEqual({ foo: 'baz' });
|
expect(request.authentication).toEqual({ foo: 'baz' });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('works for request body', async () => {
|
||||||
|
const result = plugin.init(await models.request.getById('req_1'), CONTEXT);
|
||||||
|
expect(result.request.getBody()).toEqual({ text: 'body' });
|
||||||
|
const newBody = {
|
||||||
|
mimeType: CONTENT_TYPE_FORM_URLENCODED,
|
||||||
|
params: [
|
||||||
|
{
|
||||||
|
name: 'foo',
|
||||||
|
value: 'bar',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
result.request.setBody(newBody);
|
||||||
|
expect(result.request.getBody()).toEqual(newBody);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import type { RenderedRequest } from '../../common/render';
|
import type { RenderedRequest } from '../../common/render';
|
||||||
|
import type { RequestBody } from '../../models/request';
|
||||||
import * as misc from '../../common/misc';
|
import * as misc from '../../common/misc';
|
||||||
|
|
||||||
export function init(
|
export function init(
|
||||||
@ -15,9 +16,6 @@ export function init(
|
|||||||
getId(): string {
|
getId(): string {
|
||||||
return renderedRequest._id;
|
return renderedRequest._id;
|
||||||
},
|
},
|
||||||
getBodyText(): string {
|
|
||||||
return renderedRequest.body.text || '';
|
|
||||||
},
|
|
||||||
getName(): string {
|
getName(): string {
|
||||||
return renderedRequest.name;
|
return renderedRequest.name;
|
||||||
},
|
},
|
||||||
@ -33,9 +31,6 @@ export function init(
|
|||||||
setUrl(url: string): void {
|
setUrl(url: string): void {
|
||||||
renderedRequest.url = url;
|
renderedRequest.url = url;
|
||||||
},
|
},
|
||||||
setBodyText(text: string): void {
|
|
||||||
renderedRequest.body.text = text;
|
|
||||||
},
|
|
||||||
setCookie(name: string, value: string): void {
|
setCookie(name: string, value: string): void {
|
||||||
const cookie = renderedRequest.cookies.find(c => c.name === name);
|
const cookie = renderedRequest.cookies.find(c => c.name === name);
|
||||||
if (cookie) {
|
if (cookie) {
|
||||||
@ -147,6 +142,28 @@ export function init(
|
|||||||
getAuthentication(): Object {
|
getAuthentication(): Object {
|
||||||
return renderedRequest.authentication;
|
return renderedRequest.authentication;
|
||||||
},
|
},
|
||||||
|
getBody(): RequestBody {
|
||||||
|
return renderedRequest.body;
|
||||||
|
},
|
||||||
|
setBody(body: RequestBody): void {
|
||||||
|
renderedRequest.body = body;
|
||||||
|
},
|
||||||
|
|
||||||
|
// ~~~~~~~~~~~~~~~~~~ //
|
||||||
|
// Deprecated Methods //
|
||||||
|
// ~~~~~~~~~~~~~~~~~~ //
|
||||||
|
|
||||||
|
/** @deprecated in favor of getting the whole body by getBody */
|
||||||
|
getBodyText(): string {
|
||||||
|
console.warn('request.getBodyText() is deprecated. Use request.getBody() instead.');
|
||||||
|
return renderedRequest.body.text || '';
|
||||||
|
},
|
||||||
|
|
||||||
|
/** @deprecated in favor of setting the whole body by setBody */
|
||||||
|
setBodyText(text: string): void {
|
||||||
|
console.warn('request.setBodyText() is deprecated. Use request.setBody() instead.');
|
||||||
|
renderedRequest.body.text = text;
|
||||||
|
},
|
||||||
|
|
||||||
// NOTE: For these to make sense, we'd need to account for cookies in the jar as well
|
// NOTE: For these to make sense, we'd need to account for cookies in the jar as well
|
||||||
// addCookie (name: string, value: string): void {}
|
// addCookie (name: string, value: string): void {}
|
||||||
@ -172,6 +189,7 @@ export function init(
|
|||||||
delete request.addParameter;
|
delete request.addParameter;
|
||||||
delete request.addParameter;
|
delete request.addParameter;
|
||||||
delete request.setAuthenticationParameter;
|
delete request.setAuthenticationParameter;
|
||||||
|
delete request.setBody;
|
||||||
}
|
}
|
||||||
|
|
||||||
return { request };
|
return { request };
|
||||||
|
Loading…
Reference in New Issue
Block a user