Skip to content

Commit

Permalink
fix: Variable is replaced in multipart/form-data when file is injected (
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Feb 2, 2024
1 parent b754ab7 commit 1f936db
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [6.11.3] (2023-02-02)

### Fix
- Variable is replaced in multipart/form-data when file is injected (AnWeber/vscode-httpyac#258)

## [6.11.2] (2023-01-31)

### Fix
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"publisher": "AnWeber",
"description": "HTTP/REST CLI Client for *.http files",
"version": "6.11.2",
"version": "6.11.3",
"homepage": "https://github.com/AnWeber/httpyac",
"repository": {
"type": "git",
Expand Down
5 changes: 4 additions & 1 deletion src/models/httpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export interface HttpRequest extends Request<HttpMethod> {
options?: OptionsOfUnknownResponseBody;
}

export type HttpRequestBodyLine = string | ((context: ProcessorContext) => Promise<Buffer | string | undefined>);
export type HttpRequestBodyLine =
| string
| Buffer
| ((context: ProcessorContext) => Promise<Buffer | string | undefined>);

export interface RequestBodyImport {
fileName: string;
Expand Down
1 change: 1 addition & 0 deletions src/plugins/core/registerCorePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function registerCorePlugins(api: models.HttpyacHooksApi) {
function initOnRequestHook(api: models.HttpyacHooksApi) {
api.hooks.onRequest.addHook('attachDefaultHeaders', request.attachDefaultHeaders);
api.hooks.onRequest.addHook('setEnvRequestOptions', request.setEnvRequestOptions);
api.hooks.onRequest.addHook('resolveRequestBody', request.resolveRequestBody);
api.hooks.onRequest.addHook('requestVariableReplacer', request.requestVariableReplacer);
api.hooks.onRequest.addHook('transformRequestBody', request.transformRequestBodyToBuffer);
api.hooks.onRequest.addHook('encodeRequestBody', request.encodeRequestBody);
Expand Down
1 change: 1 addition & 0 deletions src/plugins/core/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from './encodeRequestBody';
export * from './excludeProxyInterceptor';
export * from './isTrustedInterceptor';
export * from './requestVariableReplacer';
export * from './resolveRequestBody';
export * from './setEnvRequestOptions';
export * from './transformRequestBodyToBuffer';
21 changes: 21 additions & 0 deletions src/plugins/core/request/resolveRequestBody.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as models from '../../../models';
import * as utils from '../../../utils';

export async function resolveRequestBody(request: models.Request, context: models.ProcessorContext): Promise<void> {
if (request.body && Array.isArray(request.body)) {
const buffers: Array<Buffer | string> = [];
for (const obj of request.body) {
if (utils.isString(obj)) {
buffers.push(obj);
} else if (Buffer.isBuffer(obj)) {
buffers.push(obj);
} else {
const result = await obj(context);
if (result) {
buffers.push(result);
}
}
}
request.body = buffers;
}
}
2 changes: 2 additions & 0 deletions src/plugins/core/request/transformRequestBodyToBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export async function normalizeBody(
for (const obj of body) {
if (utils.isString(obj)) {
buffers.push(obj);
} else if (Buffer.isBuffer(obj)) {
buffers.push(obj);
} else {
const result = await obj(context);
if (result) {
Expand Down
29 changes: 29 additions & 0 deletions src/plugins/core/test/body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,33 @@ invoice_text: {{bar}}
`--WebKitFormBoundary\r\nContent-Disposition: form-data; name="text"\r\n\r\ninvoice_text: bar2\r\n--WebKitFormBoundary--`
);
});
it('should send mulitpart form body with variables and file', async () => {
const requests = initHttpClientProvider();

initFileProvider({ 'body.json': 'test' });
await sendHttp(
`
@bar=bar2
POST /post
content-type: multipart/form-data; boundary=WebKitFormBoundary
--WebKitFormBoundary
Content-Disposition: form-data; name="text"
invoice_text: {{bar}}
--WebKitFormBoundary
Content-Disposition: form-data; name="invoice"; filename="invoice.pdf"
Content-Type: application/pdf
< ./body.json
--WebKitFormBoundary--
`
);

expect(requests[0].headers?.['content-type']).toBe('multipart/form-data; boundary=WebKitFormBoundary');
expect(Buffer.isBuffer(requests[0].body)).toBe(true);
expect(requests[0].body?.toString()).toBe(
`--WebKitFormBoundary\r\nContent-Disposition: form-data; name="text"\r\n\r\ninvoice_text: bar2\r\n--WebKitFormBoundary\r\nContent-Disposition: form-data; name="invoice"; filename="invoice.pdf"\r\nContent-Type: application/pdf\r\n\r\ntest\r\n--WebKitFormBoundary--`
);
});
});

0 comments on commit 1f936db

Please sign in to comment.