test/jdk/java/net/httpclient/HeadTest.java
author chegar
Tue, 19 Jun 2018 09:13:58 +0100
branchhttp-client-branch
changeset 56771 73a6534bce94
parent 56631 30b27fe75b0a
child 52121 934969c63223
permissions -rw-r--r--
http-client-branch: prepare tests for TLS1.3 Contributed-by: michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56631
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
     1
/*
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
     4
 *
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
     8
 *
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    13
 * accompanied this code).
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    14
 *
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    18
 *
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    21
 * questions.
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    22
 */
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    23
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    24
/*
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    25
 * @test
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    26
 * @bug 8203433
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    27
 * @summary (httpclient) Add tests for HEAD and 304 responses.
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    28
 * @modules java.base/sun.net.www.http
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    29
 *          java.net.http/jdk.internal.net.http.common
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    30
 *          java.net.http/jdk.internal.net.http.frame
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.hpack
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    32
 *          java.logging
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    33
 *          jdk.httpserver
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    34
 * @library /lib/testlibrary /test/lib http2/server
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    35
 * @build Http2TestServer
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    36
 * @build jdk.testlibrary.SimpleSSLContext
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    37
 * @run testng/othervm
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    38
 *       -Djdk.httpclient.HttpClient.log=trace,headers,requests
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    39
 *       HeadTest
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    40
 */
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    41
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    42
import com.sun.net.httpserver.HttpServer;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    43
import com.sun.net.httpserver.HttpsConfigurator;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    44
import com.sun.net.httpserver.HttpsServer;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    45
import jdk.testlibrary.SimpleSSLContext;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    46
import org.testng.annotations.AfterTest;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    47
import org.testng.annotations.BeforeTest;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    48
import org.testng.annotations.DataProvider;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    49
import org.testng.annotations.Test;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    50
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    51
import javax.net.ServerSocketFactory;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    52
import javax.net.ssl.SSLContext;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    53
import java.io.IOException;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    54
import java.io.InputStream;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    55
import java.io.OutputStream;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    56
import java.io.OutputStreamWriter;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    57
import java.io.PrintWriter;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    58
import java.io.Writer;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    59
import java.net.InetAddress;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    60
import java.net.InetSocketAddress;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    61
import java.net.ServerSocket;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    62
import java.net.Socket;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    63
import java.net.URI;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    64
import java.net.http.HttpClient;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    65
import java.net.http.HttpClient.Redirect;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    66
import java.net.http.HttpRequest;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    67
import java.net.http.HttpResponse;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    68
import java.net.http.HttpResponse.BodyHandlers;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    69
import java.util.ArrayList;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    70
import java.util.Arrays;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    71
import java.util.Collections;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    72
import java.util.HashMap;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    73
import java.util.List;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    74
import java.util.Locale;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    75
import java.util.Map;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    76
import java.util.StringTokenizer;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    77
import java.util.concurrent.ConcurrentHashMap;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    78
import java.util.concurrent.ConcurrentLinkedQueue;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    79
import java.util.concurrent.atomic.AtomicLong;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    80
import java.util.stream.Collectors;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    81
import java.util.stream.Stream;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    82
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    83
import static java.lang.System.out;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    84
import static java.nio.charset.StandardCharsets.UTF_8;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    85
import static java.net.HttpURLConnection.HTTP_OK;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    86
import static org.testng.Assert.assertEquals;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    87
import static org.testng.Assert.assertTrue;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    88
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    89
public class HeadTest implements HttpServerAdapters {
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    90
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    91
    SSLContext sslContext;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    92
    HttpTestServer httpTestServer;        // HTTP/1.1
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    93
    HttpTestServer httpsTestServer;       // HTTPS/1.1
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    94
    HttpTestServer http2TestServer;       // HTTP/2 ( h2c )
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    95
    HttpTestServer https2TestServer;      // HTTP/2 ( h2  )
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    96
    String httpURI;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    97
    String httpsURI;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    98
    String http2URI;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
    99
    String https2URI;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   100
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   101
    static final String MESSAGE = "Basic HeadTest message body";
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   102
    static final int ITERATIONS = 3;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   103
    static final String CONTENT_LEN = "300";
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   104
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   105
    /*
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   106
     * NOT_MODIFIED status code results from a conditional GET where
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   107
     * the server does not (must not) return a response body because
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   108
     * the condition specified in the request disallows it
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   109
     */
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   110
    static final int HTTP_NOT_MODIFIED = 304;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   111
    static final int HTTP_OK = 200;
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   112
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   113
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   114
    @DataProvider(name = "positive")
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   115
    public Object[][] positive() {
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   116
        return new Object[][] {
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   117
                { httpURI, "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_1_1  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   118
                { httpsURI, "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_1_1  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   119
                { httpURI, "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_2  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   120
                { httpsURI, "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_2  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   121
                { httpURI, "HEAD", HTTP_OK, HttpClient.Version.HTTP_1_1  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   122
                { httpsURI, "HEAD", HTTP_OK, HttpClient.Version.HTTP_1_1  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   123
                { httpURI, "HEAD", HTTP_OK, HttpClient.Version.HTTP_2  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   124
                { httpsURI, "HEAD", HTTP_OK, HttpClient.Version.HTTP_2  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   125
                { httpURI + "transfer/", "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_1_1  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   126
                { httpsURI + "transfer/", "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_1_1  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   127
                { httpURI + "transfer/", "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_2  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   128
                { httpsURI + "transfer/", "GET", HTTP_NOT_MODIFIED, HttpClient.Version.HTTP_2  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   129
                { httpURI + "transfer/", "HEAD", HTTP_OK, HttpClient.Version.HTTP_1_1  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   130
                { httpsURI + "transfer/", "HEAD", HTTP_OK, HttpClient.Version.HTTP_1_1  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   131
                { httpURI + "transfer/", "HEAD", HTTP_OK, HttpClient.Version.HTTP_2  },
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   132
                { httpsURI + "transfer/", "HEAD", HTTP_OK, HttpClient.Version.HTTP_2  }
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   133
        };
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   134
    }
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   135
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   136
    static final AtomicLong requestCounter = new AtomicLong();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   137
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   138
    @Test(dataProvider = "positive")
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   139
    void test(String uriString, String method,
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   140
                        int expResp, HttpClient.Version version) throws Exception {
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   141
        out.printf("%n---- starting (%s) ----%n", uriString);
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   142
        HttpClient client = HttpClient.newBuilder()
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   143
                .followRedirects(Redirect.ALWAYS)
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   144
                .sslContext(sslContext)
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   145
                .build();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   146
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   147
        URI uri = URI.create(uriString);
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   148
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   149
        HttpRequest.Builder requestBuilder = HttpRequest
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   150
                .newBuilder(uri)
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   151
                .method(method, HttpRequest.BodyPublishers.noBody());
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   152
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   153
        if (version != null) {
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   154
            requestBuilder.version(version);
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   155
        }
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   156
        HttpRequest request = requestBuilder.build();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   157
        out.println("Initial request: " + request.uri());
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   158
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   159
        HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   160
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   161
        out.println("  Got response: " + response);
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   162
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   163
        assertEquals(response.statusCode(), expResp);
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   164
        assertEquals(response.body(), "");
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   165
        assertEquals(response.headers().firstValue("Content-length").get(), CONTENT_LEN);
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   166
    }
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   167
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   168
    // -- Infrastructure
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   169
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   170
    @BeforeTest
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   171
    public void setup() throws Exception {
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   172
        sslContext = new SimpleSSLContext().get();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   173
        if (sslContext == null)
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   174
            throw new AssertionError("Unexpected null sslContext");
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   175
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   176
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   177
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   178
        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   179
        httpTestServer.addHandler(new HeadHandler(), "/");
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   180
        httpURI = "http://" + httpTestServer.serverAuthority() + "/";
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   181
        HttpsServer httpsServer = HttpsServer.create(sa, 0);
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   182
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   183
        httpsTestServer = HttpTestServer.of(httpsServer);
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   184
        httpsTestServer.addHandler(new HeadHandler(),"/");
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   185
        httpsURI = "https://" + httpsTestServer.serverAuthority() + "/";
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   186
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   187
        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   188
        http2TestServer.addHandler(new HeadHandler(), "/");
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   189
        http2URI = "http://" + http2TestServer.serverAuthority() + "/";
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   190
        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, 0));
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   191
        https2TestServer.addHandler(new HeadHandler(), "/");
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   192
        https2URI = "https://" + https2TestServer.serverAuthority() + "/";
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   193
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   194
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   195
        httpTestServer.start();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   196
        httpsTestServer.start();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   197
        http2TestServer.start();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   198
        https2TestServer.start();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   199
    }
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   200
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   201
    @AfterTest
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   202
    public void teardown() throws Exception {
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   203
        httpTestServer.stop();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   204
        httpsTestServer.stop();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   205
        http2TestServer.stop();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   206
        https2TestServer.stop();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   207
    }
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   208
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   209
    static class HeadHandler implements HttpTestHandler {
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   210
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   211
        @Override
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   212
        public void handle(HttpTestExchange t) throws IOException {
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   213
            readAllRequestData(t); // shouldn't be any
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   214
            String method = t.getRequestMethod();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   215
            String path = t.getRequestURI().getPath();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   216
            HttpTestResponseHeaders rsph = t.getResponseHeaders();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   217
            if (path.contains("transfer"))
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   218
                rsph.addHeader("Transfer-Encoding", "chunked");
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   219
            rsph.addHeader("Content-length", CONTENT_LEN);
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   220
            if (method.equals("HEAD")) {
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   221
                t.sendResponseHeaders(HTTP_OK, -1);
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   222
            } else if (method.equals("GET")) {
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   223
                t.sendResponseHeaders(HTTP_NOT_MODIFIED, -1);
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   224
            }
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   225
            t.close();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   226
        }
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   227
    }
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   228
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   229
    static void readAllRequestData(HttpTestExchange t) throws IOException {
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   230
        try (InputStream is = t.getRequestBody()) {
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   231
            is.readAllBytes();
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   232
        }
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   233
    }
30b27fe75b0a http-client-branch: 8203433 (httpclient) Add tests for HEAD and 304 responses.
michaelm
parents:
diff changeset
   234
}