BAEL-5685 translate space using urlencoder (#15764)

* BAEL-5685 translate space using urlencoder

* Add a special character to the string

---------

Co-authored-by: Wynn Teo <wynnteo@Wynns-MacBook-Pro.local>
This commit is contained in:
Wynn Teo 2024-02-05 07:08:33 +08:00 committed by GitHub
parent ab7bcbd6d3
commit aee52febce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,43 @@
package com.baeldung.urlencoder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
public class SpaceURLEncoderUnitTest {
@Test
void givenSpaceInString_whenUsingDefaultEncoding_thenReturnPlusSign() {
String originalString = "Welcome to the Baeldung Website!";
String encodedString = URLEncoder.encode(originalString);
assertEquals("Welcome+to+the+Baeldung+Website%21", encodedString);
}
@Test
void givenSpaceInString_whenUsingUTF8Encoding_thenReturnPlusSign() throws UnsupportedEncodingException {
String originalString = "Welcome to the Baeldung Website!";
String encodedString = URLEncoder.encode(originalString, StandardCharsets.UTF_8.toString());
assertEquals("Welcome+to+the+Baeldung+Website%21", encodedString);
}
@Test
void givenSpaceInString_whenUsingDefaultEncodingAndReplace_thenReturnPct20() throws UnsupportedEncodingException {
String originalString = "Welcome to the Baeldung Website!";
String encodedString = URLEncoder.encode(originalString)
.replace("+", "%20");
assertEquals("Welcome%20to%20the%20Baeldung%20Website%21", encodedString);
}
@Test
void givenSpaceInString_whenUsingDefaultEncodingAndReplaceAll_thenReturnPct20() throws UnsupportedEncodingException {
String originalString = "Welcome to the Baeldung Website!";
String encodedString = URLEncoder.encode(originalString)
.replaceAll("\\+", "%20");
assertEquals("Welcome%20to%20the%20Baeldung%20Website%21", encodedString);
}
}