Path: blob/master/test/jdk/sun/net/www/http/ChunkedOutputStream/CheckError.java
41154 views
/*1* Copyright (c) 2004, 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 505401626* @library /test/lib27* @summary get the failure immediately when writing individual chunks over socket fail28* @run main CheckError29* @run main/othervm -Djava.net.preferIPv6Addresses=true CheckError30*/3132import java.io.BufferedReader;33import java.io.IOException;34import java.io.InputStream;35import java.io.InputStreamReader;36import java.io.OutputStream;37import java.net.HttpURLConnection;38import java.net.InetAddress;39import java.net.InetSocketAddress;40import java.net.Proxy;41import java.net.ServerSocket;42import java.net.Socket;43import java.net.URL;44import static java.lang.System.out;4546import jdk.test.lib.net.URIBuilder;4748public class CheckError {4950static int BUFFER_SIZE = 8192; // 8k51static int TOTAL_BYTES = 1 * 1024 * 1024; // 1M5253public static void main(String[] args) throws Exception {5455HTTPServer server = new HTTPServer();56server.start();57int port = server.getPort();58out.println("Server listening on " + port);596061URL url = URIBuilder.newBuilder()62.scheme("http")63.host(server.getAddress())64.port(port)65.toURL();6667HttpURLConnection conn = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);68conn.setRequestMethod("POST");69conn.setDoOutput(true);70conn.setChunkedStreamingMode(1024);7172out.println("sending " + TOTAL_BYTES + " bytes");7374int byteAtOnce;75int sendingBytes = TOTAL_BYTES;76byte[] buffer = getBuffer(BUFFER_SIZE);77try (OutputStream toServer = conn.getOutputStream()) {78while (sendingBytes > 0) {79if (sendingBytes > BUFFER_SIZE) {80byteAtOnce = BUFFER_SIZE;81} else {82byteAtOnce = sendingBytes;83}84toServer.write(buffer, 0, byteAtOnce);85sendingBytes -= byteAtOnce;86out.print((TOTAL_BYTES - sendingBytes) + " was sent. ");87toServer.flush();88// gives the server thread time to read, and eventually close;89Thread.sleep(500);90}91} catch (IOException expected) {92// Expected IOException due to server.close()93out.println("PASSED. Caught expected: " + expected);94return;95}9697// Expected IOException not received. FAIL98throw new RuntimeException("Failed: Expected IOException not received");99}100101static byte[] getBuffer(int size) {102byte[] buffer = new byte[size];103for (int i = 0; i < size; i++)104buffer[i] = (byte)i;105return buffer;106}107108static class HTTPServer extends Thread {109110final ServerSocket serverSocket;111112HTTPServer() throws IOException {113InetAddress loopback = InetAddress.getLoopbackAddress();114serverSocket = new ServerSocket();115serverSocket.bind(new InetSocketAddress(loopback, 0));116}117118int getPort() {119return serverSocket.getLocalPort();120}121122InetAddress getAddress() {123return serverSocket.getInetAddress();124}125126public void run() {127try (Socket client = serverSocket.accept()) {128129InputStream in = client.getInputStream();130BufferedReader reader = new BufferedReader(new InputStreamReader(in));131String line;132do {133line = reader.readLine();134out.println("Server: " + line);135} while (line != null && line.length() > 0);136137System.out.println("Server: receiving some data");138// just read some data, then close the connection139in.read(new byte[1024]);140141in.close();142client.close();143out.println("Server closed socket");144} catch (IOException e) {145e.printStackTrace();146}147}148}149}150151152