diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/SwaggerMethodParser.java b/client-runtime/src/main/java/com/microsoft/rest/v2/SwaggerMethodParser.java index 71d9110c01b63..401906d47c8db 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/SwaggerMethodParser.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/SwaggerMethodParser.java @@ -220,7 +220,7 @@ public int[] expectedStatusCodes() { public String scheme(Object[] swaggerMethodArguments) { final String substitutedHost = applySubstitutions(rawHost, hostSubstitutions, swaggerMethodArguments, UrlEscapers.urlPathSegmentEscaper()); final String[] substitutedHostParts = substitutedHost.split("://"); - return substitutedHostParts.length < 1 ? "https" : substitutedHostParts[0]; + return substitutedHostParts.length < 1 ? null : substitutedHostParts[0]; } /** diff --git a/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlBuilder.java b/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlBuilder.java index 0eb9cad7f0fef..b66d1833586bf 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlBuilder.java +++ b/client-runtime/src/main/java/com/microsoft/rest/v2/http/UrlBuilder.java @@ -42,8 +42,16 @@ public String scheme() { * @return This UrlBuilder so that multiple setters can be chained together. */ public UrlBuilder withHost(String host) { - if (host != null && host.endsWith("/")) { - host = host.substring(0, host.length() - 1); + if (host != null) { + if (host.endsWith("/")) { + host = host.substring(0, host.length() - 1); + } + + if (host.contains("://")) { + final String[] hostParts = host.split("://"); + withScheme(hostParts[0]); + host = hostParts[1]; + } } this.host = host; return this; diff --git a/client-runtime/src/test/java/com/microsoft/rest/v2/http/UrlBuilderTests.java b/client-runtime/src/test/java/com/microsoft/rest/v2/http/UrlBuilderTests.java index cfcd9d3be2ccd..89404d1191d1c 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/v2/http/UrlBuilderTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/v2/http/UrlBuilderTests.java @@ -193,6 +193,15 @@ public void withAbsolutePathAndQuery() { assertEquals("http://www.othersite.com/mypath?thing=stuff&otherthing=otherstuff", builder.toString()); } + @Test + public void withHostWhenHostContainsProtocol() { + final UrlBuilder builder = new UrlBuilder() + .withHost("https://www.bing.com"); + assertEquals("https", builder.scheme()); + assertEquals("www.bing.com", builder.host()); + assertEquals("https://www.bing.com", builder.toString()); + } + @Test public void parseWithNull() { assertNull(UrlBuilder.parse(null));