|
3 | 3 | import org.hamcrest.CoreMatchers; |
4 | 4 | import org.junit.Assert; |
5 | 5 | import org.junit.Test; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
6 | 8 |
|
7 | 9 | import java.io.UnsupportedEncodingException; |
| 10 | +import java.net.MalformedURLException; |
| 11 | +import java.net.URL; |
8 | 12 | import java.net.URLDecoder; |
9 | 13 | import java.net.URLEncoder; |
10 | 14 | 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; |
11 | 20 |
|
12 | 21 | public class EncoderDecoder { |
13 | 22 |
|
| 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 | + |
14 | 47 | @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); |
18 | 50 |
|
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")); |
20 | 54 | } |
21 | 55 |
|
22 | 56 | @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"); |
26 | 62 |
|
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)); |
28 | 69 | } |
29 | 70 |
|
30 | 71 | @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(); |
33 | 75 |
|
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("&")); |
35 | 79 |
|
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); |
37 | 82 | } |
| 83 | + |
38 | 84 | } |
0 commit comments