Skip to content

Commit d9b6582

Browse files
committed
SCANJLIB-226 Fix trailing slashes in REST API URL
1 parent 6490a6a commit d9b6582

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

lib/src/main/java/org/sonarsource/scanner/lib/ScannerEngineBootstrapper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
import static org.sonarsource.scanner.lib.ScannerProperties.SCANNER_ARCH;
4343
import static org.sonarsource.scanner.lib.ScannerProperties.SCANNER_OS;
44+
import static org.sonarsource.scanner.lib.internal.http.ServerConnection.removeTrailingSlash;
4445

4546
/**
4647
* Entry point to run a Sonar analysis programmatically.
@@ -151,8 +152,8 @@ private static String getServerVersion(ServerConnection serverConnection, boolea
151152

152153
private void initBootstrapDefaultValues() {
153154
setBootstrapPropertyIfNotAlreadySet(ScannerProperties.HOST_URL, getSonarCloudUrl());
154-
setBootstrapPropertyIfNotAlreadySet(ScannerProperties.API_BASE_URL,
155-
isSonarCloud(bootstrapProperties) ? SONARCLOUD_REST_API : (bootstrapProperties.get(ScannerProperties.HOST_URL) + "/api/v2"));
155+
setBootstrapPropertyIfNotAlreadySet(ScannerProperties.API_BASE_URL, isSonarCloud(bootstrapProperties) ?
156+
SONARCLOUD_REST_API : (removeTrailingSlash(bootstrapProperties.get(ScannerProperties.HOST_URL)) + "/api/v2"));
156157
if (!bootstrapProperties.containsKey(SCANNER_OS)) {
157158
setBootstrapProperty(SCANNER_OS, new OsResolver(system, new Paths2()).getOs().name().toLowerCase(Locale.ENGLISH));
158159
}

lib/src/main/java/org/sonarsource/scanner/lib/internal/http/ServerConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void init(Map<String, String> bootstrapProperties, Path sonarUserHome) {
6767
httpClient = OkHttpClientFactory.create(bootstrapProperties, sonarUserHome);
6868
}
6969

70-
private static String removeTrailingSlash(String url) {
70+
public static String removeTrailingSlash(String url) {
7171
return url.replaceAll("(/)+$", "");
7272
}
7373

lib/src/test/java/org/sonarsource/scanner/lib/ScannerEngineBootstrapperTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ void should_use_sonarcloud_url_from_property() throws Exception {
172172
}
173173
}
174174

175+
@Test
176+
void should_set_sonarqube_api_url_and_remove_trailing_slash() throws Exception {
177+
try (var scannerEngine = underTest
178+
.setBootstrapProperty(InternalProperties.SCANNER_DUMP_TO_FILE, dumpFile.toString())
179+
.setBootstrapProperty(ScannerProperties.HOST_URL, "http://localhost/")
180+
.bootstrap()) {
181+
182+
assertThat(scannerEngine.getBootstrapProperties()).contains(
183+
entry(ScannerProperties.API_BASE_URL, "http://localhost/api/v2"));
184+
assertThat(scannerEngine.isSonarCloud()).isFalse();
185+
}
186+
}
187+
175188
@Test
176189
void should_set_properties() throws Exception {
177190
try (var scannerEngine = underTest

0 commit comments

Comments
 (0)