class 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();

        // Check if non 2XX response code.
        if (responseCode / 100 != 2) {
            throw new IOException(
                "Server returned HTTP response code: " + responseCode + " for URL: " + url + " with content :'"
                    + con.getResponseMessage() + "'");
        }
        return responseCode;
    }

}

class InfluxDbHttpSenderTest {

    @Test
    public void shouldNotThrowException() throws Exception {

        HttpServer server = HttpServer.create(new InetSocketAddress(10081), 0);
        try {
            server.createContext("/write", new MyHandler());
            server.setExecutor(null); // creates a default executor
            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);
        }
    }

}