From bdeec43f42084fdb62113ec6e6cd36e6fdc59e6f Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Thu, 1 Dec 2016 11:42:59 +0530 Subject: [PATCH] BAEL-276: Added test case for path component --- .../EncoderDecoderUnitTest.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/encoderdecoder/EncoderDecoderUnitTest.java b/core-java/src/test/java/com/baeldung/encoderdecoder/EncoderDecoderUnitTest.java index 38df7855eb..0b63d2b51a 100644 --- a/core-java/src/test/java/com/baeldung/encoderdecoder/EncoderDecoderUnitTest.java +++ b/core-java/src/test/java/com/baeldung/encoderdecoder/EncoderDecoderUnitTest.java @@ -58,10 +58,10 @@ public class EncoderDecoderUnitTest { requestParams.put("key2", "value@!$2"); requestParams.put("key3", "value%3"); - String path = "path 1"; + String path = "path+1"; String fragment = "dummy Fragment"; - String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com/" + encodeValue(path) + "?", "#" + encodeValue(fragment))); + String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com/" + getPath(path) + "?", "#" + encodeValue(fragment))); Assert.assertThat(testUrl, CoreMatchers.is(encodedURL)); } @@ -77,8 +77,25 @@ public class EncoderDecoderUnitTest { String fragment = uri.getRawFragment(); String decodedQuery = Arrays.stream(query.split("&")).map(param -> param.split("=")[0] + "=" + decode(param.split("=")[1])).collect(joining("&")); - String decodedPath = Arrays.stream(path.split("/")).map(param -> decode(param)).collect(joining("/")); + String decodedPath = Arrays.stream(path.split("/")).map(param -> getPath(param)).collect(joining("/")); - Assert.assertEquals("http://www.baeldung.com/path 1?key1=value 1&key2=value@!$2&key3=value%3#dummy Fragment", scheme + "://" + host + decodedPath + "?" + decodedQuery + "#" + decode(fragment)); + Assert.assertEquals("http://www.baeldung.com/path+1?key1=value 1&key2=value@!$2&key3=value%3#dummy Fragment", scheme + "://" + host + decodedPath + "?" + decodedQuery + "#" + decode(fragment)); + } + + private String getPath(String path) { + try { + path = new URI(null, null, path, null).getPath(); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + return path; + } + + @Test + public void givenPath_thenEncodeDecodePath() throws URISyntaxException { + URI uri = new URI(null, null, "/Path 1/Path+2", null); + + Assert.assertEquals("/Path 1/Path+2", uri.getPath()); + Assert.assertEquals("/Path%201/Path+2", uri.getRawPath()); } }