From 3fdb2fbf517d7f4bfec576a05280d50cb70ba072 Mon Sep 17 00:00:00 2001 From: cryptoe Date: Fri, 17 Jun 2022 18:46:45 +0530 Subject: [PATCH 1/2] Adding file based uri. --- .../java/org/apache/druid/data/input/impl/HttpEntityTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/test/java/org/apache/druid/data/input/impl/HttpEntityTest.java b/core/src/test/java/org/apache/druid/data/input/impl/HttpEntityTest.java index f9a369e3fde7..ffa55c641bce 100644 --- a/core/src/test/java/org/apache/druid/data/input/impl/HttpEntityTest.java +++ b/core/src/test/java/org/apache/druid/data/input/impl/HttpEntityTest.java @@ -64,7 +64,7 @@ public void setup() throws IOException @Test public void testOpenInputStream() throws IOException, URISyntaxException { - URI url = new URI("https://druid.apache.org/data/wikipedia.json.gz"); + URI url = this.getClass().getClassLoader().getResource("white-rabbit.txt").toURI(); final InputStream inputStream = HttpEntity.openInputStream(url, "", null, 0); final InputStream inputStreamPartial = HttpEntity.openInputStream(url, "", null, 5); inputStream.skip(5); From 7b49071a13a02212d1b03e17f7c9c090bed18a88 Mon Sep 17 00:00:00 2001 From: cryptoe Date: Thu, 7 Jul 2022 18:52:54 +0530 Subject: [PATCH 2/2] Adding the HTTP entity test back --- .../druid/data/input/impl/HttpEntityTest.java | 52 +++++++++++++++++-- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/core/src/test/java/org/apache/druid/data/input/impl/HttpEntityTest.java b/core/src/test/java/org/apache/druid/data/input/impl/HttpEntityTest.java index ffa55c641bce..59217456dbfa 100644 --- a/core/src/test/java/org/apache/druid/data/input/impl/HttpEntityTest.java +++ b/core/src/test/java/org/apache/druid/data/input/impl/HttpEntityTest.java @@ -20,6 +20,7 @@ package org.apache.druid.data.input.impl; import com.google.common.net.HttpHeaders; +import com.sun.net.httpserver.HttpServer; import org.apache.commons.io.IOUtils; import org.apache.druid.java.util.common.StringUtils; import org.junit.Assert; @@ -33,10 +34,14 @@ import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.net.ServerSocket; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; +import java.nio.charset.StandardCharsets; public class HttpEntityTest { @@ -64,11 +69,48 @@ public void setup() throws IOException @Test public void testOpenInputStream() throws IOException, URISyntaxException { - URI url = this.getClass().getClassLoader().getResource("white-rabbit.txt").toURI(); - final InputStream inputStream = HttpEntity.openInputStream(url, "", null, 0); - final InputStream inputStreamPartial = HttpEntity.openInputStream(url, "", null, 5); - inputStream.skip(5); - Assert.assertTrue(IOUtils.contentEquals(inputStream, inputStreamPartial)); + HttpServer server = null; + InputStream inputStream = null; + InputStream inputStreamPartial = null; + ServerSocket serverSocket = null; + try { + serverSocket = new ServerSocket(0); + int port = serverSocket.getLocalPort(); + // closing port so that the httpserver can use. Can cause race conditions. + serverSocket.close(); + server = HttpServer.create(new InetSocketAddress("localhost", port), 0); + server.createContext( + "/test", + (httpExchange) -> { + String payload = "12345678910"; + byte[] outputBytes = payload.getBytes(StandardCharsets.UTF_8); + httpExchange.sendResponseHeaders(200, outputBytes.length); + OutputStream os = httpExchange.getResponseBody(); + httpExchange.getResponseHeaders().set(HttpHeaders.CONTENT_TYPE, "application/octet-stream"); + httpExchange.getResponseHeaders().set(HttpHeaders.CONTENT_LENGTH, String.valueOf(outputBytes.length)); + httpExchange.getResponseHeaders().set(HttpHeaders.CONTENT_RANGE, "bytes 0"); + os.write(outputBytes); + os.close(); + } + ); + server.start(); + + URI url = new URI("http://" + server.getAddress().getHostName() + ":" + server.getAddress().getPort() + "/test"); + inputStream = HttpEntity.openInputStream(url, "", null, 0); + inputStreamPartial = HttpEntity.openInputStream(url, "", null, 5); + inputStream.skip(5); + Assert.assertTrue(IOUtils.contentEquals(inputStream, inputStreamPartial)); + } + finally { + IOUtils.closeQuietly(inputStream); + IOUtils.closeQuietly(inputStreamPartial); + if (server != null) { + server.stop(0); + } + if (serverSocket != null) { + serverSocket.close(); + } + } } @Test