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
This commit is contained in:
Filipe Freire 2023-11-08 15:03:01 +00:00 committed by GitHub
parent 5586d4819f
commit ee5d346fcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -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,
}]);
});
});
});

View File

@ -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] !== '-';