fix plus encode bug

This commit is contained in:
Looly
2021-04-08 18:12:10 +08:00
parent 6b0b729c68
commit 1edbc0de80
11 changed files with 151 additions and 18 deletions

View File

@@ -0,0 +1,17 @@
package cn.hutool.core.net;
import cn.hutool.core.util.CharsetUtil;
import org.junit.Assert;
import org.junit.Test;
public class URLEncoderTest {
@Test
public void encodeTest(){
String encode = URLEncoder.DEFAULT.encode("+", CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals("+", encode);
encode = URLEncoder.DEFAULT.encode(" ", CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals("%20", encode);
}
}

View File

@@ -18,6 +18,13 @@ public class UrlBuilderTest {
Assert.assertEquals("http://www.hutool.cn/", buildUrl);
}
@Test
public void buildTest2() {
// path中的+不做处理
String buildUrl = UrlBuilder.ofHttp("http://www.hutool.cn/+8618888888888", CharsetUtil.CHARSET_UTF_8).build();
Assert.assertEquals("http://www.hutool.cn/+8618888888888", buildUrl);
}
@Test
public void testHost() {
String buildUrl = UrlBuilder.create()

View File

@@ -0,0 +1,12 @@
package cn.hutool.core.net;
import cn.hutool.core.util.CharsetUtil;
import org.junit.Assert;
import org.junit.Test;
public class UrlDecoderTest {
@Test
public void decodeForPathTest(){
Assert.assertEquals("+", URLDecoder.decodeForPath("+", CharsetUtil.CHARSET_UTF_8));
}
}

View File

@@ -9,7 +9,7 @@ import java.net.URL;
/**
* URLUtil单元测试
*
*
* @author looly
*
*/
@@ -26,24 +26,24 @@ public class URLUtilTest {
normalize = URLUtil.normalize(url);
Assert.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);
Assert.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);
Assert.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);
Assert.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);
Assert.assertEquals("http://www.hutool.cn//aaa/bbb?a=1&b=2", normalize);
@@ -59,7 +59,7 @@ public class URLUtilTest {
String normalize = URLUtil.normalize("http://[fe80::8f8:2022:a603:d180]:9439", true);
Assert.assertEquals(url, normalize);
}
@Test
public void formatTest() {
String url = "//www.hutool.cn//aaa/\\bbb?a=1&b=2";
@@ -81,7 +81,7 @@ public class URLUtilTest {
String encode = URLUtil.encode(body);
Assert.assertEquals("366466%20-%20%E5%89%AF%E6%9C%AC.jpg", encode);
Assert.assertEquals(body, URLUtil.decode(encode));
String encode2 = URLUtil.encodeQuery(body);
Assert.assertEquals("366466+-+%E5%89%AF%E6%9C%AC.jpg", encode2);
}