@Test public void userAgent_includes_only_version_when_there_is_no_SERVER_ID_and_server_is_provided() throws URISyntaxException, IOException { Server server = mock(Server.class); when(server.getVersion()).thenReturn("2.2"); InputStream stream = new DefaultHttpDownloader(server, new MapSettings().asConfig()).openStream(new URI(baseUrl)); Properties props = new Properties(); props.load(stream); stream.close(); assertThat(props.getProperty("agent")).isEqualTo("SonarQube 2.2 #"); }
@Test public void userAgent_is_static_value_when_server_is_not_provided() throws URISyntaxException, IOException { InputStream stream = new DefaultHttpDownloader(new MapSettings().asConfig()).openStream(new URI(baseUrl)); Properties props = new Properties(); props.load(stream); stream.close(); assertThat(props.getProperty("agent")).isEqualTo("SonarQube"); }
@Test public void userAgent_includes_version_and_SERVER_ID_when_server_is_provided() throws URISyntaxException, IOException { Server server = mock(Server.class); when(server.getVersion()).thenReturn("2.2"); MapSettings settings = new MapSettings(); settings.setProperty(CoreProperties.SERVER_ID, "blablabla"); InputStream stream = new DefaultHttpDownloader(server, settings.asConfig()).openStream(new URI(baseUrl)); Properties props = new Properties(); props.load(stream); stream.close(); assertThat(props.getProperty("agent")).isEqualTo("SonarQube 2.2 # blablabla"); }
@Test(timeout = 10000) public void openStream_network_errors() throws IOException, URISyntaxException { // non routable address String url = "http://10.255.255.1"; thrown.expect(SonarException.class); thrown.expect(hasCause(new BaseMatcher<Exception>() { @Override public boolean matches(Object ex) { return // Java 8 ex instanceof NoRouteToHostException || ex instanceof SocketException // Java 7 or before || ex instanceof SocketTimeoutException; } @Override public void describeTo(Description arg0) { } })); DefaultHttpDownloader downloader = new DefaultHttpDownloader(new MapSettings().asConfig(), 10, 50000); downloader.openStream(new URI(url)); }