fix: decomment not working in json body (#1819)

Request body json was not decommented if json parsing fails, which
would happen if variables are not quoted.

Fixes usebruno#888
This commit is contained in:
Antti Sonkeri 2024-06-19 13:48:36 +03:00 committed by GitHub
parent a0df5138b3
commit 9c11e27d1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 48 additions and 4 deletions

View File

@ -76,10 +76,11 @@ const prepareRequest = (request, collectionRoot) => {
if (!contentTypeDefined) { if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'application/json'; axiosRequest.headers['content-type'] = 'application/json';
} }
const jsonBody = decomment(request.body.json);
try { try {
axiosRequest.data = JSONbig.parse(decomment(request.body.json)); axiosRequest.data = JSONbig.parse(jsonBody);
} catch (ex) { } catch (ex) {
axiosRequest.data = request.body.json; axiosRequest.data = jsonBody;
} }
} }

View File

@ -0,0 +1,21 @@
const { describe, it, expect } = require('@jest/globals');
const prepareRequest = require('../../src/runner/prepare-request');
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 = prepareRequest({ body });
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 = prepareRequest({ body });
expect(result.data).toEqual(expected);
});
});
});

View File

@ -172,10 +172,11 @@ const prepareRequest = (request, collectionRoot, collectionPath) => {
if (!contentTypeDefined) { if (!contentTypeDefined) {
axiosRequest.headers['content-type'] = 'application/json'; axiosRequest.headers['content-type'] = 'application/json';
} }
const body = decomment(request.body.json);
try { try {
axiosRequest.data = JSONbig.parse(decomment(request.body.json)); axiosRequest.data = JSONbig.parse(body);
} catch (ex) { } catch (ex) {
axiosRequest.data = request.body.json; axiosRequest.data = body;
} }
} }

View File

@ -0,0 +1,21 @@
const { describe, it, expect } = require('@jest/globals');
const prepareRequest = require('../../src/ipc/network/prepare-request');
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 = prepareRequest({ body });
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 = prepareRequest({ body });
expect(result.data).toEqual(expected);
});
});
});