Fix curl export with multi-line body

This commit is contained in:
Gregory Schier
2024-05-14 00:05:54 -07:00
parent 4894677599
commit 9875d2353d
3 changed files with 42 additions and 1 deletions

View File

@@ -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({

View File

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

View File

@@ -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'),