Skip to content

Commit dbd2c2f

Browse files
Improve ServerConnection class code coverage
1 parent a6b0991 commit dbd2c2f

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

api/src/test/java/org/sonarsource/scanner/api/internal/ServerConnectionTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.charset.StandardCharsets;
2323
import java.nio.file.Files;
2424
import java.nio.file.Path;
25+
import java.nio.file.Paths;
2526
import java.util.HashMap;
2627
import java.util.Map;
2728
import okhttp3.mockwebserver.MockResponse;
@@ -32,7 +33,9 @@
3233
import org.junit.rules.TemporaryFolder;
3334
import org.sonarsource.scanner.api.internal.cache.Logger;
3435

36+
import static java.lang.String.format;
3537
import static org.assertj.core.api.Assertions.assertThat;
38+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3639
import static org.mockito.Mockito.mock;
3740

3841
public class ServerConnectionTest {
@@ -66,6 +69,16 @@ public void download_success() throws Exception {
6669
assertThat(response).isEqualTo(HELLO_WORLD);
6770
}
6871

72+
@Test
73+
public void downloadString_fails_on_url_validation() {
74+
ServerConnection connection = create(false, false);
75+
answer(HELLO_WORLD);
76+
77+
assertThatThrownBy(() -> connection.downloadString("should_fail"))
78+
.isInstanceOf(IllegalArgumentException.class)
79+
.hasMessage("URL path must start with slash: should_fail");
80+
}
81+
6982
@Test
7083
public void test_downloadFile() throws Exception {
7184
Path toFile = temp.newFile().toPath();
@@ -77,6 +90,27 @@ public void test_downloadFile() throws Exception {
7790
assertThat(new String(Files.readAllBytes(toFile), StandardCharsets.UTF_8)).isEqualTo(HELLO_WORLD);
7891
}
7992

93+
@Test
94+
public void downloadFile_fails_on_url_validation() {
95+
ServerConnection connection = create(false, false);
96+
answer(HELLO_WORLD);
97+
98+
assertThatThrownBy(() -> connection.downloadFile("should_fail", Paths.get("test-path")))
99+
.isInstanceOf(IllegalArgumentException.class)
100+
.hasMessage("URL path must start with slash: should_fail");
101+
}
102+
103+
@Test
104+
public void should_throw_ISE_if_response_not_successful() throws Exception {
105+
Path toFile = temp.newFile().toPath();
106+
answer(HELLO_WORLD, 400);
107+
108+
ServerConnection underTest = create(false, false);
109+
assertThatThrownBy(() -> underTest.downloadFile("/batch/index.txt", toFile))
110+
.isInstanceOf(IllegalStateException.class)
111+
.hasMessage(format("Status returned by url [http://localhost:%d/batch/index.txt] is not valid: [400]", server.getPort()));
112+
}
113+
80114
@Test
81115
public void should_support_server_url_without_trailing_slash() throws Exception {
82116
Map<String, String> props = new HashMap<>();
@@ -104,7 +138,11 @@ private ServerConnection create(boolean enableCache, boolean preferCache) {
104138
}
105139

106140
private void answer(String msg) {
107-
MockResponse response = new MockResponse().setBody(msg);
141+
answer(msg, 200);
142+
}
143+
144+
private void answer(String msg, int responseCode) {
145+
MockResponse response = new MockResponse().setBody(msg).setResponseCode(responseCode);
108146
server.enqueue(response);
109147
}
110148
}

0 commit comments

Comments
 (0)