2024-06-19 12:48:36 +02:00
|
|
|
const { describe, it, expect } = require('@jest/globals');
|
|
|
|
|
|
|
|
const prepareRequest = require('../../src/ipc/network/prepare-request');
|
2024-09-15 19:57:16 +02:00
|
|
|
const { buildFormUrlEncodedPayload } = require('../../src/utils/common');
|
2024-06-19 12:48:36 +02:00
|
|
|
|
|
|
|
describe('prepare-request: prepareRequest', () => {
|
|
|
|
describe('Decomments request body', () => {
|
|
|
|
it('If request body is valid JSON', async () => {
|
|
|
|
const body = { mode: 'json', json: '{\n"test": "{{someVar}}" // comment\n}' };
|
2024-08-27 10:42:56 +02:00
|
|
|
const expected = '{\n"test": "{{someVar}}" \n}';
|
2024-07-01 09:22:56 +02:00
|
|
|
const result = prepareRequest({ request: { body } }, {});
|
2024-06-19 12:48:36 +02:00
|
|
|
expect(result.data).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('If request body is not valid JSON', async () => {
|
|
|
|
const body = { mode: 'json', json: '{\n"test": {{someVar}} // comment\n}' };
|
|
|
|
const expected = '{\n"test": {{someVar}} \n}';
|
2024-07-01 09:22:56 +02:00
|
|
|
const result = prepareRequest({ request: { body } }, {});
|
2024-06-19 12:48:36 +02:00
|
|
|
expect(result.data).toEqual(expected);
|
|
|
|
});
|
2024-09-15 19:57:16 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2024-06-19 12:48:36 +02:00
|
|
|
});
|
2024-09-15 19:57:16 +02:00
|
|
|
});
|