test/jdk/java/net/httpclient/ShortResponseBody.java
changeset 53256 bd8df96decba
parent 52121 934969c63223
child 58968 7f1daafda27b
equal deleted inserted replaced
53255:61a385765c9b 53256:bd8df96decba
     1 /*
     1 /*
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2018, 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.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
       
    26  * @bug 8216498
    26  * @summary Tests Exception detail message when too few response bytes are
    27  * @summary Tests Exception detail message when too few response bytes are
    27  *          received before a socket exception or eof.
    28  *          received before a socket exception or eof.
    28  * @library /test/lib
    29  * @library /test/lib
    29  * @build jdk.test.lib.net.SimpleSSLContext
    30  * @build jdk.test.lib.net.SimpleSSLContext
    30  * @run testng/othervm
    31  * @run testng/othervm
    59 import org.testng.annotations.AfterTest;
    60 import org.testng.annotations.AfterTest;
    60 import org.testng.annotations.BeforeTest;
    61 import org.testng.annotations.BeforeTest;
    61 import org.testng.annotations.DataProvider;
    62 import org.testng.annotations.DataProvider;
    62 import org.testng.annotations.Test;
    63 import org.testng.annotations.Test;
    63 import javax.net.ssl.SSLContext;
    64 import javax.net.ssl.SSLContext;
       
    65 import javax.net.ssl.SSLHandshakeException;
    64 import javax.net.ssl.SSLServerSocketFactory;
    66 import javax.net.ssl.SSLServerSocketFactory;
    65 import javax.net.ssl.SSLParameters;
    67 import javax.net.ssl.SSLParameters;
    66 import javax.net.ssl.SSLSocket;
    68 import javax.net.ssl.SSLSocket;
    67 import static java.lang.System.out;
    69 import static java.lang.System.out;
    68 import static java.net.http.HttpClient.Builder.NO_PROXY;
    70 import static java.net.http.HttpClient.Builder.NO_PROXY;
   222                 String body = response.body();
   224                 String body = response.body();
   223                 out.println(response + ": " + body);
   225                 out.println(response + ": " + body);
   224                 fail("UNEXPECTED RESPONSE: " + response);
   226                 fail("UNEXPECTED RESPONSE: " + response);
   225             } catch (IOException ioe) {
   227             } catch (IOException ioe) {
   226                 out.println("Caught expected exception:" + ioe);
   228                 out.println("Caught expected exception:" + ioe);
   227                 String msg = ioe.getMessage();
   229                 assertExpectedMessage(request, ioe, expectedMsg);
   228                 assertTrue(msg.contains(expectedMsg), "exception msg:[" + msg + "]");
       
   229                 // synchronous API must have the send method on the stack
   230                 // synchronous API must have the send method on the stack
   230                 assertSendMethodOnStack(ioe);
   231                 assertSendMethodOnStack(ioe);
   231                 assertNoConnectionExpiredException(ioe);
   232                 assertNoConnectionExpiredException(ioe);
   232             }
   233             }
   233         }
   234         }
   250                 fail("UNEXPECTED RESPONSE: " + response);
   251                 fail("UNEXPECTED RESPONSE: " + response);
   251             } catch (ExecutionException ee) {
   252             } catch (ExecutionException ee) {
   252                 if (ee.getCause() instanceof IOException) {
   253                 if (ee.getCause() instanceof IOException) {
   253                     IOException ioe = (IOException) ee.getCause();
   254                     IOException ioe = (IOException) ee.getCause();
   254                     out.println("Caught expected exception:" + ioe);
   255                     out.println("Caught expected exception:" + ioe);
   255                     String msg = ioe.getMessage();
   256                     assertExpectedMessage(request, ioe, expectedMsg);
   256                     assertTrue(msg.contains(expectedMsg), "exception msg:[" + msg + "]");
       
   257                     assertNoConnectionExpiredException(ioe);
   257                     assertNoConnectionExpiredException(ioe);
   258                 } else {
   258                 } else {
   259                     throw ee;
   259                     throw ee;
   260                 }
   260                 }
   261             }
   261             }
   333                 String body = response.body();
   333                 String body = response.body();
   334                 out.println(response + ": " + body);
   334                 out.println(response + ": " + body);
   335                 fail("UNEXPECTED RESPONSE: " + response);
   335                 fail("UNEXPECTED RESPONSE: " + response);
   336             } catch (IOException ioe) {
   336             } catch (IOException ioe) {
   337                 out.println("Caught expected exception:" + ioe);
   337                 out.println("Caught expected exception:" + ioe);
   338                 String msg = ioe.getMessage();
       
   339 
   338 
   340                 List<String> expectedMessages = new ArrayList<>();
   339                 List<String> expectedMessages = new ArrayList<>();
   341                 expectedMessages.add(expectedMsg);
   340                 expectedMessages.add(expectedMsg);
   342                 MSGS_ORDER.stream().takeWhile(s -> !s.equals(expectedMsg))
   341                 MSGS_ORDER.stream().takeWhile(s -> !s.equals(expectedMsg))
   343                                    .forEach(expectedMessages::add);
   342                                    .forEach(expectedMessages::add);
   344 
   343 
   345                 assertTrue(expectedMessages.stream().anyMatch(s -> msg.indexOf(s) != -1),
   344                 assertExpectedMessage(request, ioe, expectedMessages);
   346                            "exception msg:[" + msg + "], not in [" + expectedMessages);
       
   347                 // synchronous API must have the send method on the stack
   345                 // synchronous API must have the send method on the stack
   348                 assertSendMethodOnStack(ioe);
   346                 assertSendMethodOnStack(ioe);
   349                 assertNoConnectionExpiredException(ioe);
   347                 assertNoConnectionExpiredException(ioe);
   350             }
   348             }
   351         }
   349         }
   377                     List<String> expectedMessages = new ArrayList<>();
   375                     List<String> expectedMessages = new ArrayList<>();
   378                     expectedMessages.add(expectedMsg);
   376                     expectedMessages.add(expectedMsg);
   379                     MSGS_ORDER.stream().takeWhile(s -> !s.equals(expectedMsg))
   377                     MSGS_ORDER.stream().takeWhile(s -> !s.equals(expectedMsg))
   380                             .forEach(expectedMessages::add);
   378                             .forEach(expectedMessages::add);
   381 
   379 
   382                     assertTrue(expectedMessages.stream().anyMatch(s -> msg.indexOf(s) != -1),
   380                     assertExpectedMessage(request, ioe, expectedMessages);
   383                                "exception msg:[" + msg + "], not in [" + expectedMessages);
       
   384                     assertNoConnectionExpiredException(ioe);
   381                     assertNoConnectionExpiredException(ioe);
   385                 } else {
   382                 } else {
   386                     throw ee;
   383                     throw ee;
   387                 }
   384                 }
   388             }
   385             }
       
   386         }
       
   387     }
       
   388 
       
   389 
       
   390     void assertExpectedMessage(HttpRequest request, Throwable t, String expected) {
       
   391         if (request.uri().getScheme().equalsIgnoreCase("https")
       
   392                 && (t instanceof SSLHandshakeException)) {
       
   393             // OK
       
   394             out.println("Skipping expected " + t);
       
   395         } else {
       
   396             String msg = t.getMessage();
       
   397             assertTrue(msg.contains(expected),
       
   398                     "exception msg:[" + msg + "]");
       
   399         }
       
   400     }
       
   401 
       
   402     void assertExpectedMessage(HttpRequest request, Throwable t, List<String> expected) {
       
   403         if (request.uri().getScheme().equalsIgnoreCase("https")
       
   404                 && (t instanceof SSLHandshakeException)) {
       
   405             // OK
       
   406             out.println("Skipping expected " + t);
       
   407         } else {
       
   408             String msg = t.getMessage();
       
   409             assertTrue(expected.stream().anyMatch(msg::contains),
       
   410                     "exception msg:[" + msg + "] not in " + Arrays.asList(expected));
   389         }
   411         }
   390     }
   412     }
   391 
   413 
   392     // Asserts that the "send" method appears in the stack of the given
   414     // Asserts that the "send" method appears in the stack of the given
   393     // exception. The synchronous API must contain the send method on the stack.
   415     // exception. The synchronous API must contain the send method on the stack.