mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-15 20:01:28 +00:00
By default Axios will set the Content-Type for POST/PUT/PATCH requests to "application/x-www-form-urlencoded" if the Content-Type header is not specified. This explicitly sets the content type to "false" when there the body mode is set to "none", and the user has not set an explicit content type themselves. Setting the content type to false directs Axios not to send a Content-Type header.
80 lines
3.2 KiB
JavaScript
80 lines
3.2 KiB
JavaScript
const { describe, it, expect } = require('@jest/globals');
|
|
|
|
const { prepareRequest } = require('../../src/ipc/network/prepare-request');
|
|
const { buildFormUrlEncodedPayload } = require('../../src/utils/form-data');
|
|
|
|
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}' };
|
|
const expected = `{
|
|
\"test\": \"{{someVar}}\"
|
|
}`;
|
|
const result = await prepareRequest({ request: { body }, collection: { pathname: '' } });
|
|
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}';
|
|
const result = await prepareRequest({ request: { body }, collection: { pathname: '' } });
|
|
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);
|
|
});
|
|
});
|
|
|
|
describe.each(['POST', 'PUT', 'PATCH'])('POST request with no body', (method) => {
|
|
it('Should set content-type header to false if method is ' + method + ' and there is no data in the body', async () => {
|
|
const request = { method: method, url: 'test-domain', body: { mode: 'none' } };
|
|
const result = await prepareRequest({ request, collection: { pathname: '' } });
|
|
expect(result.headers['content-type']).toEqual(false);
|
|
});
|
|
it('Should respect the content-type header if explicitly set', async () => {
|
|
const request = {
|
|
method: method,
|
|
url: 'test-domain',
|
|
body: { mode: 'none' },
|
|
headers: [{ name: 'content-type', value: 'application/json', enabled: true }]
|
|
};
|
|
const result = await prepareRequest({ request, collection: { pathname: '' } });
|
|
expect(result.headers['content-type']).toEqual('application/json');
|
|
});
|
|
});
|
|
}); |