Path: blob/master/test/jdk/sun/net/www/http/HttpClient/RetryPost.java
41154 views
/*1* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6427251 638278826* @modules jdk.httpserver27* @library /test/lib28* @run main RetryPost29* @run main/othervm -Dsun.net.http.retryPost=false RetryPost noRetry30* @summary HttpURLConnection automatically retries non-idempotent method POST31*/3233import com.sun.net.httpserver.HttpContext;34import com.sun.net.httpserver.HttpExchange;35import com.sun.net.httpserver.HttpHandler;36import java.io.IOException;37import java.io.OutputStream;38import java.net.HttpURLConnection;39import java.net.InetAddress;40import java.net.InetSocketAddress;41import java.net.Proxy;42import java.net.SocketException;43import java.net.URL;44import java.util.concurrent.Executors;45import java.util.concurrent.ExecutorService;46import jdk.test.lib.net.URIBuilder;4748public class RetryPost49{50static boolean shouldRetry = true;5152com.sun.net.httpserver.HttpServer httpServer;53MyHandler httpHandler;54ExecutorService executorService;5556public static void main(String[] args) throws Exception {57if (args.length == 1 && args[0].equals("noRetry"))58shouldRetry = false;5960new RetryPost();61}6263public RetryPost() throws Exception {64startHttpServer(shouldRetry);65doClient();66}6768void doClient() throws Exception {69try {70InetSocketAddress address = httpServer.getAddress();71URL url = URIBuilder.newBuilder()72.scheme("http")73.host(address.getAddress())74.port(address.getPort())75.path("/test/")76.toURLUnchecked();77HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);78uc.setDoOutput(true);79uc.setRequestMethod("POST");80uc.getResponseCode();8182// if we reach here then we have failed83throw new RuntimeException("Failed: POST request being retried");8485} catch (SocketException se) {86System.out.println("Got expected exception: " + se);87// this is what we expect to happen and is OK.88if (shouldRetry && httpHandler.getCallCount() != 2) {89se.printStackTrace(System.out);90throw new RuntimeException("Failed: Handler should have been called twice. " +91"It was called "+ httpHandler.getCallCount() + " times");92} else if (!shouldRetry && httpHandler.getCallCount() != 1) {93se.printStackTrace(System.out);94throw new RuntimeException("Failed: Handler should have only been called once" +95"It was called "+ httpHandler.getCallCount() + " times");96}97} finally {98httpServer.stop(1);99executorService.shutdown();100}101}102103/**104* Http Server105*/106public void startHttpServer(boolean shouldRetry) throws IOException {107InetAddress loopback = InetAddress.getLoopbackAddress();108httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(loopback, 0), 0);109httpHandler = new MyHandler(shouldRetry);110111HttpContext ctx = httpServer.createContext("/test/", httpHandler);112113executorService = Executors.newCachedThreadPool();114httpServer.setExecutor(executorService);115httpServer.start();116}117118class MyHandler implements HttpHandler {119volatile int callCount = 0;120final boolean shouldRetry;121122public MyHandler(boolean shouldRetry) {123this.shouldRetry = shouldRetry;124}125126public void handle(HttpExchange t) throws IOException {127callCount++;128129if (callCount > 1 && !shouldRetry) {130// if this bug has been fixed then this method will not be called twice131// when -Dhttp.retryPost=false132t.sendResponseHeaders(400, -1); // indicate failure by returning 400133} else {134// simply close out the stream without sending any data.135OutputStream os = t.getResponseBody();136os.close();137}138}139140public int getCallCount() {141return callCount;142}143}144145}146147148