BAEL-276: Added different URL parts for encoding and decoding
This commit is contained in:
parent
32fe3d7489
commit
41992976ec
|
@ -7,9 +7,7 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URL;
|
import java.net.*;
|
||||||
import java.net.URLDecoder;
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -20,7 +18,7 @@ import static java.util.stream.Collectors.joining;
|
||||||
public class EncoderDecoderUnitTest {
|
public class EncoderDecoderUnitTest {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(EncoderDecoderUnitTest.class);
|
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) {
|
private String encodeValue(String value) {
|
||||||
String encoded = null;
|
String encoded = null;
|
||||||
|
@ -44,11 +42,13 @@ public class EncoderDecoderUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenURL_whenAnalyze_thenCorrect() throws Exception {
|
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(uri.getScheme(), CoreMatchers.is("http"));
|
||||||
Assert.assertThat(url.getHost(), CoreMatchers.is("www.baeldung.com"));
|
Assert.assertThat(uri.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.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
|
@Test
|
||||||
|
@ -58,20 +58,27 @@ public class EncoderDecoderUnitTest {
|
||||||
requestParams.put("key2", "value@!$2");
|
requestParams.put("key2", "value@!$2");
|
||||||
requestParams.put("key3", "value%3");
|
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));
|
Assert.assertThat(testUrl, CoreMatchers.is(encodedURL));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenRequestParam_whenUTF8Scheme_thenDecodeRequestParams() throws Exception {
|
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 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue