From 31147475f3797c23a4b0ff8768d569975e87b25c Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Tue, 14 May 2024 00:05:54 -0700 Subject: [PATCH] Fix curl export with multi-line body --- plugins/exporter-curl/tests/index.test.ts | 20 ++++++++++++++++++++ plugins/importer-curl/src/index.ts | 2 +- plugins/importer-curl/tests/index.test.ts | 21 +++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/plugins/exporter-curl/tests/index.test.ts b/plugins/exporter-curl/tests/index.test.ts index b7291d5f..469528ed 100644 --- a/plugins/exporter-curl/tests/index.test.ts +++ b/plugins/exporter-curl/tests/index.test.ts @@ -80,6 +80,26 @@ describe('exporter-curl', () => { ); }); + test('Exports multi-line JSON body', () => { + expect( + pluginHookExport({ + url: 'https://yaak.app', + method: 'POST', + bodyType: 'application/json', + body: { + text: `{"foo":"bar",\n"baz":"qux"}`, + }, + headers: [{ name: 'Content-Type', value: 'application/json' }], + }), + ).toEqual( + [ + `curl -X POST 'https://yaak.app'`, + `--header 'Content-Type: application/json'`, + `--data-raw $'{"foo":"bar",\n"baz":"qux"}'`, + ].join(` \\\n `), + ); + }); + test('Exports headers', () => { expect( pluginHookExport({ diff --git a/plugins/importer-curl/src/index.ts b/plugins/importer-curl/src/index.ts index 9b25c0fe..003616d0 100644 --- a/plugins/importer-curl/src/index.ts +++ b/plugins/importer-curl/src/index.ts @@ -52,7 +52,7 @@ export function pluginHookImport(rawData: string) { // Replace non-escaped newlines with semicolons to make parsing easier // NOTE: This is really slow in debug build but fast in release mode - const normalizedData = rawData.replace(/([^\\])\n/g, '$1; '); + const normalizedData = rawData.replace(/\ncurl/g, '; curl'); let currentCommand: ParseEntry[] = []; diff --git a/plugins/importer-curl/tests/index.test.ts b/plugins/importer-curl/tests/index.test.ts index 70963b05..8e120b58 100644 --- a/plugins/importer-curl/tests/index.test.ts +++ b/plugins/importer-curl/tests/index.test.ts @@ -177,6 +177,27 @@ describe('importer-curl', () => { }); }); + test('Imports multi-line JSON', () => { + expect( + pluginHookImport( + `curl -H Content-Type:application/json -d $'{\n "foo":"bar"\n}' https://yaak.app`, + ), + ).toEqual({ + resources: { + workspaces: [baseWorkspace()], + httpRequests: [ + baseRequest({ + method: 'POST', + url: 'https://yaak.app', + headers: [{ name: 'Content-Type', value: 'application/json' }], + bodyType: 'application/json', + body: { text: '{\n "foo":"bar"\n}' }, + }), + ], + }, + }); + }); + test('Imports multiple headers', () => { expect( pluginHookImport('curl -H Foo:bar --header Name -H AAA:bbb -H :ccc https://yaak.app'),