From 5821252a40f9c6a433ed08262e68cf53f753b0ff Mon Sep 17 00:00:00 2001 From: Dimitri Mitropoulos Date: Fri, 15 Jul 2022 15:34:11 -0400 Subject: [PATCH 1/4] simplifies options like all other clients --- src/targets/shell/curl/client.ts | 45 ++++++++++++-------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/src/targets/shell/curl/client.ts b/src/targets/shell/curl/client.ts index 1ad861c23..2081ccfcf 100644 --- a/src/targets/shell/curl/client.ts +++ b/src/targets/shell/curl/client.ts @@ -27,32 +27,27 @@ export const curl: Client = { link: 'http://curl.haxx.se/', description: 'cURL is a command line tool and library for transferring data with URL syntax', }, - convert: ({ fullUrl, method, httpVersion, headersObj, allHeaders, postData }, options) => { - const opts = { - indent: ' ', - short: false, - binary: false, - globOff: false, - ...options, - }; + convert: ({ fullUrl, method, httpVersion, headersObj, allHeaders, postData }, options = {}) => { + const { indent = ' ', short = false, binary = false, globOff = false } = options; + const { push, join } = new CodeBuilder({ - ...(typeof opts.indent === 'string' ? { indent: opts.indent } : {}), - join: opts.indent !== false ? ` \\\n${opts.indent}` : ' ', + ...(typeof indent === 'string' ? { indent: indent } : {}), + join: indent !== false ? ` \\\n${indent}` : ' ', }); - const globOption = opts.short ? '-g' : '--globoff'; - const requestOption = opts.short ? '-X' : '--request'; + const globOption = short ? '-g' : '--globoff'; + const requestOption = short ? '-X' : '--request'; let formattedUrl = quote(fullUrl); push(`curl ${requestOption} ${method}`); - if (opts.globOff) { + if (globOff) { formattedUrl = unescape(formattedUrl); push(globOption); } - push(`${opts.short ? '' : '--url '}${formattedUrl}`); + push(`${short ? '' : '--url '}${formattedUrl}`); if (httpVersion === 'HTTP/1.0') { - push(opts.short ? '-0' : '--http1.0'); + push(short ? '-0' : '--http1.0'); } // if multipart form data, we want to remove the boundary @@ -78,11 +73,11 @@ export const curl: Client = { .sort() .forEach(key => { const header = `${key}: ${headersObj[key]}`; - push(`${opts.short ? '-H' : '--header'} ${quote(header)}`); + push(`${short ? '-H' : '--header'} ${quote(header)}`); }); if (allHeaders.cookie) { - push(`${opts.short ? '-b' : '--cookie'} ${quote(allHeaders.cookie as string)}`); + push(`${short ? '-b' : '--cookie'} ${quote(allHeaders.cookie as string)}`); } // construct post params @@ -96,7 +91,7 @@ export const curl: Client = { post = `${param.name}=${param.value}`; } - push(`${opts.short ? '-F' : '--form'} ${quote(post)}`); + push(`${short ? '-F' : '--form'} ${quote(post)}`); }); break; @@ -104,28 +99,20 @@ export const curl: Client = { if (postData.params) { postData.params.forEach(param => { push( - `${opts.binary ? '--data-binary' : opts.short ? '-d' : '--data'} ${quote( + `${binary ? '--data-binary' : short ? '-d' : '--data'} ${quote( `${param.name}=${param.value}`, )}`, ); }); } else { - push( - `${opts.binary ? '--data-binary' : opts.short ? '-d' : '--data'} ${quote( - postData.text, - )}`, - ); + push(`${binary ? '--data-binary' : short ? '-d' : '--data'} ${quote(postData.text)}`); } break; default: // raw request body if (postData.text) { - push( - `${opts.binary ? '--data-binary' : opts.short ? '-d' : '--data'} ${quote( - postData.text, - )}`, - ); + push(`${binary ? '--data-binary' : short ? '-d' : '--data'} ${quote(postData.text)}`); } } From 3325526379c981b4854ff906194133c34a120d93 Mon Sep 17 00:00:00 2001 From: Dimitri Mitropoulos Date: Fri, 15 Jul 2022 15:48:55 -0400 Subject: [PATCH 2/4] creates args helper --- src/targets/shell/curl/client.ts | 52 +++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/src/targets/shell/curl/client.ts b/src/targets/shell/curl/client.ts index 2081ccfcf..2d4b1dc6b 100644 --- a/src/targets/shell/curl/client.ts +++ b/src/targets/shell/curl/client.ts @@ -1,5 +1,6 @@ /** * @description + * * HTTP code snippet generator for the Shell using cURL. * * @author @@ -20,6 +21,31 @@ export interface CurlOptions { indent?: string | false; } +/** + * This is a const record with keys that correspond to the long names and values that correspond to the short names for cURL arguments. + */ +const params = { + globoff: 'g', + request: 'X', + 'url ': '', + 'http1.0': '0', + header: 'H', + cookie: 'b', + form: 'F', + data: 'd', +} as const; + +const getArg = (short: boolean) => (longName: keyof typeof params) => { + if (short) { + const shortName = params[longName]; + if (!shortName) { + return ''; + } + return `-${shortName}`; + } + return `--${longName}`; +}; + export const curl: Client = { info: { key: 'curl', @@ -35,19 +61,19 @@ export const curl: Client = { join: indent !== false ? ` \\\n${indent}` : ' ', }); - const globOption = short ? '-g' : '--globoff'; - const requestOption = short ? '-X' : '--request'; + const arg = getArg(short); + let formattedUrl = quote(fullUrl); - push(`curl ${requestOption} ${method}`); + push(`curl ${arg('request')} ${method}`); if (globOff) { formattedUrl = unescape(formattedUrl); - push(globOption); + push(arg('globoff')); } - push(`${short ? '' : '--url '}${formattedUrl}`); + push(`${arg('url ')}${formattedUrl}`); if (httpVersion === 'HTTP/1.0') { - push(short ? '-0' : '--http1.0'); + push(arg('http1.0')); } // if multipart form data, we want to remove the boundary @@ -73,11 +99,11 @@ export const curl: Client = { .sort() .forEach(key => { const header = `${key}: ${headersObj[key]}`; - push(`${short ? '-H' : '--header'} ${quote(header)}`); + push(`${arg('header')} ${quote(header)}`); }); if (allHeaders.cookie) { - push(`${short ? '-b' : '--cookie'} ${quote(allHeaders.cookie as string)}`); + push(`${arg('cookie')} ${quote(allHeaders.cookie as string)}`); } // construct post params @@ -91,7 +117,7 @@ export const curl: Client = { post = `${param.name}=${param.value}`; } - push(`${short ? '-F' : '--form'} ${quote(post)}`); + push(`${arg('form')} ${quote(post)}`); }); break; @@ -99,20 +125,18 @@ export const curl: Client = { if (postData.params) { postData.params.forEach(param => { push( - `${binary ? '--data-binary' : short ? '-d' : '--data'} ${quote( - `${param.name}=${param.value}`, - )}`, + `${binary ? '--data-binary' : arg('data')} ${quote(`${param.name}=${param.value}`)}`, ); }); } else { - push(`${binary ? '--data-binary' : short ? '-d' : '--data'} ${quote(postData.text)}`); + push(`${binary ? '--data-binary' : arg('data')} ${quote(postData.text)}`); } break; default: // raw request body if (postData.text) { - push(`${binary ? '--data-binary' : short ? '-d' : '--data'} ${quote(postData.text)}`); + push(`${binary ? '--data-binary' : arg('data')} ${quote(postData.text)}`); } } From 4cce62c8bc598baf7282b0f4715376ac8b0001f9 Mon Sep 17 00:00:00 2001 From: Dimitri Mitropoulos Date: Fri, 15 Jul 2022 15:57:38 -0400 Subject: [PATCH 3/4] urlencoding of params (and updates fixtures and adds custom fixture) --- src/fixtures/runCustomFixtures.ts | 27 +++++++++++-------- src/targets/shell/curl/client.test.ts | 16 +++++++++++ src/targets/shell/curl/client.ts | 4 ++- .../curl/fixtures/application-form-encoded.sh | 4 +-- .../shell/curl/fixtures/custom-indentation.sh | 2 +- src/targets/shell/curl/fixtures/full.sh | 2 +- .../shell/curl/fixtures/short-options.sh | 2 +- src/targets/shell/curl/fixtures/urlencode.sh | 5 ++++ 8 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 src/targets/shell/curl/fixtures/urlencode.sh diff --git a/src/fixtures/runCustomFixtures.ts b/src/fixtures/runCustomFixtures.ts index bea882996..3ffe90af9 100644 --- a/src/fixtures/runCustomFixtures.ts +++ b/src/fixtures/runCustomFixtures.ts @@ -1,4 +1,5 @@ -import { readFile } from 'fs/promises'; +import { writeFileSync } from 'fs'; +import { readFile, writeFile } from 'fs/promises'; import path from 'path'; import { HTTPSnippet, Request } from '../httpsnippet'; @@ -21,17 +22,21 @@ export interface CustomFixture { export const runCustomFixtures = ({ targetId, clientId, tests }: CustomFixture) => { describe(`custom fixtures for ${targetId}:${clientId}`, () => { tests.forEach(({ it: title, expected: fixtureFile, options, input: request }) => { + const result = new HTTPSnippet(request).convert(targetId, clientId, options); + const filePath = path.join( + __dirname, + '..', + 'targets', + targetId, + clientId, + 'fixtures', + fixtureFile, + ); + if (process.env.OVERWRITE_EVERYTHING) { + writeFileSync(filePath, String(result)); + } + it(title, async () => { - const result = new HTTPSnippet(request).convert(targetId, clientId, options); - const filePath = path.join( - __dirname, - '..', - 'targets', - targetId, - clientId, - 'fixtures', - fixtureFile, - ); const buffer = await readFile(filePath); const fixture = String(buffer); diff --git a/src/targets/shell/curl/client.test.ts b/src/targets/shell/curl/client.test.ts index 1142a34e1..9540a438f 100644 --- a/src/targets/shell/curl/client.test.ts +++ b/src/targets/shell/curl/client.test.ts @@ -1,3 +1,4 @@ +import applicationFormEncoded from '../../../fixtures/requests/application-form-encoded.json'; import full from '../../../fixtures/requests/full.json'; import nested from '../../../fixtures/requests/nested.json'; import { runCustomFixtures } from '../../../fixtures/runCustomFixtures'; @@ -71,5 +72,20 @@ runCustomFixtures({ }, expected: 'custom-indentation.sh', }, + { + it: 'should url encode the params key', + input: { + ...applicationFormEncoded, + postData: { + mimeType: 'application/x-www-form-urlencoded', + params: [ + { name: 'user name', value: 'John Doe' }, + { name: '$filter', value: 'by id' }, + ], + }, + } as Request, + options: {}, + expected: 'urlencode.sh', + }, ], }); diff --git a/src/targets/shell/curl/client.ts b/src/targets/shell/curl/client.ts index 2d4b1dc6b..fcc36c5cd 100644 --- a/src/targets/shell/curl/client.ts +++ b/src/targets/shell/curl/client.ts @@ -125,7 +125,9 @@ export const curl: Client = { if (postData.params) { postData.params.forEach(param => { push( - `${binary ? '--data-binary' : arg('data')} ${quote(`${param.name}=${param.value}`)}`, + `${binary ? '--data-binary' : '--data-urlencode'} ${quote( + `${encodeURIComponent(param.name)}=${param.value}`, + )}`, ); }); } else { diff --git a/src/targets/shell/curl/fixtures/application-form-encoded.sh b/src/targets/shell/curl/fixtures/application-form-encoded.sh index 8137bcc05..3206ced75 100644 --- a/src/targets/shell/curl/fixtures/application-form-encoded.sh +++ b/src/targets/shell/curl/fixtures/application-form-encoded.sh @@ -1,5 +1,5 @@ curl --request POST \ --url http://mockbin.com/har \ --header 'content-type: application/x-www-form-urlencoded' \ - --data foo=bar \ - --data hello=world \ No newline at end of file + --data-urlencode foo=bar \ + --data-urlencode hello=world \ No newline at end of file diff --git a/src/targets/shell/curl/fixtures/custom-indentation.sh b/src/targets/shell/curl/fixtures/custom-indentation.sh index fea12a989..78df6844d 100644 --- a/src/targets/shell/curl/fixtures/custom-indentation.sh +++ b/src/targets/shell/curl/fixtures/custom-indentation.sh @@ -3,4 +3,4 @@ curl --request POST \ @--header 'accept: application/json' \ @--header 'content-type: application/x-www-form-urlencoded' \ @--cookie 'foo=bar; bar=baz' \ -@--data foo=bar \ No newline at end of file +@--data-urlencode foo=bar \ No newline at end of file diff --git a/src/targets/shell/curl/fixtures/full.sh b/src/targets/shell/curl/fixtures/full.sh index 8c56d9807..e269ec47c 100644 --- a/src/targets/shell/curl/fixtures/full.sh +++ b/src/targets/shell/curl/fixtures/full.sh @@ -3,4 +3,4 @@ curl --request POST \ --header 'accept: application/json' \ --header 'content-type: application/x-www-form-urlencoded' \ --cookie 'foo=bar; bar=baz' \ - --data foo=bar \ No newline at end of file + --data-urlencode foo=bar \ No newline at end of file diff --git a/src/targets/shell/curl/fixtures/short-options.sh b/src/targets/shell/curl/fixtures/short-options.sh index ec2534571..0576496a8 100644 --- a/src/targets/shell/curl/fixtures/short-options.sh +++ b/src/targets/shell/curl/fixtures/short-options.sh @@ -1 +1 @@ -curl -X POST 'http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value' -H 'accept: application/json' -H 'content-type: application/x-www-form-urlencoded' -b 'foo=bar; bar=baz' -d foo=bar \ No newline at end of file +curl -X POST 'http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value' -H 'accept: application/json' -H 'content-type: application/x-www-form-urlencoded' -b 'foo=bar; bar=baz' --data-urlencode foo=bar \ No newline at end of file diff --git a/src/targets/shell/curl/fixtures/urlencode.sh b/src/targets/shell/curl/fixtures/urlencode.sh new file mode 100644 index 000000000..e55dfc117 --- /dev/null +++ b/src/targets/shell/curl/fixtures/urlencode.sh @@ -0,0 +1,5 @@ +curl --request POST \ + --url http://mockbin.com/har \ + --header 'content-type: application/x-www-form-urlencoded' \ + --data-urlencode 'user%20name=John Doe' \ + --data-urlencode '%24filter=by id' \ No newline at end of file From f2c115545f15f3a96df44cfd2acfa4ecef7c876a Mon Sep 17 00:00:00 2001 From: Dimitri Mitropoulos Date: Fri, 15 Jul 2022 17:32:22 -0400 Subject: [PATCH 4/4] only urlencode if necessary --- src/targets/shell/curl/client.ts | 11 ++++++----- .../shell/curl/fixtures/application-form-encoded.sh | 4 ++-- src/targets/shell/curl/fixtures/custom-indentation.sh | 2 +- src/targets/shell/curl/fixtures/full.sh | 2 +- src/targets/shell/curl/fixtures/short-options.sh | 2 +- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/targets/shell/curl/client.ts b/src/targets/shell/curl/client.ts index fcc36c5cd..463cec424 100644 --- a/src/targets/shell/curl/client.ts +++ b/src/targets/shell/curl/client.ts @@ -124,11 +124,12 @@ export const curl: Client = { case 'application/x-www-form-urlencoded': if (postData.params) { postData.params.forEach(param => { - push( - `${binary ? '--data-binary' : '--data-urlencode'} ${quote( - `${encodeURIComponent(param.name)}=${param.value}`, - )}`, - ); + const unencoded = param.name; + const encoded = encodeURIComponent(param.name); + const needsEncoding = encoded !== unencoded; + const name = needsEncoding ? encoded : unencoded; + const flag = binary ? '--data-binary' : `--data${needsEncoding ? '-urlencode' : ''}`; + push(`${flag} ${quote(`${name}=${param.value}`)}`); }); } else { push(`${binary ? '--data-binary' : arg('data')} ${quote(postData.text)}`); diff --git a/src/targets/shell/curl/fixtures/application-form-encoded.sh b/src/targets/shell/curl/fixtures/application-form-encoded.sh index 3206ced75..8137bcc05 100644 --- a/src/targets/shell/curl/fixtures/application-form-encoded.sh +++ b/src/targets/shell/curl/fixtures/application-form-encoded.sh @@ -1,5 +1,5 @@ curl --request POST \ --url http://mockbin.com/har \ --header 'content-type: application/x-www-form-urlencoded' \ - --data-urlencode foo=bar \ - --data-urlencode hello=world \ No newline at end of file + --data foo=bar \ + --data hello=world \ No newline at end of file diff --git a/src/targets/shell/curl/fixtures/custom-indentation.sh b/src/targets/shell/curl/fixtures/custom-indentation.sh index 78df6844d..fea12a989 100644 --- a/src/targets/shell/curl/fixtures/custom-indentation.sh +++ b/src/targets/shell/curl/fixtures/custom-indentation.sh @@ -3,4 +3,4 @@ curl --request POST \ @--header 'accept: application/json' \ @--header 'content-type: application/x-www-form-urlencoded' \ @--cookie 'foo=bar; bar=baz' \ -@--data-urlencode foo=bar \ No newline at end of file +@--data foo=bar \ No newline at end of file diff --git a/src/targets/shell/curl/fixtures/full.sh b/src/targets/shell/curl/fixtures/full.sh index e269ec47c..8c56d9807 100644 --- a/src/targets/shell/curl/fixtures/full.sh +++ b/src/targets/shell/curl/fixtures/full.sh @@ -3,4 +3,4 @@ curl --request POST \ --header 'accept: application/json' \ --header 'content-type: application/x-www-form-urlencoded' \ --cookie 'foo=bar; bar=baz' \ - --data-urlencode foo=bar \ No newline at end of file + --data foo=bar \ No newline at end of file diff --git a/src/targets/shell/curl/fixtures/short-options.sh b/src/targets/shell/curl/fixtures/short-options.sh index 0576496a8..9aa2e7f51 100644 --- a/src/targets/shell/curl/fixtures/short-options.sh +++ b/src/targets/shell/curl/fixtures/short-options.sh @@ -1 +1 @@ -curl -X POST 'http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value' -H 'accept: application/json' -H 'content-type: application/x-www-form-urlencoded' -b 'foo=bar; bar=baz' --data-urlencode foo=bar \ No newline at end of file +curl -X POST 'http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value' -H 'accept: application/json' -H 'content-type: application/x-www-form-urlencoded' -b 'foo=bar; bar=baz' --data foo=bar \ No newline at end of file