test/jdk/java/net/httpclient/ShortResponseBody.java
author chegar
Wed, 13 Jun 2018 15:36:44 +0100
branchhttp-client-branch
changeset 56752 54e96d77af2c
parent 56730 b08918259eed
child 56756 ba60eaef37d7
permissions -rw-r--r--
http-client-branch: review comment: shared executor and fixes in ShortResponseTest
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
     1
/*
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
     4
 *
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
     8
 *
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    13
 * accompanied this code).
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    14
 *
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    18
 *
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    21
 * questions.
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    22
 */
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    23
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    24
/*
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    25
 * @test
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    26
 * @summary Tests Exception detail message when too few response bytes are
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    27
 *          received before a socket exception or eof.
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    28
 * @library /lib/testlibrary
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    29
 * @build jdk.testlibrary.SimpleSSLContext
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    30
 * @run testng/othervm
56752
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
    31
 *       -Djdk.httpclient.HttpClient.log=headers,errors
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    32
 *       -Djdk.internal.httpclient.debug=true
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    33
 *       ShortResponseBody
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    34
 * @run testng/othervm
56752
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
    35
 *       -Djdk.httpclient.HttpClient.log=headers,errors
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    36
 *       -Djdk.internal.httpclient.debug=true
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    37
 *       -Djdk.httpclient.enableAllMethodRetry
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    38
 *       ShortResponseBody
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    39
 */
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    40
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    41
import java.io.IOException;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    42
import java.io.InputStream;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    43
import java.io.OutputStream;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    44
import java.io.UncheckedIOException;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    45
import java.net.InetAddress;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    46
import java.net.InetSocketAddress;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    47
import java.net.ServerSocket;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    48
import java.net.Socket;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    49
import java.net.URI;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    50
import java.net.http.HttpClient;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    51
import java.net.http.HttpRequest;
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    52
import java.net.http.HttpRequest.BodyPublishers;
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    53
import java.net.http.HttpResponse;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    54
import java.util.ArrayList;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    55
import java.util.Arrays;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    56
import java.util.List;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    57
import java.util.concurrent.ExecutionException;
56752
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
    58
import java.util.concurrent.Executor;
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
    59
import java.util.concurrent.ExecutorService;
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
    60
import java.util.concurrent.Executors;
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
    61
import java.util.concurrent.ThreadFactory;
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
    62
import java.util.concurrent.atomic.AtomicLong;
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    63
import java.util.stream.Stream;
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    64
import jdk.testlibrary.SimpleSSLContext;
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    65
import org.testng.annotations.AfterTest;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    66
import org.testng.annotations.BeforeTest;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    67
import org.testng.annotations.DataProvider;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    68
import org.testng.annotations.Test;
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    69
import javax.net.ssl.SSLContext;
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    70
import javax.net.ssl.SSLServerSocketFactory;
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    71
import javax.net.ssl.SSLSocket;
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    72
import static java.lang.System.out;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    73
import static java.net.http.HttpClient.Builder.NO_PROXY;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    74
import static java.net.http.HttpResponse.BodyHandlers.ofString;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    75
import static java.nio.charset.StandardCharsets.US_ASCII;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    76
import static java.util.stream.Collectors.toList;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    77
import static org.testng.Assert.assertTrue;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    78
import static org.testng.Assert.assertEquals;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    79
import static org.testng.Assert.fail;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    80
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    81
public class ShortResponseBody {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    82
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    83
    Server closeImmediatelyServer;
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    84
    Server closeImmediatelyHttpsServer;
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    85
    Server variableLengthServer;
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    86
    Server variableLengthHttpsServer;
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    87
    Server fixedLengthServer;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    88
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    89
    String httpURIClsImed;
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    90
    String httpsURIClsImed;
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    91
    String httpURIVarLen;
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    92
    String httpsURIVarLen;
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    93
    String httpURIFixLen;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    94
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    95
    SSLContext sslContext;
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
    96
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    97
    static final String EXPECTED_RESPONSE_BODY =
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    98
            "<html><body><h1>Heading</h1><p>Some Text</p></body></html>";
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
    99
56752
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   100
    final static AtomicLong ids = new AtomicLong();
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   101
    final ThreadFactory factory = new ThreadFactory() {
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   102
        @Override
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   103
        public Thread newThread(Runnable r) {
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   104
            Thread thread = new Thread(r,  "HttpClient-Worker-" + ids.incrementAndGet());
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   105
            thread.setDaemon(true);
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   106
            return thread;
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   107
        }
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   108
    };
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   109
    final ExecutorService service = Executors.newCachedThreadPool(factory);
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   110
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   111
    @DataProvider(name = "sanity")
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   112
    public Object[][] sanity() {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   113
        return new Object[][]{
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   114
            { httpURIVarLen  + "?length=all" },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   115
            { httpsURIVarLen + "?length=all" },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   116
            { httpURIFixLen  + "?length=all" },
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   117
        };
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   118
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   119
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   120
    @Test(dataProvider = "sanity")
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   121
    void sanity(String url) throws Exception {
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   122
        HttpClient client = HttpClient.newBuilder()
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   123
                .proxy(NO_PROXY)
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   124
                .sslContext(sslContext)
56752
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   125
                .executor(service)
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   126
                .build();
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   127
        HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   128
        HttpResponse<String> response = client.send(request, ofString());
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   129
        String body = response.body();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   130
        assertEquals(body, EXPECTED_RESPONSE_BODY);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   131
        client.sendAsync(request, ofString())
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   132
                .thenApply(resp -> resp.body())
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   133
                .thenAccept(b -> assertEquals(b, EXPECTED_RESPONSE_BODY))
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   134
                .join();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   135
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   136
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   137
    @DataProvider(name = "uris")
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   138
    public Object[][] variants() {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   139
        String[][] cases = new String[][] {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   140
            // The length query string is the total number of bytes in the reply,
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   141
            // including headers, before the server closes the connection. The
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   142
            // second arg is a partial-expected-detail message in the exception.
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   143
            { httpURIVarLen + "?length=0",   "no bytes"     }, // EOF without receiving anything
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   144
            { httpURIVarLen + "?length=1",   "status line"  }, // EOF during status-line
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   145
            { httpURIVarLen + "?length=2",   "status line"  },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   146
            { httpURIVarLen + "?length=10",  "status line"  },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   147
            { httpURIVarLen + "?length=19",  "header"       }, // EOF during Content-Type header
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   148
            { httpURIVarLen + "?length=30",  "header"       },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   149
            { httpURIVarLen + "?length=45",  "header"       },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   150
            { httpURIVarLen + "?length=48",  "header"       },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   151
            { httpURIVarLen + "?length=51",  "header"       },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   152
            { httpURIVarLen + "?length=98",  "header"       }, // EOF during Connection header
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   153
            { httpURIVarLen + "?length=100", "header"       },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   154
            { httpURIVarLen + "?length=101", "header"       },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   155
            { httpURIVarLen + "?length=104", "header"       },
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   156
            { httpURIVarLen + "?length=106", "chunked transfer encoding" }, // EOF during chunk header ( length )
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   157
            { httpURIVarLen + "?length=110", "chunked transfer encoding" }, // EOF during chunk response body data
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   158
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   159
            { httpsURIVarLen + "?length=0",   "no bytes"    },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   160
            { httpsURIVarLen + "?length=1",   "status line" },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   161
            { httpsURIVarLen + "?length=2",   "status line" },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   162
            { httpsURIVarLen + "?length=10",  "status line" },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   163
            { httpsURIVarLen + "?length=19",  "header"      },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   164
            { httpsURIVarLen + "?length=30",  "header"      },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   165
            { httpsURIVarLen + "?length=45",  "header"      },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   166
            { httpsURIVarLen + "?length=48",  "header"      },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   167
            { httpsURIVarLen + "?length=51",  "header"      },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   168
            { httpsURIVarLen + "?length=98",  "header"      },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   169
            { httpsURIVarLen + "?length=100", "header"      },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   170
            { httpsURIVarLen + "?length=101", "header"      },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   171
            { httpsURIVarLen + "?length=104", "header"      },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   172
            { httpsURIVarLen + "?length=106", "chunked transfer encoding" },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   173
            { httpsURIVarLen + "?length=110", "chunked transfer encoding" },
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   174
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   175
            { httpURIFixLen + "?length=0",   "no bytes"    }, // EOF without receiving anything
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   176
            { httpURIFixLen + "?length=1",   "status line" }, // EOF during status-line
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   177
            { httpURIFixLen + "?length=2",   "status line" },
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   178
            { httpURIFixLen + "?length=10",  "status line" },
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   179
            { httpURIFixLen + "?length=19",  "header"      }, // EOF during Content-Type header
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   180
            { httpURIFixLen + "?length=30",  "header"      },
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   181
            { httpURIFixLen + "?length=45",  "header"      },
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   182
            { httpURIFixLen + "?length=48",  "header"      },
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   183
            { httpURIFixLen + "?length=51",  "header"      },
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   184
            { httpURIFixLen + "?length=78",  "header"      }, // EOF during Connection header
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   185
            { httpURIFixLen + "?length=79",  "header"      },
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   186
            { httpURIFixLen + "?length=86",  "header"      },
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   187
            { httpURIFixLen + "?length=104", "fixed content-length" }, // EOF during body
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   188
            { httpURIFixLen + "?length=106", "fixed content-length" },
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   189
            { httpURIFixLen + "?length=110", "fixed content-length" },
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   190
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   191
            // ## ADD https fixed
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   192
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   193
            { httpURIClsImed,  "no bytes"},
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   194
            { httpsURIClsImed, "no bytes"},
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   195
        };
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   196
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   197
        List<Object[]> list = new ArrayList<>();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   198
        Arrays.asList(cases).stream()
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   199
                .map(e -> new Object[] {e[0], e[1], true})  // reuse client
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   200
                .forEach(list::add);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   201
        Arrays.asList(cases).stream()
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   202
                .map(e -> new Object[] {e[0], e[1], false}) // do not reuse client
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   203
                .forEach(list::add);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   204
        return list.stream().toArray(Object[][]::new);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   205
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   206
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   207
    static final int ITERATION_COUNT = 3;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   208
56752
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   209
    HttpClient newHttpClient() {
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   210
        return HttpClient.newBuilder()
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   211
                .proxy(NO_PROXY)
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   212
                .sslContext(sslContext)
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   213
                .executor(service)
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   214
                .build();
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   215
    }
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   216
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   217
    @Test(dataProvider = "uris")
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   218
    void testSynchronousGET(String url, String expectedMsg, boolean sameClient)
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   219
        throws Exception
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   220
    {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   221
        out.print("---\n");
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   222
        HttpClient client = null;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   223
        for (int i=0; i< ITERATION_COUNT; i++) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   224
            if (!sameClient || client == null)
56752
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   225
                client = newHttpClient();
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   226
            HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   227
            try {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   228
                HttpResponse<String> response = client.send(request, ofString());
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   229
                String body = response.body();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   230
                out.println(response + ": " + body);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   231
                fail("UNEXPECTED RESPONSE: " + response);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   232
            } catch (IOException ioe) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   233
                out.println("Caught expected exception:" + ioe);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   234
                String msg = ioe.getMessage();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   235
                assertTrue(msg.contains(expectedMsg), "exception msg:[" + msg + "]");
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   236
                // synchronous API must have the send method on the stack
56695
d219df0c7d24 http-client-branch: synchronous send should be on the exception stack
chegar
parents: 56682
diff changeset
   237
                assertSendMethodOnStack(ioe);
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   238
                assertNoConnectionExpiredException(ioe);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   239
            }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   240
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   241
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   242
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   243
    @Test(dataProvider = "uris")
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   244
    void testAsynchronousGET(String url, String expectedMsg, boolean sameClient)
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   245
        throws Exception
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   246
    {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   247
        out.print("---\n");
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   248
        HttpClient client = null;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   249
        for (int i=0; i< ITERATION_COUNT; i++) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   250
            if (!sameClient || client == null)
56752
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   251
                client = newHttpClient();
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   252
            HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   253
            try {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   254
                HttpResponse<String> response = client.sendAsync(request, ofString()).get();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   255
                String body = response.body();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   256
                out.println(response + ": " + body);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   257
                fail("UNEXPECTED RESPONSE: " + response);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   258
            } catch (ExecutionException ee) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   259
                if (ee.getCause() instanceof IOException) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   260
                    IOException ioe = (IOException) ee.getCause();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   261
                    out.println("Caught expected exception:" + ioe);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   262
                    String msg = ioe.getMessage();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   263
                    assertTrue(msg.contains(expectedMsg), "exception msg:[" + msg + "]");
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   264
                    assertNoConnectionExpiredException(ioe);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   265
                } else {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   266
                    throw ee;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   267
                }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   268
            }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   269
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   270
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   271
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   272
    // can be used to prolong request body publication
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   273
    static final class InfiniteInputStream extends InputStream {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   274
        @Override
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   275
        public int read() throws IOException {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   276
            return 1;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   277
        }
56752
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   278
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   279
        @Override
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   280
        public int read(byte[] buf, int offset, int length) {
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   281
            return length;
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   282
        }
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   283
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   284
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   285
    // POST tests are racy in what may be received before writing may cause a
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   286
    // broken pipe or reset exception, before all the received data can be read.
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   287
    // Any message up to, and including, the "expected" error message can occur.
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   288
    // Strictly ordered list, in order of possible occurrence.
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   289
    static final List<String> MSGS_ORDER =
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   290
            List.of("no bytes", "status line", "header");
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   291
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   292
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   293
    @Test(dataProvider = "uris")
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   294
    void testSynchronousPOST(String url, String expectedMsg, boolean sameClient)
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   295
        throws Exception
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   296
    {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   297
        out.print("---\n");
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   298
        HttpClient client = null;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   299
        for (int i=0; i< ITERATION_COUNT; i++) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   300
            if (!sameClient || client == null)
56752
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   301
                client = newHttpClient();
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   302
            HttpRequest request = HttpRequest.newBuilder(URI.create(url))
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   303
                    .POST(BodyPublishers.ofInputStream(() -> new InfiniteInputStream()))
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   304
                    .build();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   305
            try {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   306
                HttpResponse<String> response = client.send(request, ofString());
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   307
                String body = response.body();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   308
                out.println(response + ": " + body);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   309
                fail("UNEXPECTED RESPONSE: " + response);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   310
            } catch (IOException ioe) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   311
                out.println("Caught expected exception:" + ioe);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   312
                String msg = ioe.getMessage();
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   313
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   314
                List<String> expectedMessages = new ArrayList<>();
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   315
                expectedMessages.add(expectedMsg);
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   316
                MSGS_ORDER.stream().takeWhile(s -> !s.equals(expectedMsg))
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   317
                                   .forEach(expectedMessages::add);
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   318
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   319
                assertTrue(expectedMessages.stream().anyMatch(s -> msg.indexOf(s) != -1),
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   320
                           "exception msg:[" + msg + "], not in [" + expectedMessages);
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   321
                // synchronous API must have the send method on the stack
56695
d219df0c7d24 http-client-branch: synchronous send should be on the exception stack
chegar
parents: 56682
diff changeset
   322
                assertSendMethodOnStack(ioe);
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   323
                assertNoConnectionExpiredException(ioe);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   324
            }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   325
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   326
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   327
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   328
    @Test(dataProvider = "uris")
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   329
    void testAsynchronousPOST(String url, String expectedMsg, boolean sameClient)
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   330
        throws Exception
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   331
    {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   332
        out.print("---\n");
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   333
        HttpClient client = null;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   334
        for (int i=0; i< ITERATION_COUNT; i++) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   335
            if (!sameClient || client == null)
56752
54e96d77af2c http-client-branch: review comment: shared executor and fixes in ShortResponseTest
chegar
parents: 56730
diff changeset
   336
                client = newHttpClient();
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   337
            HttpRequest request = HttpRequest.newBuilder(URI.create(url))
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   338
                    .POST(BodyPublishers.ofInputStream(() -> new InfiniteInputStream()))
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   339
                    .build();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   340
            try {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   341
                HttpResponse<String> response = client.sendAsync(request, ofString()).get();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   342
                String body = response.body();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   343
                out.println(response + ": " + body);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   344
                fail("UNEXPECTED RESPONSE: " + response);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   345
            } catch (ExecutionException ee) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   346
                if (ee.getCause() instanceof IOException) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   347
                    IOException ioe = (IOException) ee.getCause();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   348
                    out.println("Caught expected exception:" + ioe);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   349
                    String msg = ioe.getMessage();
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   350
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   351
                    List<String> expectedMessages = new ArrayList<>();
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   352
                    expectedMessages.add(expectedMsg);
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   353
                    MSGS_ORDER.stream().takeWhile(s -> !s.equals(expectedMsg))
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   354
                            .forEach(expectedMessages::add);
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   355
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   356
                    assertTrue(expectedMessages.stream().anyMatch(s -> msg.indexOf(s) != -1),
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   357
                               "exception msg:[" + msg + "], not in [" + expectedMessages);
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   358
                    assertNoConnectionExpiredException(ioe);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   359
                } else {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   360
                    throw ee;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   361
                }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   362
            }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   363
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   364
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   365
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   366
    // Asserts that the "send" method appears in the stack of the given
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   367
    // exception. The synchronous API must contain the send method on the stack.
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   368
    static void assertSendMethodOnStack(IOException ioe) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   369
        final String cn = "jdk.internal.net.http.HttpClientImpl";
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   370
        List<StackTraceElement> list = Stream.of(ioe.getStackTrace())
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   371
                .filter(ste -> ste.getClassName().equals(cn)
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   372
                        && ste.getMethodName().equals("send"))
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   373
                .collect(toList());
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   374
        if (list.size() != 1) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   375
            ioe.printStackTrace(out);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   376
            fail(cn + ".send method not found in stack.");
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   377
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   378
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   379
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   380
    // Asserts that the implementation-specific ConnectionExpiredException does
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   381
    // NOT appear anywhere in the exception or its causal chain.
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   382
    static void assertNoConnectionExpiredException(IOException ioe) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   383
        Throwable throwable = ioe;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   384
        do {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   385
            String cn = throwable.getClass().getSimpleName();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   386
            if (cn.equals("ConnectionExpiredException")) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   387
                ioe.printStackTrace(out);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   388
                fail("UNEXPECTED ConnectionExpiredException in:[" + ioe + "]");
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   389
            }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   390
        } while ((throwable = throwable.getCause()) != null);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   391
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   392
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   393
    // -- infra
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   394
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   395
    /**
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   396
     * A server that, listens on a port, accepts new connections, and can be
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   397
     * closed.
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   398
     */
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   399
    static abstract class Server extends Thread implements AutoCloseable {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   400
        protected final ServerSocket ss;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   401
        protected volatile boolean closed;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   402
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   403
        Server(String name) throws IOException {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   404
            super(name);
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   405
            ss = newServerSocket();
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   406
            ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   407
            this.start();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   408
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   409
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   410
        protected ServerSocket newServerSocket() throws IOException {
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   411
            return new ServerSocket();
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   412
        }
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   413
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   414
        public int getPort() { return ss.getLocalPort(); }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   415
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   416
        @Override
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   417
        public void close() {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   418
            if (closed)
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   419
                return;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   420
            closed = true;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   421
            try {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   422
                ss.close();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   423
            } catch (IOException e) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   424
                throw new UncheckedIOException("Unexpected", e);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   425
            }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   426
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   427
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   428
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   429
    /**
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   430
     * A server that closes the connection immediately, without reading or writing.
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   431
     */
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   432
    static class PlainCloseImmediatelyServer extends Server {
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   433
        PlainCloseImmediatelyServer() throws IOException {
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   434
            super("PlainCloseImmediatelyServer");
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   435
        }
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   436
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   437
        protected PlainCloseImmediatelyServer(String name) throws IOException {
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   438
            super(name);
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   439
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   440
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   441
        @Override
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   442
        public void run() {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   443
            while (!closed) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   444
                try (Socket s = ss.accept()) {
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   445
                    if (s instanceof SSLSocket) {
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   446
                        ((SSLSocket)s).startHandshake();
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   447
                    }
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   448
                    out.println("Server: got connection, closing immediately ");
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   449
                } catch (IOException e) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   450
                    if (!closed)
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   451
                        throw new UncheckedIOException("Unexpected", e);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   452
                }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   453
            }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   454
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   455
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   456
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   457
    /**
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   458
     * A server that closes the connection immediately, without reading or writing,
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   459
     * after completing the SSL handshake.
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   460
     */
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   461
    static final class SSLCloseImmediatelyServer extends PlainCloseImmediatelyServer {
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   462
        SSLCloseImmediatelyServer() throws IOException {
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   463
            super("SSLCloseImmediatelyServer");
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   464
        }
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   465
        @Override
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   466
        public ServerSocket newServerSocket() throws IOException {
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   467
            return SSLServerSocketFactory.getDefault().createServerSocket();
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   468
        }
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   469
    }
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   470
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   471
    /**
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   472
     * A server that replies with headers and a, possibly partial, reply, before
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   473
     * closing the connection. The number of bytes of written ( header + body),
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   474
     * is controllable through the "length" query string param in the requested
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   475
     * URI.
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   476
     */
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   477
    static abstract class ReplyingServer extends Server {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   478
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   479
        private final String name;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   480
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   481
        ReplyingServer(String name) throws IOException {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   482
            super(name);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   483
            this.name = name;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   484
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   485
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   486
        abstract String response();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   487
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   488
        @Override
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   489
        public void run() {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   490
            while (!closed) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   491
                try (Socket s = ss.accept()) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   492
                    out.print(name + ": got connection ");
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   493
                    InputStream is = s.getInputStream();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   494
                    URI requestMethod = readRequestMethod(is);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   495
                    out.print(requestMethod + " ");
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   496
                    URI uriPath = readRequestPath(is);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   497
                    out.println(uriPath);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   498
                    readRequestHeaders(is);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   499
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   500
                    String query = uriPath.getRawQuery();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   501
                    assert query != null;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   502
                    String qv = query.split("=")[1];
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   503
                    int len;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   504
                    if (qv.equals("all")) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   505
                        len = response().getBytes(US_ASCII).length;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   506
                    } else {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   507
                        len = Integer.parseInt(query.split("=")[1]);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   508
                    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   509
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   510
                    OutputStream os = s.getOutputStream();
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   511
                    out.println(name + ": writing " + len  + " bytes");
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   512
                    byte[] responseBytes = response().getBytes(US_ASCII);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   513
                    for (int i = 0; i< len; i++) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   514
                        os.write(responseBytes[i]);
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   515
                        os.flush();
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   516
                    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   517
                } catch (IOException e) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   518
                    if (!closed)
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   519
                        throw new UncheckedIOException("Unexpected", e);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   520
                }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   521
            }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   522
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   523
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   524
        static final byte[] requestEnd = new byte[] { '\r', '\n', '\r', '\n' };
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   525
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   526
        // Read the request method
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   527
        static URI readRequestMethod(InputStream is) throws IOException {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   528
            StringBuilder sb = new StringBuilder();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   529
            int r;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   530
            while ((r = is.read()) != -1 && r != 0x20) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   531
                sb.append((char)r);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   532
            }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   533
            return URI.create(sb.toString());
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   534
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   535
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   536
        // Read the request URI path
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   537
        static URI readRequestPath(InputStream is) throws IOException {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   538
            StringBuilder sb = new StringBuilder();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   539
            int r;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   540
            while ((r = is.read()) != -1 && r != 0x20) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   541
                sb.append((char)r);
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   542
            }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   543
            return URI.create(sb.toString());
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   544
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   545
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   546
        // Read until the end of a HTTP request headers
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   547
        static void readRequestHeaders(InputStream is) throws IOException {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   548
            int requestEndCount = 0, r;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   549
            while ((r = is.read()) != -1) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   550
                if (r == requestEnd[requestEndCount]) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   551
                    requestEndCount++;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   552
                    if (requestEndCount == 4) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   553
                        break;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   554
                    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   555
                } else {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   556
                    requestEndCount = 0;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   557
                }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   558
            }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   559
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   560
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   561
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   562
    /** A server that issues a, possibly-partial, chunked reply. */
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   563
    static class PlainVariableLengthServer extends ReplyingServer {
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   564
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   565
        static final String CHUNKED_RESPONSE_BODY =
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   566
                "6\r\n"+ "<html>\r\n" +
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   567
                "6\r\n"+ "<body>\r\n" +
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   568
                "10\r\n"+ "<h1>Heading</h1>\r\n" +
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   569
                "10\r\n"+ "<p>Some Text</p>\r\n" +
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   570
                "7\r\n"+ "</body>\r\n" +
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   571
                "7\r\n"+ "</html>\r\n" +
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   572
                "0\r\n"+ "\r\n";
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   573
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   574
        static final String RESPONSE_HEADERS =
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   575
                "HTTP/1.1 200 OK\r\n" +
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   576
                "Content-Type: text/html; charset=utf-8\r\n" +
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   577
                "Transfer-Encoding: chunked\r\n" +
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   578
                "Connection: close\r\n\r\n";
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   579
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   580
        static final String RESPONSE = RESPONSE_HEADERS + CHUNKED_RESPONSE_BODY;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   581
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   582
        PlainVariableLengthServer() throws IOException {
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   583
            super("PlainVariableLengthServer");
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   584
        }
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   585
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   586
        protected PlainVariableLengthServer(String name) throws IOException {
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   587
            super(name);
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   588
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   589
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   590
        @Override
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   591
        String response( ) { return RESPONSE; }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   592
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   593
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   594
    /** A server that issues a, possibly-partial, chunked reply over SSL. */
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   595
    static final class SSLVariableLengthServer extends PlainVariableLengthServer {
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   596
        SSLVariableLengthServer() throws IOException {
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   597
            super("SSLVariableLengthServer");
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   598
        }
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   599
        @Override
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   600
        public ServerSocket newServerSocket() throws IOException {
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   601
            return SSLServerSocketFactory.getDefault().createServerSocket();
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   602
        }
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   603
    }
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   604
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   605
    /** A server that issues a fixed-length reply. */
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   606
    static final class FixedLengthServer extends ReplyingServer {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   607
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   608
        static final String RESPONSE_BODY = EXPECTED_RESPONSE_BODY;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   609
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   610
        static final String RESPONSE_HEADERS =
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   611
                "HTTP/1.1 200 OK\r\n" +
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   612
                "Content-Type: text/html; charset=utf-8\r\n" +
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   613
                "Content-Length: " + RESPONSE_BODY.length() + "\r\n" +
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   614
                "Connection: close\r\n\r\n";
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   615
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   616
        static final String RESPONSE = RESPONSE_HEADERS + RESPONSE_BODY;
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   617
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   618
        FixedLengthServer() throws IOException {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   619
            super("FixedLengthServer");
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   620
        }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   621
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   622
        @Override
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   623
        String response( ) { return RESPONSE; }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   624
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   625
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   626
    static String serverAuthority(Server server) {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   627
        return InetAddress.getLoopbackAddress().getHostName() + ":"
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   628
                + server.getPort();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   629
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   630
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   631
    @BeforeTest
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   632
    public void setup() throws Exception {
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   633
        sslContext = new SimpleSSLContext().get();
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   634
        if (sslContext == null)
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   635
            throw new AssertionError("Unexpected null sslContext");
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   636
        SSLContext.setDefault(sslContext);
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   637
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   638
        closeImmediatelyServer = new PlainCloseImmediatelyServer();
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   639
        httpURIClsImed = "http://" + serverAuthority(closeImmediatelyServer)
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   640
                + "/http1/closeImmediately/foo";
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   641
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   642
        closeImmediatelyHttpsServer = new SSLCloseImmediatelyServer();
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   643
        httpsURIClsImed = "https://" + serverAuthority(closeImmediatelyHttpsServer)
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   644
                + "/https1/closeImmediately/foo";
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   645
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   646
        variableLengthServer = new PlainVariableLengthServer();
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   647
        httpURIVarLen = "http://" + serverAuthority(variableLengthServer)
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   648
                + "/http1/variable/bar";
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   649
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   650
        variableLengthHttpsServer = new SSLVariableLengthServer();
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   651
        httpsURIVarLen = "https://" + serverAuthority(variableLengthHttpsServer)
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   652
                + "/https1/variable/bar";
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   653
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   654
        fixedLengthServer = new FixedLengthServer();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   655
        httpURIFixLen = "http://" + serverAuthority(fixedLengthServer)
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   656
                + "/http1/fixed/baz";
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   657
    }
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   658
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   659
    @AfterTest
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   660
    public void teardown() throws Exception {
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   661
        closeImmediatelyServer.close();
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   662
        closeImmediatelyHttpsServer.close();
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   663
        variableLengthServer.close();
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   664
        variableLengthHttpsServer.close();
56682
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   665
        fixedLengthServer.close();
9822bbe48b9b http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
diff changeset
   666
    }
56730
b08918259eed http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents: 56695
diff changeset
   667
}