From dd0ba857c21228dd0acc4bea55672b3f5ada0b12 Mon Sep 17 00:00:00 2001 From: Shrikant Kandula Date: Tue, 16 Jun 2020 09:59:43 +0530 Subject: [PATCH] Fix cURL import when using `--url` argument Also fixes cases where there's a space between `-X` and `POST` and similar cases. --- .../server/services/CurlImporterService.java | 16 +++++++++++---- .../services/CurlImporterServiceTest.java | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/CurlImporterService.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/CurlImporterService.java index 1fbe002a7c..60e2febc82 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/CurlImporterService.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/CurlImporterService.java @@ -190,7 +190,9 @@ public class CurlImporterService extends BaseApiImporter { } else if (token.startsWith("-d")) { // `-dstuff` -> `--data stuff` normalizedTokens.add(ARG_DATA); - normalizedTokens.add(token.substring(2)); + if (token.length() > 2) { + normalizedTokens.add(token.substring(2)); + } } else if ("-H".equals(token)) { normalizedTokens.add(ARG_HEADER); @@ -198,7 +200,9 @@ public class CurlImporterService extends BaseApiImporter { } else if (token.startsWith("-H")) { // `-HContent-Type:application/json` -> `--header Content-Type:application/json` normalizedTokens.add(ARG_HEADER); - normalizedTokens.add(token.substring(2)); + if (token.length() > 2) { + normalizedTokens.add(token.substring(2)); + } } else if ("-X".equals(token)) { normalizedTokens.add(ARG_REQUEST); @@ -206,7 +210,9 @@ public class CurlImporterService extends BaseApiImporter { } else if (token.startsWith("-X")) { // `-XGET` -> `--request GET` normalizedTokens.add(ARG_REQUEST); - normalizedTokens.add(token.substring(2).toUpperCase()); + if (token.length() > 2) { + normalizedTokens.add(token.substring(2).toUpperCase()); + } } else if ("-b".equals(token)) { normalizedTokens.add(ARG_COOKIE); @@ -217,7 +223,9 @@ public class CurlImporterService extends BaseApiImporter { } else if ("-A".equals(token)) { normalizedTokens.add(ARG_USER_AGENT); - } else { + } else if (!"--url".equals(token)) { + // We skip the `--url` argument since it's superfluous and URLs are directly sniffed out of the argument + // list. The `--url` argument holds no special significance in cURL. normalizedTokens.add(token); } diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/CurlImporterServiceTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/CurlImporterServiceTest.java index 58b2a2d141..4042b287e0 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/CurlImporterServiceTest.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/CurlImporterServiceTest.java @@ -518,6 +518,26 @@ public class CurlImporterServiceTest { assertEmptyBody(action); } + @Test + public void parseWithDashedUrlArgument() { + Action action = curlImporterService.curlToAction("curl --url http://api.sloths.com"); + assertMethod(action, HttpMethod.GET); + assertUrl(action, "http://api.sloths.com"); + assertEmptyPath(action); + assertEmptyHeaders(action); + assertEmptyBody(action); + } + + @Test + public void parseWithDashedUrlArgument2() { + Action action = curlImporterService.curlToAction("curl -X POST -d '{\"name\":\"test\",\"salary\":\"123\",\"age\":\"23\"}' --url http://dummy.restapiexample.com/api/v1/create"); + assertMethod(action, HttpMethod.POST); + assertUrl(action, "http://dummy.restapiexample.com"); + assertPath(action, "/api/v1/create"); + assertHeaders(action, new Property("Content-Type", "application/x-www-form-urlencoded")); + assertBody(action, "{\"name\":\"test\",\"salary\":\"123\",\"age\":\"23\"}"); + } + @Test @WithUserDetails(value = "api_user") public void importInvalidCurlCommand() {