Skip to content

Commit 3d16399

Browse files
fix(issue #119): Escape empty brackets [] when present in query parameters (#121)
* fix(issue #119): Escape empty brackets [] when present If the url has query parameters and they contain empty brackets, they need to be escaped for the callback function in HttpSteps to complete normally. As done by escapeBrackets in MockFaster. If the given step, has empty brackets but in the url path this time. Mockserver won't be able to compile the path url to a regex and crash. But for path url you can decode the empty brackets into their UTF-8 encoding, i.e. %5B for [ and %5D for ], and mockserver will still match the request with the mock. This method of using UTF-8 encoding for the brackets didn't work for the query parameters, otherwise Mockserver won't be able to match the mock parameter with the real one, so we have to escape them. Co-authored-by: brian-mulier <[email protected]>
1 parent b4d2e87 commit 3d16399

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

mockfaster/src/main/java/com/decathlon/tzatziki/utils/MockFaster.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class MockFaster {
5050
.withReasonPhrase("Not Found Again!")
5151
.withStatusCode(404);
5252

53+
public static final String INVALID_REGEX_PATTERN = "(\\[])";
5354
private static final String PROTOCOL = "(?:([^:]+)://)?";
5455
private static final String HOST = "([^/]+)?";
5556
private static final Pattern URI = Pattern.compile("^" + PROTOCOL + HOST + "((/[^?]+)?(?:\\?(.+))?)?$");
@@ -377,4 +378,9 @@ public void respond(ExpectationResponseCallback callback) {
377378
add_mock(request, callback, comparison);
378379
}
379380
}
381+
382+
public static String escapeBrackets(String string) {
383+
Matcher matcher = Pattern.compile(INVALID_REGEX_PATTERN).matcher(string);
384+
return matcher.replaceAll("\\\\[\\\\]");
385+
}
380386
}

tzatziki-http/src/main/java/com/decathlon/tzatziki/steps/HttpSteps.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void url_is_mocked_as(String path, Interaction interaction, Comparison co
160160
interaction.consumptionIndex++;
161161

162162
String queryParamPattern = ofNullable(uri.group(5)).filter(s -> !s.isEmpty()).map(s -> "?" + toQueryString(toParameters(s, false))).orElse("");
163-
Pattern urlPattern = Pattern.compile(uri.group(4) + queryParamPattern);
163+
Pattern urlPattern = Pattern.compile(escapeBrackets(uri.group(4) + queryParamPattern));
164164
objects.add("_request", request);
165165

166166
AtomicInteger consumptionSum = new AtomicInteger();
@@ -373,7 +373,7 @@ public void we_receive_a_status_and(Guard guard, HttpStatusCode status, Comparis
373373
we_receive(guard, comparison, type, content);
374374
}
375375

376-
@Then(THAT + GUARD + QUOTED_CONTENT + " has received(?: "+ VERIFICATION+")? " + COUNT_OR_VARIABLE + " " + CALL + "(?: " + VARIABLE + ")?$")
376+
@Then(THAT + GUARD + QUOTED_CONTENT + " has received(?: " + VERIFICATION + ")? " + COUNT_OR_VARIABLE + " " + CALL + "(?: " + VARIABLE + ")?$")
377377
public void mockserver_has_received(Guard guard, String path, String verification, String countAsString, Method method, String variable) {
378378
guard.in(objects, () -> {
379379
int expectedNbCalls = objects.getCount(countAsString);

tzatziki-http/src/test/resources/com/decathlon/tzatziki/steps/http.feature

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,11 @@ Feature: to interact with an http service and setup mocks
12051205
"""
12061206
Then we received a status OK_200
12071207

1208+
Scenario: Brackets should be handled and escaped properly for HTTP mocks
1209+
Given that getting "http://invalid/regex%5B%5D?re[]toto[]=1" will return a status OK_200
1210+
When we get "http://invalid/regex[]?re[]toto[]=1"
1211+
Then we received a status OK_200
1212+
12081213
Scenario Template: Exceed max amount of expectation
12091214
Given we add 1-1 mocks for id endpoint
12101215
Given we add <mocksRange> mocks for id endpoint
@@ -1216,7 +1221,7 @@ Feature: to interact with an http service and setup mocks
12161221
| mocksRange |
12171222
| 2-150 |
12181223
| 151-250 |
1219-
1224+
12201225
@ignore @run-manually
12211226
Scenario Template: Mocks from other tests should be considered as unhandled requests
12221227
* a root logger set to INFO
@@ -1227,4 +1232,4 @@ Feature: to interact with an http service and setup mocks
12271232
Examples:
12281233
| idx |
12291234
| 1 |
1230-
| 2 |
1235+
| 2 |

0 commit comments

Comments
 (0)