This commit is contained in:
Looly
2023-10-23 00:41:20 +08:00
parent da00aa3969
commit d8f02fe4cf
40 changed files with 186 additions and 162 deletions

View File

@@ -12,11 +12,8 @@
package org.dromara.hutool.core.net;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.net.url.URLDecoder;
import org.dromara.hutool.core.net.url.URLEncoder;
import org.dromara.hutool.core.net.url.UrlQuery;
import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.core.net.url.UrlDecoder;
import org.dromara.hutool.core.net.url.UrlEncoder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -25,26 +22,26 @@ public class URLEncoderTest {
@Test
void encodeTest() {
final String body = "366466 - 副本.jpg";
final String encode = URLEncoder.encodeAll(body);
final String encode = UrlEncoder.encodeAll(body);
Assertions.assertEquals("366466%20-%20%E5%89%AF%E6%9C%AC.jpg", encode);
Assertions.assertEquals(body, URLDecoder.decode(encode));
Assertions.assertEquals(body, UrlDecoder.decode(encode));
final String encode2 = URLEncoder.encodeQuery(body);
final String encode2 = UrlEncoder.encodeQuery(body);
Assertions.assertEquals("366466%20-%20%E5%89%AF%E6%9C%AC.jpg", encode2);
}
@Test
void encodeQueryPlusTest() {
final String body = "+";
final String encode2 = URLEncoder.encodeQuery(body);
final String encode2 = UrlEncoder.encodeQuery(body);
Assertions.assertEquals("+", encode2);
}
@Test
void encodeEmojiTest() {
final String emoji = "🐶😊😂🤣";
final String encode = URLEncoder.encodeAll(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);
Assertions.assertEquals(emoji, URLDecoder.decode(encode));
Assertions.assertEquals(emoji, UrlDecoder.decode(encode));
}
}

View File

@@ -12,7 +12,7 @@
package org.dromara.hutool.core.net;
import org.dromara.hutool.core.net.url.URLUtil;
import org.dromara.hutool.core.net.url.UrlUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -32,66 +32,66 @@ public class URLUtilTest {
public void normalizeTest() {
// issue#I25MZL多个/被允许
String url = "http://www.hutool.cn//aaa/bbb";
String normalize = URLUtil.normalize(url);
String normalize = UrlUtil.normalize(url);
Assertions.assertEquals("http://www.hutool.cn//aaa/bbb", normalize);
url = "www.hutool.cn//aaa/bbb";
normalize = URLUtil.normalize(url);
normalize = UrlUtil.normalize(url);
Assertions.assertEquals("http://www.hutool.cn//aaa/bbb", normalize);
}
@Test
public void normalizeTest2() {
String url = "http://www.hutool.cn//aaa/\\bbb?a=1&b=2";
String normalize = URLUtil.normalize(url);
String normalize = UrlUtil.normalize(url);
Assertions.assertEquals("http://www.hutool.cn//aaa//bbb?a=1&b=2", normalize);
url = "www.hutool.cn//aaa/bbb?a=1&b=2";
normalize = URLUtil.normalize(url);
normalize = UrlUtil.normalize(url);
Assertions.assertEquals("http://www.hutool.cn//aaa/bbb?a=1&b=2", normalize);
}
@Test
public void normalizeTest3() {
String url = "http://www.hutool.cn//aaa/\\bbb?a=1&b=2";
String normalize = URLUtil.normalize(url, true);
String normalize = UrlUtil.normalize(url, true);
Assertions.assertEquals("http://www.hutool.cn//aaa//bbb?a=1&b=2", normalize);
url = "www.hutool.cn//aaa/bbb?a=1&b=2";
normalize = URLUtil.normalize(url, true);
normalize = UrlUtil.normalize(url, true);
Assertions.assertEquals("http://www.hutool.cn//aaa/bbb?a=1&b=2", normalize);
url = "\\/www.hutool.cn//aaa/bbb?a=1&b=2";
normalize = URLUtil.normalize(url, true);
normalize = UrlUtil.normalize(url, true);
Assertions.assertEquals("http://www.hutool.cn//aaa/bbb?a=1&b=2", normalize);
}
@Test
public void normalizeIpv6Test() {
final String url = "http://[fe80::8f8:2022:a603:d180]:9439";
final String normalize = URLUtil.normalize("http://[fe80::8f8:2022:a603:d180]:9439", true);
final String normalize = UrlUtil.normalize("http://[fe80::8f8:2022:a603:d180]:9439", true);
Assertions.assertEquals(url, normalize);
}
@Test
public void formatTest() {
final String url = "//www.hutool.cn//aaa/\\bbb?a=1&b=2";
final String normalize = URLUtil.normalize(url);
final String normalize = UrlUtil.normalize(url);
Assertions.assertEquals("http://www.hutool.cn//aaa//bbb?a=1&b=2", normalize);
}
@Test
public void getHostTest() throws MalformedURLException {
final String url = "https://www.hutool.cn//aaa/\\bbb?a=1&b=2";
final String normalize = URLUtil.normalize(url);
final URI host = URLUtil.getHost(new URL(normalize));
final String normalize = UrlUtil.normalize(url);
final URI host = UrlUtil.getHost(new URL(normalize));
Assertions.assertEquals("https://www.hutool.cn", host.toString());
}
@Test
public void getPathTest(){
final String url = " http://www.aaa.bbb/search?scope=ccc&q=ddd";
final String path = URLUtil.getPath(url);
final String path = UrlUtil.getPath(url);
Assertions.assertEquals("/search", path);
}
}

View File

@@ -12,8 +12,8 @@
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.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;
@@ -25,12 +25,12 @@ class UrlDecoderTest {
@Test
void decodeForPathTest() {
Assertions.assertEquals("+", URLDecoder.decodeForPath("+", CharsetUtil.UTF_8));
Assertions.assertEquals("+", UrlDecoder.decodeForPath("+", CharsetUtil.UTF_8));
}
@Test
public void decodePlusTest() {
final String decode = URLDecoder.decode("+", CharsetUtil.UTF_8);
final String decode = UrlDecoder.decode("+", CharsetUtil.UTF_8);
Assertions.assertEquals(" ", decode);
}
@@ -41,36 +41,36 @@ class UrlDecoderTest {
final String s = "测试";
final String expectedDecode = "%FE%FF%6D%4B%8B%D5";
final String s1 = URLEncoder.encodeAll(s, StandardCharsets.UTF_16);
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);
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);
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)
UrlDecoder.decode(mixDecoded, StandardCharsets.UTF_16)
);
}
@Test
void decodeCharSetIsNullToStrTest() {
final String hello = "你好";
String decode = URLDecoder.decode(hello, null, true);
String decode = UrlDecoder.decode(hello, null, true);
Assertions.assertEquals(hello, decode);
}
@Test
void decodeStrIsEmptyToStrTest() {
final String strEmpty = "";
String decode = URLDecoder.decode(strEmpty, StandardCharsets.UTF_8, true);
String decode = UrlDecoder.decode(strEmpty, StandardCharsets.UTF_8, true);
Assertions.assertEquals(strEmpty, decode);
}
@@ -78,19 +78,19 @@ class UrlDecoderTest {
void decodeStrWithUTF8ToStrTest() {
final String exceptedDecode = "你好";
final String encode = "%E4%BD%A0%E5%A5%BD";
String s1 = URLDecoder.decode(encode);
String s1 = UrlDecoder.decode(encode);
Assertions.assertEquals(exceptedDecode, s1);
String s2 = URLDecoder.decode(encode, StandardCharsets.UTF_8);
String s2 = UrlDecoder.decode(encode, StandardCharsets.UTF_8);
Assertions.assertEquals(exceptedDecode, s2);
String s3 = URLDecoder.decode(encode, true);
String s3 = UrlDecoder.decode(encode, true);
Assertions.assertEquals(exceptedDecode, s3);
String s4 = URLDecoder.decode(encode + "+", false);
String s4 = UrlDecoder.decode(encode + "+", false);
Assertions.assertEquals(exceptedDecode + "+", s4);
String s5 = URLDecoder.decode(encode, StandardCharsets.UTF_8, false);
String s5 = UrlDecoder.decode(encode, StandardCharsets.UTF_8, false);
Assertions.assertEquals(exceptedDecode, s5);
}
@@ -98,10 +98,10 @@ class UrlDecoderTest {
void decodeStrWithUTF8ToByteTest(){
final String exceptedDecode = "你好";
final String encode = "%E4%BD%A0%E5%A5%BD";
byte[] decode = URLDecoder.decode(encode.getBytes(StandardCharsets.UTF_8));
byte[] decode = UrlDecoder.decode(encode.getBytes(StandardCharsets.UTF_8));
Assertions.assertEquals(exceptedDecode, new String(decode,StandardCharsets.UTF_8));
byte[] decode1 = URLDecoder.decode((encode + "+").getBytes(StandardCharsets.UTF_8));
byte[] decode1 = UrlDecoder.decode((encode + "+").getBytes(StandardCharsets.UTF_8));
Assertions.assertEquals(exceptedDecode+" ",new String(decode1,StandardCharsets.UTF_8));
}
}

View File

@@ -13,7 +13,7 @@
package org.dromara.hutool.core.net;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.net.url.URLUtil;
import org.dromara.hutool.core.net.url.UrlUtil;
import org.dromara.hutool.core.net.url.UrlBuilder;
import org.dromara.hutool.core.net.url.UrlQuery;
import org.dromara.hutool.core.util.CharsetUtil;
@@ -58,7 +58,7 @@ public class UrlQueryTest {
public void parseTest3() {
// issue#1688@Github
final String u = "https://www.baidu.com/proxy";
final UrlQuery query = UrlQuery.of(URLUtil.url(u).getQuery(), Charset.defaultCharset());
final UrlQuery query = UrlQuery.of(UrlUtil.url(u).getQuery(), Charset.defaultCharset());
Assertions.assertTrue(MapUtil.isEmpty(query.getQueryMap()));
}
@@ -75,13 +75,13 @@ public class UrlQueryTest {
Map<String, String> map = new LinkedHashMap<>();
map.put("username", "SSM");
map.put("password", "123456");
String query = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
String query = UrlUtil.buildQuery(map, StandardCharsets.UTF_8);
Assertions.assertEquals("username=SSM&password=123456", query);
map = new TreeMap<>();
map.put("username", "SSM");
map.put("password", "123456");
query = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
query = UrlUtil.buildQuery(map, StandardCharsets.UTF_8);
Assertions.assertEquals("password=123456&username=SSM", query);
}
@@ -90,19 +90,19 @@ public class UrlQueryTest {
Map<String, String> map = new LinkedHashMap<>();
map.put(null, "SSM");
map.put("password", "123456");
String query = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
String query = UrlUtil.buildQuery(map, StandardCharsets.UTF_8);
Assertions.assertEquals("password=123456", query);
map = new TreeMap<>();
map.put("username", "SSM");
map.put("password", "");
query = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
query = UrlUtil.buildQuery(map, StandardCharsets.UTF_8);
Assertions.assertEquals("password=&username=SSM", query);
map = new TreeMap<>();
map.put("username", "SSM");
map.put("password", null);
query = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
query = UrlUtil.buildQuery(map, StandardCharsets.UTF_8);
Assertions.assertEquals("password&username=SSM", query);
}
@@ -111,13 +111,13 @@ public class UrlQueryTest {
Map<String, String> map = new LinkedHashMap<>();
map.put("key1&", "SSM");
map.put("key2", "123456&");
String query = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
String query = UrlUtil.buildQuery(map, StandardCharsets.UTF_8);
Assertions.assertEquals("key1%26=SSM&key2=123456%26", query);
map = new TreeMap<>();
map.put("username=", "SSM");
map.put("password", "=");
query = URLUtil.buildQuery(map, StandardCharsets.UTF_8);
query = UrlUtil.buildQuery(map, StandardCharsets.UTF_8);
Assertions.assertEquals("password==&username%3D=SSM", query);
}

View File

@@ -27,7 +27,7 @@ public class IssueI6ZF6KTest {
final Map<String, Object> form = MapBuilder.<String, Object>of()
.put("condition", json)
.build();
final String requestBody = URLUtil.buildQuery(form, CharsetUtil.UTF_8);
final String requestBody = UrlUtil.buildQuery(form, CharsetUtil.UTF_8);
Console.log(requestBody);
}
}