From 56f228efef77ed67460fcc59b6cad4e2be0b0665 Mon Sep 17 00:00:00 2001 From: Dan Schulte Date: Tue, 5 Dec 2017 11:03:17 -0800 Subject: [PATCH] =?UTF-8?q?Add=20http=20scheme=20if=20no=20scheme=20is=20p?= =?UTF-8?q?rovided=20when=20creating=20a=20HttpRequest=20=E2=80=A6=20(#313?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add http scheme if no scheme is provided when creating a HttpRequest in RestProxy * Remove adding a default protocol when one isn't specified * Remove unused exception import --- .../com/microsoft/rest/v2/SwaggerMethodParser.java | 2 +- .../java/com/microsoft/rest/v2/http/UrlBuilder.java | 12 ++++++++++-- .../com/microsoft/rest/v2/http/UrlBuilderTests.java | 9 +++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) 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));