From ee5d346fcf76b8068cc9071ba08232fb96fb9c4c Mon Sep 17 00:00:00 2001 From: Filipe Freire Date: Wed, 8 Nov 2023 15:03:01 +0000 Subject: [PATCH] fix: multi-line curl not properly imported due to padding #6755 (#6774) * fix: multi-line curl not properly imported due to padding #6755 * Update curl.test.ts * Update curl.test.ts --- .../utils/importers/importers/curl.test.ts | 19 +++++++++++++++++++ .../src/utils/importers/importers/curl.ts | 7 ++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/insomnia/src/utils/importers/importers/curl.test.ts b/packages/insomnia/src/utils/importers/importers/curl.test.ts index 0e1922dec..bacd314b8 100644 --- a/packages/insomnia/src/utils/importers/importers/curl.test.ts +++ b/packages/insomnia/src/utils/importers/importers/curl.test.ts @@ -153,4 +153,23 @@ describe('curl', () => { }]); }); }); + describe('cURL -H flags', () => { + it.each([ + { flag: '-H', inputs: ['X-Host: example.com'], expected: [{ name: 'X-Host', value: 'example.com' }] }, + { flag: '-H', inputs: ['X-Host:example.com'], expected: [{ name: 'X-Host', value: 'example.com' }] }, + { flag: '-H', inputs: ['Content-Type:application/x-www-form-urlencoded'], expected: [{ name: 'Content-Type', value: 'application/x-www-form-urlencoded' }] }, + { flag: ' -H', inputs: ['Content-Type:application/x-www-form-urlencoded'], expected: [{ name: 'Content-Type', value: 'application/x-www-form-urlencoded' }] }, + { flag: ' -H', inputs: ['Content-Type:application/x-www-form-urlencoded'], expected: [{ name: 'Content-Type', value: 'application/x-www-form-urlencoded' }] }, + ])('handles %p correctly', async ({ + flag, + inputs, + expected, + }: { flag: string; inputs: string[]; expected: Parameter[] }) => { + const flaggedInputs = inputs.map(input => `${flag} ${quote([input])}`).join(' '); + const rawData = `curl https://example.com ${flaggedInputs}`; + expect(convert(rawData)).toMatchObject([{ + headers: expected, + }]); + }); + }); }); diff --git a/packages/insomnia/src/utils/importers/importers/curl.ts b/packages/insomnia/src/utils/importers/importers/curl.ts index 03698f9a5..0e62cb6dc 100644 --- a/packages/insomnia/src/utils/importers/importers/curl.ts +++ b/packages/insomnia/src/utils/importers/importers/curl.ts @@ -46,7 +46,12 @@ const importCommand = (parseEntries: ParseEntry[]): ImportRequest => { // Start at 1 so we can skip the ^curl part for (let i = 1; i < parseEntries.length; i++) { - const parseEntry = parseEntries[i]; + let parseEntry = parseEntries[i]; + // trim leading spaces between parsed entries + // regex won't match otherwise (e.g. -H 'Content-Type: application/json') + if (typeof parseEntry === 'string') { + parseEntry = parseEntry.trim(); + } if (typeof parseEntry === 'string' && parseEntry.match(/^-{1,2}[\w-]+/)) { const isSingleDash = parseEntry[0] === '-' && parseEntry[1] !== '-';