2222import java .nio .charset .StandardCharsets ;
2323import java .nio .file .Files ;
2424import java .nio .file .Path ;
25+ import java .nio .file .Paths ;
2526import java .util .HashMap ;
2627import java .util .Map ;
2728import okhttp3 .mockwebserver .MockResponse ;
3233import org .junit .rules .TemporaryFolder ;
3334import org .sonarsource .scanner .api .internal .cache .Logger ;
3435
36+ import static java .lang .String .format ;
3537import static org .assertj .core .api .Assertions .assertThat ;
38+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
3639import static org .mockito .Mockito .mock ;
3740
3841public 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