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:
Yi Tang 2020-04-24 02:44:54 +08:00 committed by GitHub
parent b7d97a071b
commit 28e7d928a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 6 deletions

View File

@ -1,6 +1,7 @@
import * as plugin from '../request';
import * as models from '../../../models';
import { globalBeforeEach } from '../../../__jest__/before-each';
import { CONTENT_TYPE_FORM_URLENCODED } from '../../../common/constants';
const CONTEXT = {
user_key: 'my_user_key',
@ -28,6 +29,7 @@ describe('init()', () => {
'addHeader',
'addParameter',
'getAuthentication',
'getBody',
'getBodyText',
'getEnvironment',
'getEnvironmentVariable',
@ -44,6 +46,7 @@ describe('init()', () => {
'removeHeader',
'removeParameter',
'setAuthenticationParameter',
'setBody',
'setBodyText',
'setCookie',
'setHeader',
@ -63,6 +66,7 @@ describe('init()', () => {
expect(Object.keys(result)).toEqual(['request']);
expect(Object.keys(result.request).sort()).toEqual([
'getAuthentication',
'getBody',
'getBodyText',
'getEnvironment',
'getEnvironmentVariable',
@ -223,4 +227,20 @@ describe('request.*', () => {
expect(result.request.getAuthentication()).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);
});
});

View File

@ -1,5 +1,6 @@
// @flow
import type { RenderedRequest } from '../../common/render';
import type { RequestBody } from '../../models/request';
import * as misc from '../../common/misc';
export function init(
@ -15,9 +16,6 @@ export function init(
getId(): string {
return renderedRequest._id;
},
getBodyText(): string {
return renderedRequest.body.text || '';
},
getName(): string {
return renderedRequest.name;
},
@ -33,9 +31,6 @@ export function init(
setUrl(url: string): void {
renderedRequest.url = url;
},
setBodyText(text: string): void {
renderedRequest.body.text = text;
},
setCookie(name: string, value: string): void {
const cookie = renderedRequest.cookies.find(c => c.name === name);
if (cookie) {
@ -147,6 +142,28 @@ export function init(
getAuthentication(): Object {
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
// addCookie (name: string, value: string): void {}
@ -172,6 +189,7 @@ export function init(
delete request.addParameter;
delete request.addParameter;
delete request.setAuthenticationParameter;
delete request.setBody;
}
return { request };