From 41992976ec45cf641ea47415ed0595c69a565bbd Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Tue, 15 Nov 2016 15:29:27 +0530 Subject: [PATCH] BAEL-276: Added different URL parts for encoding and decoding --- .../EncoderDecoderUnitTest.java | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 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 c944dfa6fe..38df7855eb 100644 --- a/core-java/src/test/java/com/baeldung/encoderdecoder/EncoderDecoderUnitTest.java +++ b/core-java/src/test/java/com/baeldung/encoderdecoder/EncoderDecoderUnitTest.java @@ -7,9 +7,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.UnsupportedEncodingException; -import java.net.URL; -import java.net.URLDecoder; -import java.net.URLEncoder; +import java.net.*; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.HashMap; @@ -20,7 +18,7 @@ import static java.util.stream.Collectors.joining; public class EncoderDecoderUnitTest { private static final Logger LOGGER = LoggerFactory.getLogger(EncoderDecoderUnitTest.class); - private static final String testUrl = "http://www.baeldung.com?key1=value+1&key2=value%40%21%242&key3=value%253"; + private static final String testUrl = "http://www.baeldung.com/path+1?key1=value+1&key2=value%40%21%242&key3=value%253#dummy+Fragment"; private String encodeValue(String value) { String encoded = null; @@ -44,11 +42,13 @@ public class EncoderDecoderUnitTest { @Test public void givenURL_whenAnalyze_thenCorrect() throws Exception { - URL url = new URL(testUrl); + URI uri = new URI(testUrl); - Assert.assertThat(url.getProtocol(), CoreMatchers.is("http")); - Assert.assertThat(url.getHost(), CoreMatchers.is("www.baeldung.com")); - Assert.assertThat(url.getQuery(), CoreMatchers.is("key1=value+1&key2=value%40%21%242&key3=value%253")); + Assert.assertThat(uri.getScheme(), CoreMatchers.is("http")); + Assert.assertThat(uri.getHost(), CoreMatchers.is("www.baeldung.com")); + Assert.assertThat(uri.getRawPath(), CoreMatchers.is("/path+1")); + Assert.assertThat(uri.getRawQuery(), CoreMatchers.is("key1=value+1&key2=value%40%21%242&key3=value%253")); + Assert.assertThat(uri.getRawFragment(), CoreMatchers.is("dummy+Fragment")); } @Test @@ -58,20 +58,27 @@ public class EncoderDecoderUnitTest { requestParams.put("key2", "value@!$2"); requestParams.put("key3", "value%3"); - String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com?", "")); + 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))); Assert.assertThat(testUrl, CoreMatchers.is(encodedURL)); } @Test public void givenRequestParam_whenUTF8Scheme_thenDecodeRequestParams() throws Exception { - URL url = new URL(testUrl); + URI uri = new URI(testUrl); - String query = url.getQuery(); + String scheme = uri.getScheme(); + String host = uri.getHost(); + String query = uri.getRawQuery(); + String path = uri.getRawPath(); + 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("/")); - Assert.assertEquals("http://www.baeldung.com?key1=value 1&key2=value@!$2&key3=value%3", url.getProtocol() + "://" + url.getHost() + "?" + decodedQuery); + 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)); } - }