From d70f3462f36687f465d9815a33def0c7c001a8a8 Mon Sep 17 00:00:00 2001 From: Avneesh Hota Date: Tue, 17 Oct 2023 16:28:15 +0530 Subject: [PATCH 1/2] replace multiple differents vars --- .../execution/VariableResolver.java | 17 ++++--- .../execution/TestVariableResolver.java | 44 +++++++++++++++++++ 2 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 apps/testing/src/test/java/com/akto/test_editor/execution/TestVariableResolver.java diff --git a/apps/testing/src/main/java/com/akto/test_editor/execution/VariableResolver.java b/apps/testing/src/main/java/com/akto/test_editor/execution/VariableResolver.java index 15906b8105..9fabbdfca9 100644 --- a/apps/testing/src/main/java/com/akto/test_editor/execution/VariableResolver.java +++ b/apps/testing/src/main/java/com/akto/test_editor/execution/VariableResolver.java @@ -26,26 +26,25 @@ public static String resolveExpression(Map varMap, String expres Pattern pattern = Pattern.compile("\\$\\{[^}]*\\}"); Matcher matcher = pattern.matcher(expression); - if (matcher.find()) { + while (matcher.find()) { try { String match = matcher.group(0); match = match.substring(2, match.length()); match = match.substring(0, match.length() - 1); Object val = getValue(varMap, match); String valString = val.toString(); - expression = expression.replaceAll("(\\$\\{[^}]*\\})", valString); + expression = expression.replaceFirst("(\\$\\{[^}]*\\})", valString); } catch (Exception e) { return expression; } + } + + Object val = getValue(varMap, expression); + if (val == null) { + return expression; } else { - Object val = getValue(varMap, expression); - if (val == null) { - return expression; - } else { - return val.toString(); - } + return val.toString(); } - return expression; } diff --git a/apps/testing/src/test/java/com/akto/test_editor/execution/TestVariableResolver.java b/apps/testing/src/test/java/com/akto/test_editor/execution/TestVariableResolver.java new file mode 100644 index 0000000000..eb94bf744f --- /dev/null +++ b/apps/testing/src/test/java/com/akto/test_editor/execution/TestVariableResolver.java @@ -0,0 +1,44 @@ +package com.akto.test_editor.execution; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static junit.framework.TestCase.assertEquals; + +public class TestVariableResolver { + + @Test + public void testResolveExpression() { + Map varMap = new HashMap<>(); + varMap.put("var1", "user1"); + varMap.put("var2", "user2"); + String result = VariableResolver.resolveExpression(varMap, "${var1}"); + assertEquals("user1", result); + result = VariableResolver.resolveExpression(varMap, "var1"); + assertEquals("user1", result); + + result = VariableResolver.resolveExpression(varMap, "${var1}!!!"); + assertEquals("user1!!!", result); + result = VariableResolver.resolveExpression(varMap, "var1!!!"); + assertEquals("var1!!!", result); + + result = VariableResolver.resolveExpression(varMap, "${var1}${var2}"); + assertEquals("user1user2", result); + result = VariableResolver.resolveExpression(varMap, "var1${var2}"); + assertEquals("var1user2", result); + + result = VariableResolver.resolveExpression(varMap, "${var1}&${var2}"); + assertEquals("user1&user2", result); + result = VariableResolver.resolveExpression(varMap, "var1&${var2}"); + assertEquals("var1&user2", result); + + result = VariableResolver.resolveExpression(varMap, "${var3}"); + assertEquals("${var3}", result); + + result = VariableResolver.resolveExpression(varMap, "${var1}${var1}"); + assertEquals("user1user1", result); + } + +} From 6736ee949c89e2189e2258634e09d055e6bc9ecb Mon Sep 17 00:00:00 2001 From: Avneesh Hota Date: Tue, 17 Oct 2023 16:31:52 +0530 Subject: [PATCH 2/2] added unit test --- .../com/akto/test_editor/execution/TestVariableResolver.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apps/testing/src/test/java/com/akto/test_editor/execution/TestVariableResolver.java b/apps/testing/src/test/java/com/akto/test_editor/execution/TestVariableResolver.java index eb94bf744f..edc6aac402 100644 --- a/apps/testing/src/test/java/com/akto/test_editor/execution/TestVariableResolver.java +++ b/apps/testing/src/test/java/com/akto/test_editor/execution/TestVariableResolver.java @@ -39,6 +39,9 @@ public void testResolveExpression() { result = VariableResolver.resolveExpression(varMap, "${var1}${var1}"); assertEquals("user1user1", result); + + result = VariableResolver.resolveExpression(varMap, "akto"); + assertEquals("akto", result); } }