BAEL-255: Added encode-decode of url

This commit is contained in:
Sunil Gulabani 2016-10-12 10:33:48 +05:30
parent 5b09ae5275
commit 3a27d6b50e
1 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package com.baeldung.encoderdecoder;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class EncoderDecoder {
@Test
public void givenPlainURL_whenUsingUTF8EncodingScheme_thenEncodeURL() throws UnsupportedEncodingException {
String plainURL = "http://www.baeldung.com" ;
String encodedURL = URLEncoder.encode(plainURL, StandardCharsets.UTF_8.toString());
Assert.assertThat("http%3A%2F%2Fwww.baeldung.com", CoreMatchers.is(encodedURL));
}
@Test
public void givenEncodedURL_whenUsingUTF8EncodingScheme_thenDecodeURL() throws UnsupportedEncodingException {
String encodedURL = "http%3A%2F%2Fwww.baeldung.com" ;
String decodedURL = URLDecoder.decode(encodedURL, StandardCharsets.UTF_8.toString());
Assert.assertThat("http://www.baeldung.com", CoreMatchers.is(decodedURL));
}
@Test
public void givenEncodedURL_whenUsingWrongEncodingScheme_thenDecodeInvalidURL() throws UnsupportedEncodingException {
String encodedURL = "http%3A%2F%2Fwww.baeldung.com";
String decodedURL = URLDecoder.decode(encodedURL, StandardCharsets.UTF_16.toString());
Assert.assertFalse("http://www.baeldung.com".equals(decodedURL));
}
}