Skip to content

Commit dae343c

Browse files
authored
Merge pull request #2714 from srmppn/feature/fix-forward-slashes
Fix forward slashes being turned into escape
2 parents 98469ec + 82fd008 commit dae343c

5 files changed

Lines changed: 21 additions & 9 deletions

File tree

karate-core/src/main/java/com/intuit/karate/JsonUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public class JsonUtils {
6464

6565
private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);
6666

67+
private static final JSONStyle JSON_STYLE = new JSONStyle(JSONStyle.FLAG_PROTECT_4WEB);
68+
6769
private JsonUtils() {
6870
// only static methods
6971
}
@@ -108,7 +110,7 @@ public static String toStrictJson(String raw) {
108110
JSONParser jp = new JSONParser(JSONParser.MODE_PERMISSIVE);
109111
try {
110112
Object o = jp.parse(raw);
111-
return JSONValue.toJSONString(o);
113+
return JSONValue.toJSONString(o, JSON_STYLE);
112114
} catch (Exception e) {
113115
throw new RuntimeException(e);
114116
}
@@ -121,7 +123,7 @@ public static String toJson(Object o) {
121123
public static String toJson(Object o, boolean pretty) {
122124
if (!pretty) { // TODO use JSONStyleIdent in json-smart 2.4
123125
try {
124-
return JSONValue.toJSONString(o);
126+
return JSONValue.toJSONString(o, JSON_STYLE);
125127
} catch (Throwable t) {
126128
logger.warn("object to json serialization failure, trying alternate approach: {}", t.getMessage());
127129
}

karate-core/src/main/java/com/intuit/karate/core/Variable.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ public String getAsString() {
228228
case LIST:
229229
case MAP:
230230
try {
231-
return JsonUtils.toJson(value);
232-
} catch (Throwable t) {
233-
logger.warn("conversion to json string failed, will attempt to use fall-back approach: {}", t.getMessage());
234-
return JsonUtils.toJsonSafe(value, false);
235-
}
231+
return JsonUtils.toJson(value);
232+
} catch (Throwable t) {
233+
logger.warn("conversion to json string failed, will attempt to use fall-back approach: {}", t.getMessage());
234+
return JsonUtils.toJsonSafe(value, false);
235+
}
236236
case XML:
237237
return XmlUtils.toString(getValue());
238238
default:

karate-core/src/test/java/com/intuit/karate/JsonUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class JsonUtilsTest {
2222
@Test
2323
void testParse() {
2424
String temp = JsonUtils.toStrictJson("{redirect:{url:'/index'}}");
25-
assertEquals(temp, "{\"redirect\":{\"url\":\"\\/index\"}}");
25+
assertEquals("{\"redirect\":{\"url\":\"/index\"}}", temp);
2626
}
2727

2828
@Test

karate-core/src/test/java/com/intuit/karate/core/HttpMockHandlerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,14 @@ void testKeepOriginalResponseHeaders() {
120120
matchContains(response.getHeaders().keySet(), "X-Special-Header");
121121
matchContains(response.getHeaders().keySet(), "X-Custom-Header");
122122
}
123+
124+
@Test
125+
void testResponseHavingForwardSlashes() {
126+
background()
127+
.scenario(
128+
"pathMatches('/hello')",
129+
"def response = { 'url': 'https://test123' }");
130+
response = handle().path("/hello").invoke("get");
131+
match(response.getBody(), "{\"url\":\"https://test123\"}".getBytes());
132+
}
123133
}

karate-core/src/test/java/com/intuit/karate/core/StepRuntimeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static Stream<Arguments> testParameters() throws ClassNotFoundException,
101101
com.intuit.karate.ScenarioActions.class.getMethod("print", String.class),
102102
new ArrayList<String>() { { add("\"name:\", name"); }},
103103
"print \"name:\", name"),
104-
Arguments.of("com.intuit.karate.ScenarioActions.print(java.lang.String) [\"'test with\\/slash'\"]", // JSON escapes forward slash
104+
Arguments.of("com.intuit.karate.ScenarioActions.print(java.lang.String) [\"'test with/slash'\"]", // JSON with forward slash
105105
com.intuit.karate.ScenarioActions.class,
106106
com.intuit.karate.ScenarioActions.class.getMethod("print", String.class),
107107
new ArrayList<String>() { { add("'test with/slash'"); }},

0 commit comments

Comments
 (0)