From: Julien HENRY Date: Fri, 12 Jul 2013 14:59:27 +0000 (+0200) Subject: SONARPLUGINS-3061 Support trailing slash in sonar.host.url X-Git-Tag: 2.5-rc1~114 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d998842fbd070404ac2dde82e391103256fcc550;p=sonar-scanner-cli.git SONARPLUGINS-3061 Support trailing slash in sonar.host.url --- diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java index 968e46c..116ba4f 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java @@ -41,10 +41,17 @@ class ServerConnection { private final String userAgent; private ServerConnection(String serverUrl, String app, String appVersion) { - this.serverUrl = serverUrl; + this.serverUrl = removeEndSlash(serverUrl); this.userAgent = app + "/" + appVersion; } + private String removeEndSlash(String url) { + if (url == null) { + return null; + } + return url.endsWith("/") ? url.substring(0, url.length() - 1) : url; + } + static ServerConnection create(Properties properties) { String serverUrl = properties.getProperty("sonar.host.url"); String app = properties.getProperty(InternalProperties.RUNNER_APP); diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/MockHttpServer.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/MockHttpServer.java index 38c1df5..77982f3 100644 --- a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/MockHttpServer.java +++ b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/MockHttpServer.java @@ -22,6 +22,7 @@ package org.sonar.runner.impl; import org.apache.commons.io.IOUtils; import org.mortbay.jetty.Handler; import org.mortbay.jetty.HttpConnection; +import org.mortbay.jetty.HttpException; import org.mortbay.jetty.Request; import org.mortbay.jetty.Server; import org.mortbay.jetty.handler.AbstractHandler; @@ -58,6 +59,10 @@ class MockHttpServer { Handler handler = new AbstractHandler() { public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException { + // SONARPLUGINS-3061 + if (target.startsWith("//")) { + throw new HttpException(404, "Not found"); + } Request baseRequest = request instanceof Request ? (Request) request : HttpConnection.getCurrentConnection().getRequest(); setResponseBody(getMockResponseData()); setRequestBody(IOUtils.toString(baseRequest.getInputStream())); diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java index fe53622..bb671bf 100644 --- a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java +++ b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java @@ -63,6 +63,20 @@ public class ServerConnectionTest { assertThat(FileUtils.readFileToString(toFile)).isEqualTo("abcde"); } + // SONARPLUGINS-3061 + @Test + public void should_support_trailing_slash() throws Exception { + httpServer.setMockResponseData("abcde"); + Properties props = new Properties(); + props.setProperty("sonar.host.url", httpServer.url() + "/"); + + ServerConnection connection = ServerConnection.create(props); + File toFile = temp.newFile(); + connection.download("/batch/index.txt", toFile); + + assertThat(FileUtils.readFileToString(toFile)).isEqualTo("abcde"); + } + @Test public void should_not_download_file_when_host_is_down() throws Exception { Properties props = new Properties();