Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Allow empty Content-Type when no body (#1693) #3973

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 35 additions & 33 deletions packages/bruno-electron/src/ipc/network/interpolate-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,41 +65,43 @@ const interpolateVars = (request, envVariables = {}, runtimeVariables = {}, proc

const contentType = getContentType(request.headers);

/*
We explicitly avoid interpolating buffer values because the file content is read as a buffer object in raw body mode.
Even if the selected file's content type is JSON, this prevents the buffer object from being interpolated.
*/
if (contentType.includes('json') && !Buffer.isBuffer(request.data)) {
if (typeof request.data === 'string') {
if (request.data.length) {
request.data = _interpolate(request.data);
if (typeof contentType === 'string') {
/*
We explicitly avoid interpolating buffer values because the file content is read as a buffer object in raw body mode.
Even if the selected file's content type is JSON, this prevents the buffer object from being interpolated.
*/
if (contentType.includes('json') && !Buffer.isBuffer(request.data)) {
if (typeof request.data === 'string') {
if (request.data.length) {
request.data = _interpolate(request.data);
}
} else if (typeof request.data === 'object') {
try {
let parsed = JSON.stringify(request.data);
parsed = _interpolate(parsed);
request.data = JSON.parse(parsed);
} catch (err) {}
}
} else if (typeof request.data === 'object') {
try {
let parsed = JSON.stringify(request.data);
parsed = _interpolate(parsed);
request.data = JSON.parse(parsed);
} catch (err) {}
}
} else if (contentType === 'application/x-www-form-urlencoded') {
if (typeof request.data === 'object') {
try {
forOwn(request?.data, (value, key) => {
request.data[key] = _interpolate(value);
});
} catch (err) {}
}
} else if (contentType === 'multipart/form-data') {
if (Array.isArray(request?.data) && !(request.data instanceof FormData)) {
try {
request.data = request?.data?.map(d => ({
...d,
value: _interpolate(d?.value)
}));
} catch (err) {}
} else if (contentType === 'application/x-www-form-urlencoded') {
if (typeof request.data === 'object') {
try {
forOwn(request?.data, (value, key) => {
request.data[key] = _interpolate(value);
});
} catch (err) {}
}
} else if (contentType === 'multipart/form-data') {
if (Array.isArray(request?.data) && !(request.data instanceof FormData)) {
try {
request.data = request?.data?.map(d => ({
...d,
value: _interpolate(d?.value)
}));
} catch (err) {}
}
} else {
request.data = _interpolate(request.data);
}
} else {
request.data = _interpolate(request.data);
}

each(request.pathParams, (param) => {
Expand Down
7 changes: 7 additions & 0 deletions packages/bruno-electron/src/ipc/network/prepare-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ const prepareRequest = async (item, collection = {}, abortController) => {
axiosRequest.data = graphqlQuery;
}

// if the mode is 'none' then set the content-type header to false. #1693
if (request.body.mode === 'none') {
if(!contentTypeDefined) {
axiosRequest.headers['content-type'] = false;
}
}

if (request.script) {
axiosRequest.script = request.script;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,13 @@ describe('interpolate-vars: interpolateVars', () => {
});
});
});

describe('Handles content-type header set to false', () => {
it('Should result empty data', async () => {
const request = { method: 'POST', url: 'test', data: undefined, headers: { 'content-type': false } };

const result = interpolateVars(request, { 'test.url': 'test.com' }, null, null);
expect(result.data).toEqual(undefined);
});
});
});
18 changes: 18 additions & 0 deletions packages/bruno-electron/tests/network/prepare-request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,22 @@ describe('prepare-request: prepareRequest', () => {
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');
});
});
});