test/jdk/java/net/httpclient/RedirectMethodChange.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 52121 934969c63223
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     4
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     8
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    13
 * accompanied this code).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    14
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    18
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    21
 * questions.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    22
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    23
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    24
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    25
 * @test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    26
 * @summary Method change during redirection
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
 * @modules java.base/sun.net.www.http
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
 *          java.net.http/jdk.internal.net.http.common
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
 *          java.net.http/jdk.internal.net.http.frame
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
 *          java.net.http/jdk.internal.net.http.hpack
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
 *          jdk.httpserver
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    32
 * @library /test/lib http2/server
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
 * @build Http2TestServer
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    34
 * @build jdk.test.lib.net.SimpleSSLContext
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
 * @run testng/othervm RedirectMethodChange
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
import javax.net.ssl.SSLContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import java.io.InputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import java.io.OutputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import java.net.InetAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import java.net.InetSocketAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
import java.net.URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
import java.net.http.HttpRequest.BodyPublishers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
import java.net.http.HttpResponse.BodyHandlers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
import com.sun.net.httpserver.HttpServer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
import com.sun.net.httpserver.HttpsConfigurator;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
import com.sun.net.httpserver.HttpsServer;
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    53
import jdk.test.lib.net.SimpleSSLContext;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
import org.testng.annotations.AfterTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
import org.testng.annotations.BeforeTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
import org.testng.annotations.DataProvider;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
import org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
import static java.nio.charset.StandardCharsets.US_ASCII;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
import static org.testng.Assert.assertEquals;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
public class RedirectMethodChange implements HttpServerAdapters {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
    SSLContext sslContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
    HttpClient client;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
    HttpTestServer httpTestServer;        // HTTP/1.1    [ 4 servers ]
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
    HttpTestServer httpsTestServer;       // HTTPS/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
    HttpTestServer http2TestServer;       // HTTP/2 ( h2c )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
    HttpTestServer https2TestServer;      // HTTP/2 ( h2  )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
    String httpURI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
    String httpsURI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
    String http2URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
    String https2URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
    static final String RESPONSE = "Hello world";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
    static final String POST_BODY = "This is the POST body 123909090909090";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
    static HttpRequest.BodyPublisher getRequestBodyFor(String method) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
        switch (method) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
            case "GET":
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
            case "DELETE":
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
            case "HEAD":
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
                return BodyPublishers.noBody();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
            case "POST":
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
            case "PUT":
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
                return BodyPublishers.ofString(POST_BODY);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
            default:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
                throw new AssertionError("Unknown method:" + method);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
    @DataProvider(name = "variants")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
    public Object[][] variants() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
        return new Object[][] {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
                { httpURI, "GET",  301, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
                { httpURI, "GET",  302, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
                { httpURI, "GET",  303, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
                { httpURI, "GET",  307, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
                { httpURI, "GET",  308, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
                { httpURI, "POST", 301, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
                { httpURI, "POST", 302, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
                { httpURI, "POST", 303, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
                { httpURI, "POST", 307, "POST" },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
                { httpURI, "POST", 308, "POST" },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
                { httpURI, "PUT",  301, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
                { httpURI, "PUT",  302, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
                { httpURI, "PUT",  303, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
                { httpURI, "PUT",  307, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
                { httpURI, "PUT",  308, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
                { httpsURI, "GET",  301, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
                { httpsURI, "GET",  302, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
                { httpsURI, "GET",  303, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
                { httpsURI, "GET",  307, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
                { httpsURI, "GET",  308, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
                { httpsURI, "POST", 301, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
                { httpsURI, "POST", 302, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
                { httpsURI, "POST", 303, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
                { httpsURI, "POST", 307, "POST" },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
                { httpsURI, "POST", 308, "POST" },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
                { httpsURI, "PUT",  301, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
                { httpsURI, "PUT",  302, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
                { httpsURI, "PUT",  303, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
                { httpsURI, "PUT",  307, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
                { httpsURI, "PUT",  308, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
                { http2URI, "GET",  301, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
                { http2URI, "GET",  302, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
                { http2URI, "GET",  303, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
                { http2URI, "GET",  307, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
                { http2URI, "GET",  308, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
                { http2URI, "POST", 301, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
                { http2URI, "POST", 302, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
                { http2URI, "POST", 303, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
                { http2URI, "POST", 307, "POST" },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
                { http2URI, "POST", 308, "POST" },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
                { http2URI, "PUT",  301, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
                { http2URI, "PUT",  302, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
                { http2URI, "PUT",  303, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
                { http2URI, "PUT",  307, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
                { http2URI, "PUT",  308, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
                { https2URI, "GET",  301, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
                { https2URI, "GET",  302, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
                { https2URI, "GET",  303, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
                { https2URI, "GET",  307, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
                { https2URI, "GET",  308, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
                { https2URI, "POST", 301, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
                { https2URI, "POST", 302, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
                { https2URI, "POST", 303, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
                { https2URI, "POST", 307, "POST" },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
                { https2URI, "POST", 308, "POST" },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
                { https2URI, "PUT",  301, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
                { https2URI, "PUT",  302, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
                { https2URI, "PUT",  303, "GET"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
                { https2URI, "PUT",  307, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
                { https2URI, "PUT",  308, "PUT"  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
    @Test(dataProvider = "variants")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
    public void test(String uriString,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
                     String method,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
                     int redirectCode,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
                     String expectedMethod)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
        throws Exception
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
    {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
        HttpRequest req = HttpRequest.newBuilder(URI.create(uriString))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
                .method(method, getRequestBodyFor(method))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
                .header("X-Redirect-Code", Integer.toString(redirectCode))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
                .header("X-Expect-Method", expectedMethod)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
                .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
        HttpResponse<String> resp = client.send(req, BodyHandlers.ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
        System.out.println("Response: " + resp + ", body: " + resp.body());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
        assertEquals(resp.statusCode(), 200);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
        assertEquals(resp.body(), RESPONSE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
    // -- Infrastructure
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
    @BeforeTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
    public void setup() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
        sslContext = new SimpleSSLContext().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
        if (sslContext == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
            throw new AssertionError("Unexpected null sslContext");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
        client = HttpClient.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
                .followRedirects(HttpClient.Redirect.NORMAL)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
                .sslContext(sslContext)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
                .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
        String targetURI = "http://" + httpTestServer.serverAuthority() + "/http1/redirect/rmt";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
        RedirMethodChgeHandler handler = new RedirMethodChgeHandler(targetURI);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
        httpTestServer.addHandler(handler, "/http1/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
        httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/test/rmt";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
        HttpsServer httpsServer = HttpsServer.create(sa, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
        httpsTestServer = HttpTestServer.of(httpsServer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
        targetURI = "https://" + httpsTestServer.serverAuthority() + "/https1/redirect/rmt";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
        handler = new RedirMethodChgeHandler(targetURI);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
        httpsTestServer.addHandler(handler,"/https1/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
        httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/test/rmt";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
        targetURI = "http://" + http2TestServer.serverAuthority() + "/http2/redirect/rmt";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
        handler = new RedirMethodChgeHandler(targetURI);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
        http2TestServer.addHandler(handler, "/http2/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
        http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/test/rmt";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   215
        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
        targetURI = "https://" + https2TestServer.serverAuthority() + "/https2/redirect/rmt";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
        handler = new RedirMethodChgeHandler(targetURI);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
        https2TestServer.addHandler(handler, "/https2/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
        https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/test/rmt";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
        httpTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
        httpsTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
        http2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
        https2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   226
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   227
    @AfterTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   228
    public void teardown() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   229
        httpTestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   230
        httpsTestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   231
        http2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
        https2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   234
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   235
    /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   236
     * Stateful handler.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
     * Request to "<protocol>/test/rmt" is first, with the following checked
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
     * headers:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   240
     *   X-Redirect-Code: nnn    <the redirect code to send back>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   241
     *   X-Expect-Method: the method that the client should use for the next request
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
     * The following request should be to "<protocol>/redirect/rmt" and should
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
     * use the method indicated previously. If all ok, return a 200 response.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
     * Otherwise 50X error.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
    static class RedirMethodChgeHandler implements HttpTestHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
        boolean inTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
        String expectedMethod;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
        final String targetURL;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   253
        RedirMethodChgeHandler(String targetURL) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   254
            this.targetURL = targetURL;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   255
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
        boolean readAndCheckBody(HttpTestExchange e) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
            String method = e.getRequestMethod();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
            String requestBody;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
            try (InputStream is = e.getRequestBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
                requestBody = new String(is.readAllBytes(), US_ASCII);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
            if ((method.equals("POST") || method.equals("PUT"))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
                    && !requestBody.equals(POST_BODY)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
                Throwable ex = new RuntimeException("Unexpected request body for "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   266
                        + method + ": [" + requestBody +"]");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
                ex.printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
                e.sendResponseHeaders(503, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   269
                return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   270
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   271
            return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   272
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   273
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   274
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   275
        public synchronized void handle(HttpTestExchange he) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   276
            boolean newtest = he.getRequestURI().getPath().endsWith("/test/rmt");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   277
            if ((newtest && inTest) || (!newtest && !inTest)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   278
                Throwable ex = new RuntimeException("Unexpected newtest:" + newtest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   279
                        + ", inTest:" + inTest +  ", for " + he.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   280
                ex.printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   281
                he.sendResponseHeaders(500, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
                return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   284
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   285
            if (newtest) {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   286
                HttpTestRequestHeaders hdrs = he.getRequestHeaders();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   287
                String value = hdrs.firstValue("X-Redirect-Code").get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   288
                int redirectCode = Integer.parseInt(value);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   289
                expectedMethod = hdrs.firstValue("X-Expect-Method").get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   290
                if (!readAndCheckBody(he))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   291
                    return;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   292
                HttpTestResponseHeaders headersbuilder = he.getResponseHeaders();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   293
                headersbuilder.addHeader("Location", targetURL);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   294
                he.sendResponseHeaders(redirectCode, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   295
                inTest = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   296
            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   297
                // should be the redirect
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   298
                if (!he.getRequestURI().getPath().endsWith("/redirect/rmt")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   299
                    Throwable ex = new RuntimeException("Unexpected redirected request, got:"
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   300
                            + he.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   301
                    ex.printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   302
                    he.sendResponseHeaders(501, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   303
                } else if (!he.getRequestMethod().equals(expectedMethod)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   304
                    Throwable ex = new RuntimeException("Expected: " + expectedMethod
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   305
                            + " Got: " + he.getRequestMethod());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   306
                    ex.printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   307
                    he.sendResponseHeaders(504, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   308
                } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   309
                    if (!readAndCheckBody(he))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   310
                        return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   311
                    he.sendResponseHeaders(200, RESPONSE.length());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   312
                    try (OutputStream os = he.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   313
                        os.write(RESPONSE.getBytes(US_ASCII));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   314
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   315
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   316
                inTest = false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   317
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   318
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   319
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   320
}