[NO ISSUE][TEST] Add Test Case For Chunked Http Requests
- user model changes: no
- storage format changes: no
- interface changes: no
Details:
- Add test case for chunked HTTP requests to ensure
the HTTP server can handle requests from clients
sending chunked requests.
Change-Id: I64ad01ca5ca322fb3efd4e86f053b70eb74e1079
Reviewed-on: https://asterix-gerrit.ics.uci.edu/2975
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Michael Blow <mblow@apache.org>
diff --git a/hyracks-fullstack/hyracks/hyracks-http/pom.xml b/hyracks-fullstack/hyracks/hyracks-http/pom.xml
index 6ca7a42..86d79a6 100644
--- a/hyracks-fullstack/hyracks/hyracks-http/pom.xml
+++ b/hyracks-fullstack/hyracks/hyracks-http/pom.xml
@@ -79,5 +79,9 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
diff --git a/hyracks-fullstack/hyracks/hyracks-http/src/test/java/org/apache/hyracks/http/servlet/EchoServlet.java b/hyracks-fullstack/hyracks/hyracks-http/src/test/java/org/apache/hyracks/http/servlet/EchoServlet.java
new file mode 100644
index 0000000..9298aed
--- /dev/null
+++ b/hyracks-fullstack/hyracks/hyracks-http/src/test/java/org/apache/hyracks/http/servlet/EchoServlet.java
@@ -0,0 +1,46 @@
+/*
+ * 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.hyracks.http.servlet;
+
+import java.util.concurrent.ConcurrentMap;
+
+import org.apache.hyracks.http.api.IServletRequest;
+import org.apache.hyracks.http.api.IServletResponse;
+import org.apache.hyracks.http.server.AbstractServlet;
+import org.apache.hyracks.http.server.utils.HttpUtil;
+
+import io.netty.handler.codec.http.HttpResponseStatus;
+
+/**
+ * A servlet that echos the received request body
+ */
+public class EchoServlet extends AbstractServlet {
+
+ public EchoServlet(ConcurrentMap<String, Object> ctx, String... paths) {
+ super(ctx, paths);
+ }
+
+ @Override
+ protected void post(IServletRequest request, IServletResponse response) throws Exception {
+ final String requestBody = HttpUtil.getRequestBody(request);
+ response.setStatus(HttpResponseStatus.OK);
+ HttpUtil.setContentType(response, HttpUtil.ContentType.TEXT_PLAIN, HttpUtil.Encoding.UTF8);
+ response.writer().write(requestBody);
+ }
+}
\ No newline at end of file
diff --git a/hyracks-fullstack/hyracks/hyracks-http/src/test/java/org/apache/hyracks/http/test/HttpServerTest.java b/hyracks-fullstack/hyracks/hyracks-http/src/test/java/org/apache/hyracks/http/test/HttpServerTest.java
index b5683ae..6b8b51a 100644
--- a/hyracks-fullstack/hyracks/hyracks-http/src/test/java/org/apache/hyracks/http/test/HttpServerTest.java
+++ b/hyracks-fullstack/hyracks/hyracks-http/src/test/java/org/apache/hyracks/http/test/HttpServerTest.java
@@ -24,6 +24,7 @@
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.Socket;
+import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
@@ -32,6 +33,13 @@
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.commons.lang3.RandomStringUtils;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
import org.apache.hyracks.http.HttpTestUtil;
import org.apache.hyracks.http.server.HttpServer;
import org.apache.hyracks.http.server.HttpServerConfig;
@@ -39,6 +47,7 @@
import org.apache.hyracks.http.server.InterruptOnCloseHandler;
import org.apache.hyracks.http.server.WebManager;
import org.apache.hyracks.http.servlet.ChattyServlet;
+import org.apache.hyracks.http.servlet.EchoServlet;
import org.apache.hyracks.http.servlet.SleepyServlet;
import org.apache.hyracks.util.StorageUtil;
import org.apache.logging.log4j.Level;
@@ -376,6 +385,36 @@
f.set(obj, value);
}
+ @Test
+ public void chunkedRequestTest() throws Exception {
+ final WebManager webMgr = new WebManager();
+ final int serverRequestChunkSize = StorageUtil.getIntSizeInBytes(1, StorageUtil.StorageUnit.KILOBYTE);
+ final HttpServerConfig config = HttpServerConfigBuilder.custom().setThreadCount(16).setRequestQueueSize(16)
+ .setMaxRequestChunkSize(serverRequestChunkSize).build();
+ final HttpServer server = new HttpServer(webMgr.getBosses(), webMgr.getWorkers(), PORT, config);
+ EchoServlet servlet = new EchoServlet(server.ctx(), PATH);
+ server.addServlet(servlet);
+ webMgr.add(server);
+ webMgr.start();
+ try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
+ final URI uri = new URI(HttpServerTest.PROTOCOL, null, HttpServerTest.HOST, HttpServerTest.PORT,
+ HttpServerTest.PATH, null, null);
+ final HttpPost postRequest = new HttpPost(uri);
+ final int requestSize = StorageUtil.getIntSizeInBytes(8, StorageUtil.StorageUnit.KILOBYTE);
+ final String requestBody = RandomStringUtils.randomAlphanumeric(requestSize);
+ final StringEntity chunkedEntity = new StringEntity(requestBody);
+ chunkedEntity.setChunked(true);
+ postRequest.setEntity(chunkedEntity);
+ try (CloseableHttpResponse response = httpClient.execute(postRequest)) {
+ final String responseBody = EntityUtils.toString(response.getEntity());
+ Assert.assertEquals(response.getStatusLine().getStatusCode(), HttpResponseStatus.OK.code());
+ Assert.assertEquals(responseBody, requestBody);
+ }
+ } finally {
+ webMgr.stop();
+ }
+ }
+
private void request(int count) throws URISyntaxException {
request(count, 0);
}