You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2022/02/03 05:51:12 UTC

[GitHub] [druid] kfaraz commented on a change in pull request #12215: Fix bug while adding `Range` header in HttpEntity

kfaraz commented on a change in pull request #12215:
URL: https://github.com/apache/druid/pull/12215#discussion_r798230234



##########
File path: core/src/test/java/org/apache/druid/data/input/impl/HttpEntityTest.java
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.data.input.impl;
+
+import com.google.common.net.HttpHeaders;
+import org.apache.commons.io.IOUtils;
+import org.apache.druid.java.util.common.StringUtils;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.mockito.Mockito;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLConnection;
+
+public class HttpEntityTest
+{
+  long offset = 15;
+  URI uri = Mockito.mock(URI.class);
+  URL url = Mockito.mock(URL.class);
+  URLConnection urlConnection = Mockito.mock(URLConnection.class);
+  InputStream inputStreamMock = Mockito.mock(InputStream.class);
+  String contentRange = StringUtils.format("bytes %d-%d/%d", offset, 1000, 1000);
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Test
+  public void testOpenInputStream() throws IOException, URISyntaxException
+  {
+    URI url = new URI("https://druid.apache.org/data/wikipedia.json.gz");
+    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));
+  }
+
+  @Test
+  public void testWithServerSupportingRanges() throws IOException
+  {
+    Mockito.when(uri.toURL()).thenReturn(url);
+    Mockito.when(url.openConnection()).thenReturn(urlConnection);
+    Mockito.when(urlConnection.getHeaderField(HttpHeaders.CONTENT_RANGE)).thenReturn("posts 12-22/22");
+    Mockito.when(urlConnection.getInputStream()).thenReturn(inputStreamMock);
+    Mockito.when(inputStreamMock.skip(offset)).thenReturn(offset);
+    HttpEntity.openInputStream(uri, "", null, offset);
+    Mockito.verify(inputStreamMock, Mockito.times(1)).skip(offset);
+  }
+
+  @Test
+  public void testWithServerNotSupportingRanges() throws IOException
+  {
+    Mockito.when(uri.toURL()).thenReturn(url);
+    Mockito.when(url.openConnection()).thenReturn(urlConnection);
+    Mockito.when(urlConnection.getHeaderField(HttpHeaders.CONTENT_RANGE)).thenReturn(contentRange);

Review comment:
       If this method is testing for a server that does not support ranges, should this mock return `contentRange` here or null?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org