diff --git a/src/main/java/com/emarsys/escher/Helper.java b/src/main/java/com/emarsys/escher/Helper.java index 5072369..7b7201e 100644 --- a/src/main/java/com/emarsys/escher/Helper.java +++ b/src/main/java/com/emarsys/escher/Helper.java @@ -41,13 +41,9 @@ public String canonicalize(EscherRequest request, List signedHeaders) th } - private String canonicalizePath(EscherRequest request) throws EscherException { - try { - String path = request.getURI().toURL().getPath(); - return path.equals("") ? "/" : path; - } catch (MalformedURLException e) { - throw new EscherException(e); - } + private String canonicalizePath(EscherRequest request) { + String path = request.getURI().getRawPath(); + return path.isEmpty() ? "/" : path; } @@ -72,7 +68,7 @@ private String queryParameterToString(NameValuePair entry) { public static String encodeURIComponent(String s) throws UnsupportedEncodingException { - // We need this to be uri encoded (' ' => '%20') not x-www-form-urlencoded (' ' => '+') with + // We need this to be uri encoded (' ' => '%20') not x-www-form-urlencoded (' ' => '+') with // some of the RFC3986 reserved characters kept as they were (-._~) which is used by URLEncoder. // This will result an encoding method similar to other escher libs. return URLEncoder.encode(s, "UTF-8") diff --git a/src/test/java/com/emarsys/escher/HelperTest.java b/src/test/java/com/emarsys/escher/HelperTest.java index a9ae6b1..377517e 100644 --- a/src/test/java/com/emarsys/escher/HelperTest.java +++ b/src/test/java/com/emarsys/escher/HelperTest.java @@ -140,6 +140,23 @@ public void testCanonicalizeWithUrlParamsContainingSpecialCharacters() throws Ex assertThat(canonicalized, containsString("specialchars=-_.~")); } + @Test + public void testCanonicalizeWithRelativeUri() throws Exception { + TestParam param = parseTestData("get-vanilla"); + TestParam.Request paramRequest = param.getRequest(); + paramRequest.setUrl(paramRequest.getUrl() + "?name=John%2BWick"); + + URI relativeUri = new URI(paramRequest.getUrl()); + + EscherRequestImpl request = new EscherRequestImpl(paramRequest.getMethod(), relativeUri, new ArrayList<>(), paramRequest.getBody()); + + Helper helper = new Helper(createConfig(param)); + + String canonicalized = helper.canonicalize(request, param.getHeadersToSign()); + + assertThat(canonicalized, containsString("name=John%2BWick")); + } + @Test @UseDataProvider("getAddMandatoryHeadersCases")