# HG changeset patch # User Jaikiran Pai # Date 1530018198 -19800 # Tue Jun 26 18:33:18 2018 +0530 # Node ID 955a66f0f04a0c73be1287203df644dbb0bad7ac # Parent 6274aee1f6922bdb6f819bee02fe2ccfffbc4501 Add a testcase to reproduce the failure of PUT requests if it exceeded a certain number of invocations diff --git a/test/jdk/java/net/httpclient/PUTRequestSizeTest.java b/test/jdk/java/net/httpclient/PUTRequestSizeTest.java new file mode 100644 --- /dev/null +++ b/test/jdk/java/net/httpclient/PUTRequestSizeTest.java @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpHeaders; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.io.IOException; +import com.sun.net.httpserver.HttpServer; +import com.sun.net.httpserver.HttpContext; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import java.net.InetSocketAddress; +import java.net.InetAddress; +import java.net.HttpURLConnection; + + +/** + * @test + * @run main PUTRequestSizeTest + * @summary Tests that a PUT request works successfully when invoked + using the same HttpClient instance, multiple times + */ +public class PUTRequestSizeTest { + + public static void main(final String[] args) throws Exception { + final String webAppContext = "/puttest"; + // create a local http server which has a web app context that just returns back + // a response 200 + final HttpServer server = createAndStartServer(webAppContext); + try { + final HttpClient httpClient = HttpClient.newBuilder().build(); + final URL requestURL = new URL("http://" + server.getAddress().getHostString() + + ":" + server.getAddress().getPort() + webAppContext + "/"); + // invoke a few times + for (int i = 0; i < 10; i++) { + System.out.println("Sending PUT request " + (i +1)); + issuePUT(httpClient, requestURL); + } + } finally { + server.stop(0); + } + } + + private static void issuePUT(final HttpClient httpClient, final URL requestURL) throws Exception { + final byte[] data = new byte[1]; + final HttpRequest.Builder requestBuilder = HttpRequest.newBuilder() + .PUT(HttpRequest.BodyPublishers.ofByteArray(data)) + .uri(requestURL.toURI()); + + final HttpRequest request = requestBuilder.build(); + System.out.println("Initiating PUT request with " + data.length + " bytes"); + final HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.discarding()); + System.out.println("Status code for data with " + data.length + " bytes is " + response.statusCode()); + } + + private static HttpServer createAndStartServer(final String webAppContext) throws IOException { + final InetSocketAddress address = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); + final HttpServer server = HttpServer.create(address, 0); + // setup the handler + final HttpContext context = server.createContext(webAppContext, new SimpleHandler()); + // start the server + server.start(); + return server; + } + + private static class SimpleHandler implements HttpHandler { + @Override + public void handle(final HttpExchange httpExchange) throws IOException { + final URI requestURI = httpExchange.getRequestURI(); + System.out.println("Handling " + httpExchange.getRequestMethod() + " request " + requestURI); + httpExchange.sendResponseHeaders(200, -1); + } + } + +} \ No newline at end of file