Skip to content

Commit a17457d

Browse files
rmfcnbknagy
authored andcommitted
DIME-6543: the built-in java URL only accepts absolute URLs, which prevents us using relative paths. The URI already has this information, so there is no need to convert it to URL, and the escher does not use the host (presented or not), so we can go with relative paths.
1 parent 445b167 commit a17457d

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/main/java/com/emarsys/escher/Helper.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,9 @@ public String canonicalize(EscherRequest request, List<String> signedHeaders) th
4141
}
4242

4343

44-
private String canonicalizePath(EscherRequest request) throws EscherException {
45-
try {
46-
String path = request.getURI().toURL().getPath();
47-
return path.equals("") ? "/" : path;
48-
} catch (MalformedURLException e) {
49-
throw new EscherException(e);
50-
}
44+
private String canonicalizePath(EscherRequest request) {
45+
String path = request.getURI().getRawPath();
46+
return path.isEmpty() ? "/" : path;
5147
}
5248

5349

@@ -72,7 +68,7 @@ private String queryParameterToString(NameValuePair entry) {
7268

7369

7470
public static String encodeURIComponent(String s) throws UnsupportedEncodingException {
75-
// We need this to be uri encoded (' ' => '%20') not x-www-form-urlencoded (' ' => '+') with
71+
// We need this to be uri encoded (' ' => '%20') not x-www-form-urlencoded (' ' => '+') with
7672
// some of the RFC3986 reserved characters kept as they were (-._~) which is used by URLEncoder.
7773
// This will result an encoding method similar to other escher libs.
7874
return URLEncoder.encode(s, "UTF-8")

src/test/java/com/emarsys/escher/HelperTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,23 @@ public void testCanonicalizeWithUrlParamsContainingSpecialCharacters() throws Ex
140140
assertThat(canonicalized, containsString("specialchars=-_.~"));
141141
}
142142

143+
@Test
144+
public void testCanonicalizeWithRelativeUri() throws Exception {
145+
TestParam param = parseTestData("get-vanilla");
146+
TestParam.Request paramRequest = param.getRequest();
147+
paramRequest.setUrl(paramRequest.getUrl() + "?name=John%2BWick");
148+
149+
URI relativeUri = new URI(paramRequest.getUrl());
150+
151+
EscherRequestImpl request = new EscherRequestImpl(paramRequest.getMethod(), relativeUri, new ArrayList<>(), paramRequest.getBody());
152+
153+
Helper helper = new Helper(createConfig(param));
154+
155+
String canonicalized = helper.canonicalize(request, param.getHeadersToSign());
156+
157+
assertThat(canonicalized, containsString("name=John%2BWick"));
158+
}
159+
143160

144161
@Test
145162
@UseDataProvider("getAddMandatoryHeadersCases")

0 commit comments

Comments
 (0)