@Override protected byte[] readBytes(URI uri) { return download(uri); }
@Override public String downloadPlainText(URI uri, String encoding) { return readString(uri, Charset.forName(encoding)); }
@Override protected String readString(URI uri, Charset charset) { try { return IOUtils.toString(downloader.newInputSupplier(uri, this.connectTimeout, this.readTimeout).getInput(), charset); } catch (IOException e) { throw failToDownload(uri, e); } }
@Test public void readGzipString() throws URISyntaxException { String text = new DefaultHttpDownloader(new MapSettings().asConfig()).readString(new URI(baseUrl + "/gzip/"), StandardCharsets.UTF_8); assertThat(text).isEqualTo("GZIP response"); }
@Test public void shouldNotCreateFileIfFailToDownload() throws Exception { File toDir = temporaryFolder.newFolder(); File toFile = new File(toDir, "downloadToFile.txt"); try { int port = new InetSocketAddress("localhost", 0).getPort(); new DefaultHttpDownloader(new MapSettings().asConfig()).download(new URI("http://localhost:" + port), toFile); } catch (SonarException e) { assertThat(toFile).doesNotExist(); } }
@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"); }
@Override protected String description(URI uri) { return String.format("%s (%s)", uri.toString(), getProxySynthesis(uri)); }
@Test public void uri_description() throws URISyntaxException { String description = new DefaultHttpDownloader(new MapSettings().asConfig()).description(new URI("http://sonarsource.org")); assertThat(description).matches("http://sonarsource.org \\(.*\\)"); }
@Test public void downloadBytes() throws URISyntaxException { byte[] bytes = new DefaultHttpDownloader(new MapSettings().asConfig()).readBytes(new URI(baseUrl)); assertThat(bytes.length).isGreaterThan(10); }
@Test public void supported_schemes() { assertThat(new DefaultHttpDownloader(new MapSettings().asConfig()).getSupportedSchemes()).contains("http"); }
@Test public void followRedirect() throws URISyntaxException { String content = new DefaultHttpDownloader(new MapSettings().asConfig()).readString(new URI(baseUrl + "/redirect/"), StandardCharsets.UTF_8); assertThat(content).contains("agent"); }
@Test public void downloadToFile() throws URISyntaxException, IOException { File toDir = temporaryFolder.newFolder(); File toFile = new File(toDir, "downloadToFile.txt"); new DefaultHttpDownloader(new MapSettings().asConfig()).download(new URI(baseUrl), toFile); assertThat(toFile).exists(); assertThat(toFile.length()).isGreaterThan(10l); }
@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 #"); }
private SonarException failToDownload(URI uri, IOException e) { throw new SonarException(String.format("Fail to download: %s (%s)", uri, getProxySynthesis(uri)), e); }
@Test public void readString() throws URISyntaxException { String text = new DefaultHttpDownloader(new MapSettings().asConfig()).readString(new URI(baseUrl), StandardCharsets.UTF_8); assertThat(text.length()).isGreaterThan(10); }
@Override public byte[] download(URI uri) { try { return ByteStreams.toByteArray(downloader.newInputSupplier(uri, this.connectTimeout, this.readTimeout).getInput()); } catch (IOException e) { throw failToDownload(uri, e); } }
@Override protected byte[] readBytes(URI uri) { return download(uri); }
@Override public String downloadPlainText(URI uri, String encoding) { return readString(uri, Charset.forName(encoding)); }
@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"); }
@Override protected String description(URI uri) { return String.format("%s (%s)", uri.toString(), getProxySynthesis(uri)); }