Skip to content

Commit d55903a

Browse files
BAEL-276 - URL encoding
1 parent a65bad7 commit d55903a

1 file changed

Lines changed: 58 additions & 12 deletions

File tree

‎core-java-8/src/test/java/com/baeldung/encoderdecoder/EncoderDecoder.java‎

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,82 @@
33
import org.hamcrest.CoreMatchers;
44
import org.junit.Assert;
55
import org.junit.Test;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
68

79
import java.io.UnsupportedEncodingException;
10+
import java.net.MalformedURLException;
11+
import java.net.URL;
812
import java.net.URLDecoder;
913
import java.net.URLEncoder;
1014
import java.nio.charset.StandardCharsets;
15+
import java.util.Arrays;
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
import java.util.function.Function;
19+
import java.util.stream.Collectors;
1120

1221
public class EncoderDecoder {
1322

23+
private static final String URL = "http://www.baeldung.com?key1=value+1&key2=value%40%21%242&key3=value%253";
24+
private static final Logger LOGGER = LoggerFactory.getLogger(EncoderDecoder.class);
25+
26+
private String encodeValue(String value) {
27+
String encoded = null;
28+
try {
29+
encoded = URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
30+
} catch (UnsupportedEncodingException e) {
31+
LOGGER.error("Error encoding parameter {}", e.getMessage(), e);
32+
}
33+
return encoded;
34+
}
35+
36+
37+
private String decode(String value) {
38+
String decoded = null;
39+
try {
40+
decoded = URLDecoder.decode(value, StandardCharsets.UTF_8.toString());
41+
} catch (UnsupportedEncodingException e) {
42+
LOGGER.error("Error encoding parameter {}", e.getMessage(), e);
43+
}
44+
return decoded;
45+
}
46+
1447
@Test
15-
public void givenPlainURL_whenUsingUTF8EncodingScheme_thenEncodeURL() throws UnsupportedEncodingException {
16-
String plainURL = "http://www.baeldung.com" ;
17-
String encodedURL = URLEncoder.encode(plainURL, StandardCharsets.UTF_8.toString());
48+
public void givenURL_whenAnalyze_thenCorrect() throws Exception {
49+
URL url = new URL(URL);
1850

19-
Assert.assertThat("http%3A%2F%2Fwww.baeldung.com", CoreMatchers.is(encodedURL));
51+
Assert.assertThat(url.getProtocol(), CoreMatchers.is("http"));
52+
Assert.assertThat(url.getHost(), CoreMatchers.is("www.baeldung.com"));
53+
Assert.assertThat(url.getQuery(), CoreMatchers.is("key1=value+1&key2=value%40%21%242&key3=value%253"));
2054
}
2155

2256
@Test
23-
public void givenEncodedURL_whenUsingUTF8EncodingScheme_thenDecodeURL() throws UnsupportedEncodingException {
24-
String encodedURL = "http%3A%2F%2Fwww.baeldung.com" ;
25-
String decodedURL = URLDecoder.decode(encodedURL, StandardCharsets.UTF_8.toString());
57+
public void givenRequestParam_whenUTF8Scheme_thenEncode() throws Exception {
58+
Map<String, String> requestParams = new HashMap<>();
59+
requestParams.put("key1", "value 1");
60+
requestParams.put("key2", "value@!$2");
61+
requestParams.put("key3", "value%3");
2662

27-
Assert.assertThat("http://www.baeldung.com", CoreMatchers.is(decodedURL));
63+
String encodedQuery = requestParams.keySet().stream()
64+
.map(key -> key + "=" + encodeValue(requestParams.get(key)))
65+
.collect(Collectors.joining("&"));
66+
String encodedURL = "http://www.baeldung.com?" + encodedQuery;
67+
68+
Assert.assertThat(URL, CoreMatchers.is(encodedURL));
2869
}
2970

3071
@Test
31-
public void givenEncodedURL_whenUsingWrongEncodingScheme_thenDecodeInvalidURL() throws UnsupportedEncodingException {
32-
String encodedURL = "http%3A%2F%2Fwww.baeldung.com";
72+
public void givenRequestParam_whenUTF8Scheme_thenDecodeRequestParams() throws Exception {
73+
URL url = new URL(URL);
74+
String query = url.getQuery();
3375

34-
String decodedURL = URLDecoder.decode(encodedURL, StandardCharsets.UTF_16.toString());
76+
String decodedQuery = Arrays.stream(query.split("&"))
77+
.map(param -> param.split("=")[0] + "=" + decode(param.split("=")[1]))
78+
.collect(Collectors.joining("&"));
3579

36-
Assert.assertFalse("http://www.baeldung.com".equals(decodedURL));
80+
Assert.assertEquals(
81+
"http://www.baeldung.com?key1=value 1&key2=value@!$2&key3=value%3", url.getProtocol() + "://" + url.getHost() + "?" + decodedQuery);
3782
}
83+
3884
}

0 commit comments

Comments
 (0)