mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add methods
This commit is contained in:
parent
1051b7cb5f
commit
f5d68a0360
@ -1,7 +1,7 @@
|
|||||||
package cn.hutool.core.net.url;
|
package cn.hutool.core.net.url;
|
||||||
|
|
||||||
import cn.hutool.core.lang.builder.Builder;
|
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import cn.hutool.core.lang.builder.Builder;
|
||||||
import cn.hutool.core.text.StrUtil;
|
import cn.hutool.core.text.StrUtil;
|
||||||
import cn.hutool.core.util.CharsetUtil;
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
|
|
||||||
@ -280,6 +280,21 @@ public final class UrlBuilder implements Builder<String> {
|
|||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取端口,如果未自定义返回协议默认端口
|
||||||
|
*
|
||||||
|
* @return 端口
|
||||||
|
* @since 5.8.9
|
||||||
|
*/
|
||||||
|
public int getPortWithDefault() {
|
||||||
|
int port = getPort();
|
||||||
|
if (port <= 0) {
|
||||||
|
port = toURL().getDefaultPort();
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置端口,默认-1
|
* 设置端口,默认-1
|
||||||
*
|
*
|
||||||
|
@ -16,8 +16,11 @@ public class UrlBuilderTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void buildTest() {
|
public void buildTest() {
|
||||||
final String buildUrl = UrlBuilder.of().setHost("www.hutool.cn").build();
|
final UrlBuilder builder = UrlBuilder.of();
|
||||||
|
final String buildUrl = builder.setHost("www.hutool.cn").build();
|
||||||
|
|
||||||
Assert.assertEquals("http://www.hutool.cn/", buildUrl);
|
Assert.assertEquals("http://www.hutool.cn/", buildUrl);
|
||||||
|
Assert.assertEquals(buildUrl, 80, builder.getPortWithDefault());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -26,9 +29,11 @@ public class UrlBuilderTest {
|
|||||||
String buildUrl = UrlBuilder.of().setScheme("http").setHost("192.168.1.1").setPort(8080).setWithEndTag(false).build();
|
String buildUrl = UrlBuilder.of().setScheme("http").setHost("192.168.1.1").setPort(8080).setWithEndTag(false).build();
|
||||||
Assert.assertEquals("http://192.168.1.1:8080", buildUrl);
|
Assert.assertEquals("http://192.168.1.1:8080", buildUrl);
|
||||||
|
|
||||||
buildUrl = UrlBuilder.of().setScheme("http").setHost("192.168.1.1").setPort(8080).addQuery("url", "http://192.168.1.1/test/1")
|
final UrlBuilder urlBuilder = UrlBuilder.of();
|
||||||
|
buildUrl = urlBuilder.setScheme("http").setHost("192.168.1.1").setPort(8080).addQuery("url", "http://192.168.1.1/test/1")
|
||||||
.setWithEndTag(false).build();
|
.setWithEndTag(false).build();
|
||||||
Assert.assertEquals("http://192.168.1.1:8080?url=http://192.168.1.1/test/1", buildUrl);
|
Assert.assertEquals("http://192.168.1.1:8080?url=http://192.168.1.1/test/1", buildUrl);
|
||||||
|
Assert.assertEquals(buildUrl, 8080, urlBuilder.getPortWithDefault());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -459,7 +464,7 @@ public class UrlBuilderTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getAuthorityTest(){
|
public void getAuthorityTest() {
|
||||||
final UrlBuilder builder = UrlBuilder.ofHttp("127.0.0.1:8080")
|
final UrlBuilder builder = UrlBuilder.ofHttp("127.0.0.1:8080")
|
||||||
.addQuery("param[0].field", "编码");
|
.addQuery("param[0].field", "编码");
|
||||||
|
|
||||||
@ -467,7 +472,7 @@ public class UrlBuilderTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void addPathTest(){
|
public void addPathTest() {
|
||||||
//https://gitee.com/dromara/hutool/issues/I5O4ML
|
//https://gitee.com/dromara/hutool/issues/I5O4ML
|
||||||
UrlBuilder.of().addPath("");
|
UrlBuilder.of().addPath("");
|
||||||
UrlBuilder.of().addPath("/");
|
UrlBuilder.of().addPath("/");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user