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