This commit is contained in:
Looly 2022-12-27 17:55:37 +08:00
parent 228d0a438a
commit 8c0fb77421
2 changed files with 20 additions and 3 deletions

View File

@ -107,10 +107,12 @@ public final class UrlBuilder implements Builder<String> {
public static UrlBuilder ofHttp(String httpUrl, final Charset charset) { public static UrlBuilder ofHttp(String httpUrl, final Charset charset) {
Assert.notBlank(httpUrl, "Http url must be not blank!"); Assert.notBlank(httpUrl, "Http url must be not blank!");
final int sepIndex = httpUrl.indexOf("://"); httpUrl = StrUtil.trimStart(httpUrl);
if (sepIndex < 0) { // issue#I66CIR
httpUrl = "http://" + httpUrl.trim(); if(false == StrUtil.startWithAnyIgnoreCase(httpUrl, "http://", "https://")){
httpUrl = "http://" + httpUrl;
} }
return of(httpUrl, charset); return of(httpUrl, charset);
} }

View File

@ -479,4 +479,19 @@ public class UrlBuilderTest {
UrlBuilder.of().addPath("//"); UrlBuilder.of().addPath("//");
UrlBuilder.of().addPath("//a"); UrlBuilder.of().addPath("//a");
} }
@Test
public void ofHttpTest() {
UrlBuilder ofHttp = UrlBuilder.ofHttp("http://hutool.cn");
Assert.assertEquals("http://hutool.cn", ofHttp.toString());
ofHttp = UrlBuilder.ofHttp("https://hutool.cn");
Assert.assertEquals("https://hutool.cn", ofHttp.toString());
ofHttp = UrlBuilder.ofHttp("hutool.cn");
Assert.assertEquals("http://hutool.cn", ofHttp.toString());
ofHttp = UrlBuilder.ofHttp("hutool.cn?old=http://aaa");
Assert.assertEquals("http://hutool.cn?old=http://aaa", ofHttp.toString());
}
} }