Skip to content

Commit b7db6ce

Browse files
BAEL-276 - Refactoring encoding test
1 parent 426ac12 commit b7db6ce

1 file changed

Lines changed: 35 additions & 22 deletions

File tree

‎core-java/src/test/java/com/baeldung/encoderdecoder/EncoderDecoderUnitTest.java‎

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
import java.util.Map;
1515

1616
import static java.util.stream.Collectors.joining;
17+
import static org.hamcrest.CoreMatchers.*;
1718

1819
public class EncoderDecoderUnitTest {
1920

2021
private static final Logger LOGGER = LoggerFactory.getLogger(EncoderDecoderUnitTest.class);
21-
private static final String testUrl = "http://www.baeldung.com/path+1?key1=value+1&key2=value%40%21%242&key3=value%253#dummy+Fragment";
22+
private static final String testUrl = "http://www.baeldung.com?key1=value+1&key2=value%40%21%242&key3=value%253";
23+
private static final String testUrlWithPath = "http://www.baeldung.com/path+1?key1=value+1&key2=value%40%21%242&key3=value%253";
24+
2225

2326
private String encodeValue(String value) {
2427
String encoded = null;
@@ -42,13 +45,11 @@ private String decode(String value) {
4245

4346
@Test
4447
public void givenURL_whenAnalyze_thenCorrect() throws Exception {
45-
URI uri = new URI(testUrl);
48+
URL url = new URL(testUrl);
4649

47-
Assert.assertThat(uri.getScheme(), CoreMatchers.is("http"));
48-
Assert.assertThat(uri.getHost(), CoreMatchers.is("www.baeldung.com"));
49-
Assert.assertThat(uri.getRawPath(), CoreMatchers.is("/path+1"));
50-
Assert.assertThat(uri.getRawQuery(), CoreMatchers.is("key1=value+1&key2=value%40%21%242&key3=value%253"));
51-
Assert.assertThat(uri.getRawFragment(), CoreMatchers.is("dummy+Fragment"));
50+
Assert.assertThat(url.getProtocol(), is("http"));
51+
Assert.assertThat(url.getHost(), is("www.baeldung.com"));
52+
Assert.assertThat(url.getQuery(), is("key1=value+1&key2=value%40%21%242&key3=value%253"));
5253
}
5354

5455
@Test
@@ -58,35 +59,29 @@ public void givenRequestParam_whenUTF8Scheme_thenEncode() throws Exception {
5859
requestParams.put("key2", "value@!$2");
5960
requestParams.put("key3", "value%3");
6061

61-
String path = "path+1";
62-
String fragment = "dummy Fragment";
63-
64-
String encodedURL = requestParams.keySet().stream().map(key -> key + "=" + encodeValue(requestParams.get(key))).collect(joining("&", "http://www.baeldung.com/" + getPath(path) + "?", "#" + encodeValue(fragment)));
62+
String encodedURL = requestParams.keySet().stream()
63+
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
64+
.collect(joining("&", "http://www.baeldung.com?", ""));
6565

66-
Assert.assertThat(testUrl, CoreMatchers.is(encodedURL));
66+
Assert.assertThat(testUrl, is(encodedURL));
6767
}
6868

6969
@Test
7070
public void givenRequestParam_whenUTF8Scheme_thenDecodeRequestParams() throws Exception {
71-
URI uri = new URI(testUrl);
71+
URL url = new URL(testUrl);
7272

73-
String scheme = uri.getScheme();
74-
String host = uri.getHost();
75-
String query = uri.getRawQuery();
76-
String path = uri.getRawPath();
77-
String fragment = uri.getRawFragment();
73+
String query = url.getQuery();
7874

7975
String decodedQuery = Arrays.stream(query.split("&")).map(param -> param.split("=")[0] + "=" + decode(param.split("=")[1])).collect(joining("&"));
80-
String decodedPath = Arrays.stream(path.split("/")).map(param -> getPath(param)).collect(joining("/"));
8176

82-
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));
77+
Assert.assertEquals("http://www.baeldung.com?key1=value 1&key2=value@!$2&key3=value%3", url.getProtocol() + "://" + url.getHost() + "?" + decodedQuery);
8378
}
8479

85-
private String getPath(String path) {
80+
private String encodePath(String path) {
8681
try {
8782
path = new URI(null, null, path, null).getPath();
8883
} catch (URISyntaxException e) {
89-
e.printStackTrace();
84+
LOGGER.error("Error encoding parameter {}", e.getMessage(), e);
9085
}
9186
return path;
9287
}
@@ -98,4 +93,22 @@ public void givenPath_thenEncodeDecodePath() throws URISyntaxException {
9893
Assert.assertEquals("/Path 1/Path+2", uri.getPath());
9994
Assert.assertEquals("/Path%201/Path+2", uri.getRawPath());
10095
}
96+
97+
@Test
98+
public void givenPathAndRequestParam_whenUTF8Scheme_thenEncode() throws Exception {
99+
Map<String, String> requestParams = new HashMap<>();
100+
requestParams.put("key1", "value 1");
101+
requestParams.put("key2", "value@!$2");
102+
requestParams.put("key3", "value%3");
103+
104+
String path = "path+1";
105+
106+
String encodedURL = requestParams.keySet().stream()
107+
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
108+
.collect(joining("&", "http://www.baeldung.com/" + encodePath(path) + "?", ""));
109+
110+
Assert.assertThat(testUrlWithPath, is(encodedURL));
111+
}
112+
113+
101114
}

0 commit comments

Comments
 (0)