bugfix(#1320):Now the form-url-encoded params in the body can contain multiple values with same name. (#2964)

* Now the form-url-encoded params in the body can contain multiple values with same name.

* Updated the tests and renamed the function name

* Added the inimported function

* Minor changes.
This commit is contained in:
Sanjai Kumar 2024-09-15 23:27:16 +05:30 committed by GitHub
parent 8856e8ec71
commit 0937bab7f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 67 additions and 7 deletions

View File

@ -5,6 +5,7 @@ const FormData = require('form-data');
const fs = require('fs');
const path = require('path');
const { getTreePathFromCollectionToItem } = require('../../utils/collection');
const { buildFormUrlEncodedPayload } = require('../../utils/common');
const mergeFolderLevelHeaders = (request, requestTreePath) => {
let folderHeaders = new Map();
@ -300,7 +301,7 @@ const prepareRequest = (item, collection) => {
let contentTypeDefined = false;
let url = request.url;
// collection headers
// Collection level headers
each(get(collectionRoot, 'request.headers', []), (h) => {
if (h.enabled && h.name.length > 0) {
headers[h.name] = h.value;
@ -319,6 +320,7 @@ const prepareRequest = (item, collection) => {
mergeVars(collection, request, requestTreePath);
}
// Request level headers
each(request.headers, (h) => {
if (h.enabled && h.name.length > 0) {
headers[h.name] = h.value;
@ -372,11 +374,11 @@ const prepareRequest = (item, collection) => {
}
if (request.body.mode === 'formUrlEncoded') {
axiosRequest.headers['content-type'] = 'application/x-www-form-urlencoded';
const params = {};
if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'application/x-www-form-urlencoded';
}
const enabledParams = filter(request.body.formUrlEncoded, (p) => p.enabled);
each(enabledParams, (p) => (params[p.name] = p.value));
axiosRequest.data = params;
axiosRequest.data = buildFormUrlEncodedPayload(enabledParams);
}
if (request.body.mode === 'multipartForm') {

View File

@ -85,6 +85,24 @@ const flattenDataForDotNotation = (data) => {
return result;
};
/**
* @param {Array.<object>} params The request body Array
* @returns {object} Returns an obj with repeating key as a array of values
* {item: 2, item: 3, item1: 4} becomes {item: [2,3], item1: 4}
*/
const buildFormUrlEncodedPayload = (params) => {
return params.reduce((acc, p) => {
if (!acc[p.name]) {
acc[p.name] = p.value;
} else if (Array.isArray(acc[p.name])) {
acc[p.name].push(p.value);
} else {
acc[p.name] = [acc[p.name], p.value];
}
return acc;
}, {});
};
module.exports = {
uuid,
stringifyJson,
@ -93,5 +111,6 @@ module.exports = {
safeParseJSON,
simpleHash,
generateUidBasedOnHash,
flattenDataForDotNotation
flattenDataForDotNotation,
buildFormUrlEncodedPayload
};

View File

@ -1,6 +1,7 @@
const { describe, it, expect } = require('@jest/globals');
const prepareRequest = require('../../src/ipc/network/prepare-request');
const { buildFormUrlEncodedPayload } = require('../../src/utils/common');
describe('prepare-request: prepareRequest', () => {
describe('Decomments request body', () => {
@ -17,5 +18,43 @@ describe('prepare-request: prepareRequest', () => {
const result = prepareRequest({ request: { body } }, {});
expect(result.data).toEqual(expected);
});
it('should handle single key-value pair', () => {
const requestObj = [{ name: 'item', value: 2 }];
const expected = { item: 2 };
const result = buildFormUrlEncodedPayload(requestObj);
expect(result).toEqual(expected);
});
it('should handle multiple key-value pairs with unique keys', () => {
const requestObj = [
{ name: 'item1', value: 2 },
{ name: 'item2', value: 3 }
];
const expected = { item1: 2, item2: 3 };
const result = buildFormUrlEncodedPayload(requestObj);
expect(result).toEqual(expected);
});
it('should handle multiple key-value pairs with the same key', () => {
const requestObj = [
{ name: 'item', value: 2 },
{ name: 'item', value: 3 }
];
const expected = { item: [2, 3] };
const result = buildFormUrlEncodedPayload(requestObj);
expect(result).toEqual(expected);
});
it('should handle mixed key-value pairs with unique and duplicate keys', () => {
const requestObj = [
{ name: 'item1', value: 2 },
{ name: 'item2', value: 3 },
{ name: 'item1', value: 4 }
];
const expected = { item1: [2, 4], item2: 3 };
const result = buildFormUrlEncodedPayload(requestObj);
expect(result).toEqual(expected);
});
});
});
});