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..edc6aac402 --- /dev/null +++ b/apps/testing/src/test/java/com/akto/test_editor/execution/TestVariableResolver.java @@ -0,0 +1,47 @@ +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); + + result = VariableResolver.resolveExpression(varMap, "akto"); + assertEquals("akto", result); + } + +}