Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/HttpClient/StreamingRetry.java
38867 views
/*1* Copyright (c) 2010, 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*/2829import java.net.HttpURLConnection;30import java.net.ServerSocket;31import java.net.URL;32import java.io.IOException;33import java.io.InputStream;34import java.io.OutputStream;35import static java.lang.System.out;3637public class StreamingRetry implements Runnable {38static final int ACCEPT_TIMEOUT = 20 * 1000; // 20 seconds39volatile ServerSocket ss;4041public static void main(String[] args) throws Exception {42(new StreamingRetry()).instanceMain();43}4445void instanceMain() throws Exception {46out.println("Test with default method");47test(null);48out.println("Test with POST method");49test("POST");50out.println("Test with PUT method");51test("PUT");5253if (failed > 0) throw new RuntimeException("Some tests failed");54}5556void test(String method) throws Exception {57ss = new ServerSocket(0);58ss.setSoTimeout(ACCEPT_TIMEOUT);59int port = ss.getLocalPort();6061Thread otherThread = new Thread(this);62otherThread.start();6364try {65URL url = new URL("http://localhost:" + port + "/");66HttpURLConnection uc = (HttpURLConnection) url.openConnection();67uc.setDoOutput(true);68if (method != null)69uc.setRequestMethod(method);70uc.setChunkedStreamingMode(4096);71OutputStream os = uc.getOutputStream();72os.write("Hello there".getBytes());7374InputStream is = uc.getInputStream();75is.close();76} catch (IOException expected) {77//expected.printStackTrace();78} finally {79ss.close();80otherThread.join();81}82}8384// Server85public void run() {86try {87(ss.accept()).close();88(ss.accept()).close();89ss.close();90fail("The server shouldn't accept a second connection");91} catch (IOException e) {92//OK, the client will close the server socket if successful93}94}9596volatile int failed = 0;97void fail() {failed++; Thread.dumpStack();}98void fail(String msg) {System.err.println(msg); fail();}99}100101102