InfluxDbHttpSender extends InfluxDbBaseSender { @Override protected int writeData(byte[] line) throws Exception { final HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); if (authStringEncoded != null && !authStringEncoded.isEmpty()) { con.setRequestProperty("Authorization", "Basic " + authStringEncoded); } con.setDoOutput(true); con.setConnectTimeout(connectTimeout); con.setReadTimeout(readTimeout); OutputStream out = con.getOutputStream(); try { out.write(line); out.flush(); } finally { out.close(); } int responseCode = con.getResponseCode(); if (responseCode / 100 != 2) { throw new IOException( "Server returned HTTP response code: " + responseCode + " for URL: " + url + " with content :'" + con.getResponseMessage() + "'"); } return responseCode; } InfluxDbHttpSender(
        final String protocol, final String hostname, final int port, final String database, final String authString,
        final TimeUnit timePrecision, final int connectTimeout, final int readTimeout, final String measurementPrefix); @Deprecated  InfluxDbHttpSender(
        final String protocol, final String hostname, final int port, final String database, final String authString,
        final TimeUnit timePrecision);   }
@Test(expected = UnknownHostException.class) public void shouldThrowUnknownHostException() throws Exception { InfluxDbHttpSender influxDbHttpSender = new InfluxDbHttpSender( "http", "testtestasdfg", 80, "testdb", "asdf", TimeUnit.MINUTES, 1000, 1000, "" ); influxDbHttpSender.writeData(new byte[0]); }
@Test(expected = ConnectException.class) public void shouldThrowConnectException() throws Exception { InfluxDbHttpSender influxDbHttpSender = new InfluxDbHttpSender( "http", "localhost", 10080, "testdb", "asdf", TimeUnit.MINUTES, 1000, 1000, "" ); influxDbHttpSender.writeData(new byte[0]); }
@Test(expected = IOException.class) public void shouldThrowCosnnectException() throws Exception { HttpServer server = HttpServer.create(new InetSocketAddress(10082), 0); try { server.createContext("/testdb", new MyHandler()); server.setExecutor(null); server.start(); InfluxDbHttpSender influxDbHttpSender = new InfluxDbHttpSender( "http", "localhost", 10082, "testdb", "asdf", TimeUnit.MINUTES, 1000, 1000, "" ); influxDbHttpSender.writeData(new byte[0]); } catch (IOException e) { throw new IOException(e); } finally { server.stop(0); } }
@Test public void shouldNotThrowException() throws Exception { HttpServer server = HttpServer.create(new InetSocketAddress(10081), 0); try { server.createContext("/write", new MyHandler()); server.setExecutor(null); server.start(); InfluxDbHttpSender influxDbHttpSender = new InfluxDbHttpSender( "http", "localhost", 10081, "testdb", "asdf", TimeUnit.MINUTES, 1000, 1000, "" ); assertThat(influxDbHttpSender.writeData(new byte[0]) == 0); } catch (IOException e) { throw new IOException(e); } finally { server.stop(0); } }