This commit is contained in:
Looly
2023-04-17 23:42:26 +08:00
parent f99e54d7ae
commit 542120f220
4 changed files with 94 additions and 68 deletions

View File

@@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test;
public class URLEncoderTest {
@Test
public void encodeTest() {
void encodeTest() {
final String body = "366466 - 副本.jpg";
final String encode = URLEncoder.encodeAll(body);
Assertions.assertEquals("366466%20-%20%E5%89%AF%E6%9C%AC.jpg", encode);
@@ -18,14 +18,14 @@ public class URLEncoderTest {
}
@Test
public void encodeQueryPlusTest() {
void encodeQueryPlusTest() {
final String body = "+";
final String encode2 = URLEncoder.encodeQuery(body);
Assertions.assertEquals("+", encode2);
}
@Test
public void encodeEmojiTest(){
void encodeEmojiTest(){
final String emoji = "🐶😊😂🤣";
final String encode = URLEncoder.encodeAll(emoji);
Assertions.assertEquals("%F0%9F%90%B6%F0%9F%98%8A%F0%9F%98%82%F0%9F%A4%A3", encode);

View File

@@ -1,13 +1,44 @@
package org.dromara.hutool.core.net;
import org.dromara.hutool.core.net.url.URLDecoder;
import org.dromara.hutool.core.net.url.URLEncoder;
import org.dromara.hutool.core.util.CharsetUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
public class UrlDecoderTest {
@Test
public void decodeForPathTest(){
void decodeForPathTest(){
Assertions.assertEquals("+", URLDecoder.decodeForPath("+", CharsetUtil.UTF_8));
}
@Test
void issue3063Test() throws UnsupportedEncodingException {
// https://github.com/dromara/hutool/issues/3063
final String s = "测试";
final String expectedDecode = "%FE%FF%6D%4B%8B%D5";
final String s1 = URLEncoder.encodeAll(s, StandardCharsets.UTF_16);
Assertions.assertEquals(expectedDecode, s1);
final String s2 = java.net.URLEncoder.encode(s, "UTF-16");
Assertions.assertEquals(expectedDecode, s2);
final String decode = URLDecoder.decode(s1, StandardCharsets.UTF_16);
Assertions.assertEquals(s, decode);
// 测试编码字符串和非编码字符串混合
final String mixDecoded = expectedDecode + "你好";
final String decode2 = URLDecoder.decode(mixDecoded, StandardCharsets.UTF_16);
Assertions.assertEquals("测试你好", decode2);
Assertions.assertEquals(
java.net.URLDecoder.decode(mixDecoded, "UTF-16"),
URLDecoder.decode(mixDecoded, StandardCharsets.UTF_16)
);
}
}