Skip to content

Commit 82065d8

Browse files
Merge pull request eugenp#744 from sunilgulabani/master
BAEL-276: Added encode-decode of url
2 parents 5172920 + 3a27d6b commit 82065d8

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.encoderdecoder;
2+
3+
import org.hamcrest.CoreMatchers;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import java.io.UnsupportedEncodingException;
8+
import java.net.URLDecoder;
9+
import java.net.URLEncoder;
10+
import java.nio.charset.StandardCharsets;
11+
12+
public class EncoderDecoder {
13+
14+
@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());
18+
19+
Assert.assertThat("http%3A%2F%2Fwww.baeldung.com", CoreMatchers.is(encodedURL));
20+
}
21+
22+
@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());
26+
27+
Assert.assertThat("http://www.baeldung.com", CoreMatchers.is(decodedURL));
28+
}
29+
30+
@Test
31+
public void givenEncodedURL_whenUsingWrongEncodingScheme_thenDecodeInvalidURL() throws UnsupportedEncodingException {
32+
String encodedURL = "http%3A%2F%2Fwww.baeldung.com";
33+
34+
String decodedURL = URLDecoder.decode(encodedURL, StandardCharsets.UTF_16.toString());
35+
36+
Assert.assertFalse("http://www.baeldung.com".equals(decodedURL));
37+
}
38+
}

0 commit comments

Comments
 (0)