test/jdk/java/net/URLConnection/ChunkedEncoding.java
changeset 55645 3203e857fa71
parent 47216 71c04702a3d5
equal deleted inserted replaced
55644:556313991cac 55645:3203e857fa71
     1 /*
     1 /*
     2  * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  *
    25  * @test
    26  * @bug 4333920
    26  * @bug 4333920
    27  * @bug 4394548
    27  * @bug 4394548
       
    28  * @library /test/lib
    28  * @summary Check that chunked encoding response doesn't cause
    29  * @summary Check that chunked encoding response doesn't cause
    29  *          getInputStream to block until last chunk arrives.
    30  *          getInputStream to block until last chunk arrives.
    30  *          Also regression against NPE in ChunkedInputStream.
    31  *          Also regression against NPE in ChunkedInputStream.
    31  */
    32  */
    32 import java.net.*;
    33 import java.net.*;
    33 import java.io.*;
    34 import java.io.*;
    34 import java.util.Random;
    35 import java.util.Random;
       
    36 import jdk.test.lib.net.URIBuilder;
    35 
    37 
    36 public class ChunkedEncoding implements Runnable {
    38 public class ChunkedEncoding implements Runnable {
    37 
    39 
    38     ServerSocket ss;
    40     ServerSocket ss;
    39 
    41 
   113             String trailer = "Checksum:" + cs + "\r\n";
   115             String trailer = "Checksum:" + cs + "\r\n";
   114             out.print(trailer);
   116             out.print(trailer);
   115             out.print("\r\n");
   117             out.print("\r\n");
   116             out.flush();
   118             out.flush();
   117 
   119 
       
   120             /*
       
   121              * Sleep added to avoid connection reset
       
   122              * on the client side
       
   123              */
       
   124             Thread.sleep(1000);
   118             s.close();
   125             s.close();
   119             ss.close();
   126             ss.close();
   120         } catch (Exception e) {
   127         } catch (Exception e) {
   121             e.printStackTrace();
   128             e.printStackTrace();
   122         }
   129         }
   123     }
   130     }
   124 
   131 
   125     ChunkedEncoding() throws Exception {
   132     ChunkedEncoding() throws Exception {
   126 
   133 
   127         /* start the server */
   134         /* start the server */
   128         ss = new ServerSocket(0);
   135         ss = new ServerSocket();
       
   136         ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
   129         (new Thread(this)).start();
   137         (new Thread(this)).start();
   130 
       
   131         /* establish http connection to server */
   138         /* establish http connection to server */
   132         String uri = "http://localhost:" +
   139         URL url = URIBuilder.newBuilder()
   133                      Integer.toString(ss.getLocalPort()) +
   140                 .scheme("http")
   134                      "/foo";
   141                 .loopback()
   135         URL url = new URL(uri);
   142                 .port(ss.getLocalPort())
   136         HttpURLConnection http = (HttpURLConnection)url.openConnection();
   143                 .path("/foo")
       
   144                 .toURL();
       
   145         HttpURLConnection http = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
   137 
   146 
   138         /*
   147         /*
   139          * Server should only send headers if TE:trailers
   148          * Server should only send headers if TE:trailers
   140          * specified - see updated HTTP 1.1 spec.
   149          * specified - see updated HTTP 1.1 spec.
   141          */
   150          */