test/jdk/java/net/httpclient/Response204.java
changeset 52196 420445d16008
child 53300 54aa3ea04fe8
child 53700 4ce47bc1fb92
equal deleted inserted replaced
52195:f08c1d7a5c53 52196:420445d16008
       
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     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
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 8211437
       
    27  * @run main/othervm -Djdk.httpclient.HttpClient.log=headers,requests Response204
       
    28  * @summary
       
    29  */
       
    30 
       
    31 import com.sun.net.httpserver.*;
       
    32 
       
    33 import java.net.http.HttpClient;
       
    34 import java.net.http.HttpRequest;
       
    35 import java.net.http.HttpResponse;
       
    36 import java.util.*;
       
    37 import java.util.concurrent.*;
       
    38 import java.util.logging.*;
       
    39 import java.io.*;
       
    40 import java.net.*;
       
    41 
       
    42 /**
       
    43  * Verify that a 204 response code with no content-length is handled correctly
       
    44  */
       
    45 public class Response204 {
       
    46 
       
    47     public static void main (String[] args) throws Exception {
       
    48         Logger logger = Logger.getLogger ("com.sun.net.httpserver");
       
    49         ConsoleHandler c = new ConsoleHandler();
       
    50         c.setLevel (Level.WARNING);
       
    51         logger.addHandler (c);
       
    52         logger.setLevel (Level.WARNING);
       
    53         Handler handler = new Handler();
       
    54         InetSocketAddress addr = new InetSocketAddress (0);
       
    55         HttpServer server = HttpServer.create (addr, 0);
       
    56         HttpContext ctx = server.createContext ("/test", handler);
       
    57         ExecutorService executor = Executors.newCachedThreadPool();
       
    58         server.setExecutor (executor);
       
    59         server.start ();
       
    60 
       
    61         URI uri = new URI("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
       
    62 
       
    63         try {
       
    64             HttpClient client = HttpClient.newHttpClient();
       
    65             HttpRequest request = HttpRequest.newBuilder(uri)
       
    66                     .GET()
       
    67                     .build();
       
    68             HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
       
    69             if (response.statusCode() != 204)
       
    70                 throw new RuntimeException("wrong response code");
       
    71             if (response.body() != null && !response.body().equals(""))
       
    72                 throw new RuntimeException("should have received empty response");
       
    73             System.out.println(response.headers().firstValue("content-length").orElse("nichts"));
       
    74             System.out.println ("OK 1");
       
    75             // Send a second time. This time we should get exception because the server
       
    76             // is going to send an invalid 204 with a Content-length
       
    77             try {
       
    78                 response = client.send(request, HttpResponse.BodyHandlers.ofString());
       
    79                 throw new RuntimeException("send should have thrown exception");
       
    80             } catch (IOException ioe) {
       
    81                 System.out.println("OK 2");
       
    82             }
       
    83         } finally {
       
    84             server.stop(2);
       
    85             executor.shutdown();
       
    86         }
       
    87     }
       
    88 
       
    89     public static boolean error = false;
       
    90 
       
    91     static class Handler implements HttpHandler {
       
    92         volatile int counter = 0;
       
    93 
       
    94         public void handle(HttpExchange t)
       
    95                 throws IOException {
       
    96             InputStream is = t.getRequestBody();
       
    97             Headers map = t.getRequestHeaders();
       
    98             Headers rmap = t.getResponseHeaders();
       
    99             while (is.read() != -1) ;
       
   100             is.close();
       
   101             if (counter++ == 1) {
       
   102                 // pretend there is a body
       
   103                 rmap.set("Content-length", "10");
       
   104             }
       
   105             t.sendResponseHeaders(204, -1);
       
   106             t.close();
       
   107         }
       
   108     }
       
   109 }