Path: blob/master/test/jdk/sun/net/www/http/HttpClient/StreamingRetry.java
41154 views
/*1* Copyright (c) 2010, 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 6672144 805098326* @summary Do not retry failed request with a streaming body.27* @library /test/lib28* @run main StreamingRetry29* @run main/othervm -Djava.net.preferIPv6Addresses=true StreamingRetry30*/3132import java.net.HttpURLConnection;33import java.net.InetAddress;34import java.net.InetSocketAddress;35import java.net.Proxy;36import java.net.ServerSocket;37import java.net.URL;38import java.io.IOException;39import java.io.InputStream;40import java.io.OutputStream;41import static java.lang.System.out;4243import jdk.test.lib.net.URIBuilder;4445public class StreamingRetry implements Runnable {46static final int ACCEPT_TIMEOUT = 20 * 1000; // 20 seconds47volatile ServerSocket ss;4849public static void main(String[] args) throws Exception {50(new StreamingRetry()).instanceMain();51}5253void instanceMain() throws Exception {54out.println("Test with default method");55test(null);56out.println("Test with POST method");57test("POST");58out.println("Test with PUT method");59test("PUT");6061if (failed > 0) throw new RuntimeException("Some tests failed");62}6364void test(String method) throws Exception {65ss = new ServerSocket();66ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));67ss.setSoTimeout(ACCEPT_TIMEOUT);68int port = ss.getLocalPort();6970Thread otherThread = new Thread(this);71otherThread.start();7273try {74URL url = URIBuilder.newBuilder()75.scheme("http")76.host(ss.getInetAddress())77.port(port)78.path("/")79.toURL();80HttpURLConnection uc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);81uc.setDoOutput(true);82if (method != null)83uc.setRequestMethod(method);84uc.setChunkedStreamingMode(4096);85OutputStream os = uc.getOutputStream();86os.write("Hello there".getBytes());8788InputStream is = uc.getInputStream();89is.close();90} catch (IOException expected) {91//expected.printStackTrace();92} finally {93ss.close();94otherThread.join();95}96}9798// Server99public void run() {100try {101(ss.accept()).close();102(ss.accept()).close();103ss.close();104fail("The server shouldn't accept a second connection");105} catch (IOException e) {106//OK, the client will close the server socket if successful107}108}109110volatile int failed = 0;111void fail() {failed++; Thread.dumpStack();}112void fail(String msg) {System.err.println(msg); fail();}113}114115116