test/jdk/java/net/httpclient/BasicRedirectTest.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 Basic test for redirect and redirect policies
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
 *          java.logging
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
 *          jdk.httpserver
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    33
 * @library /test/lib http2/server
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
 * @build Http2TestServer
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    35
 * @build jdk.test.lib.net.SimpleSSLContext
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
 * @run testng/othervm
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
 *       -Djdk.httpclient.HttpClient.log=trace,headers,requests
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
 *       BasicRedirectTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import com.sun.net.httpserver.HttpServer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import com.sun.net.httpserver.HttpsConfigurator;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import com.sun.net.httpserver.HttpsServer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
import java.io.InputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
import java.io.OutputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
import java.net.InetAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
import java.net.InetSocketAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
import java.net.URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
import java.net.http.HttpClient.Redirect;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
import java.net.http.HttpResponse.BodyHandlers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
import javax.net.ssl.SSLContext;
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    56
import jdk.test.lib.net.SimpleSSLContext;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
import org.testng.annotations.AfterTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
import org.testng.annotations.BeforeTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
import org.testng.annotations.DataProvider;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
import org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
import static java.lang.System.out;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
import static java.nio.charset.StandardCharsets.UTF_8;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
import static org.testng.Assert.assertEquals;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
import static org.testng.Assert.assertFalse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
import static org.testng.Assert.assertTrue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
public class BasicRedirectTest implements HttpServerAdapters {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
    SSLContext sslContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
    HttpTestServer httpTestServer;        // HTTP/1.1    [ 4 servers ]
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
    HttpTestServer httpsTestServer;       // HTTPS/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
    HttpTestServer http2TestServer;       // HTTP/2 ( h2c )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
    HttpTestServer https2TestServer;      // HTTP/2 ( h2  )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
    String httpURI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
    String httpURIToMoreSecure; // redirects HTTP to HTTPS
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
    String httpsURI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
    String httpsURIToLessSecure; // redirects HTTPS to HTTP
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
    String http2URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
    String http2URIToMoreSecure; // redirects HTTP to HTTPS
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
    String https2URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
    String https2URIToLessSecure; // redirects HTTPS to HTTP
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
    static final String MESSAGE = "Is fearr Gaeilge briste, na Bearla cliste";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
    static final int ITERATIONS = 3;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
    @DataProvider(name = "positive")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
    public Object[][] positive() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
        return new Object[][] {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
                { httpURI,               Redirect.ALWAYS        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
                { httpsURI,              Redirect.ALWAYS        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
                { http2URI,              Redirect.ALWAYS        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
                { https2URI,             Redirect.ALWAYS        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
                { httpURIToMoreSecure,   Redirect.ALWAYS        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
                { http2URIToMoreSecure,  Redirect.ALWAYS        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
                { httpsURIToLessSecure,  Redirect.ALWAYS        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
                { https2URIToLessSecure, Redirect.ALWAYS        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
                { httpURI,               Redirect.NORMAL        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
                { httpsURI,              Redirect.NORMAL        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
                { http2URI,              Redirect.NORMAL        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
                { https2URI,             Redirect.NORMAL        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
                { httpURIToMoreSecure,   Redirect.NORMAL        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
                { http2URIToMoreSecure,  Redirect.NORMAL        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
    @Test(dataProvider = "positive")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
    void test(String uriString, Redirect redirectPolicy) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
        out.printf("%n---- starting positive (%s, %s) ----%n", uriString, redirectPolicy);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
        HttpClient client = HttpClient.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
                .followRedirects(redirectPolicy)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
                .sslContext(sslContext)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
                .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
        URI uri = URI.create(uriString);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
        HttpRequest request = HttpRequest.newBuilder(uri).build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
        out.println("Initial request: " + request.uri());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
        for (int i=0; i< ITERATIONS; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
            out.println("iteration: " + i);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
            HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
            out.println("  Got response: " + response);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
            out.println("  Got body Path: " + response.body());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
            out.println("  Got response.request: " + response.request());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
            assertEquals(response.statusCode(), 200);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
            assertEquals(response.body(), MESSAGE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
            // asserts redirected URI in response.request().uri()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
            assertTrue(response.uri().getPath().endsWith("message"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
            assertPreviousRedirectResponses(request, response);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
    static void assertPreviousRedirectResponses(HttpRequest initialRequest,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
                                                HttpResponse<?> finalResponse) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
        // there must be at least one previous response
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
        finalResponse.previousResponse()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
                .orElseThrow(() -> new RuntimeException("no previous response"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
        HttpResponse<?> response = finalResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
        do {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
            URI uri = response.uri();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
            response = response.previousResponse().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
            assertTrue(300 <= response.statusCode() && response.statusCode() <= 309,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
                       "Expected 300 <= code <= 309, got:" + response.statusCode());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
            assertEquals(response.body(), null, "Unexpected body: " + response.body());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
            String locationHeader = response.headers().firstValue("Location")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
                      .orElseThrow(() -> new RuntimeException("no previous Location"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
            assertTrue(uri.toString().endsWith(locationHeader),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
                      "URI: " + uri + ", Location: " + locationHeader);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
        } while (response.previousResponse().isPresent());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
        // initial
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
        assertEquals(initialRequest, response.request(),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
                String.format("Expected initial request [%s] to equal last prev req [%s]",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
                              initialRequest, response.request()));
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
    // --  negatives
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
    @DataProvider(name = "negative")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
    public Object[][] negative() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
        return new Object[][] {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
                { httpURI,               Redirect.NEVER         },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
                { httpsURI,              Redirect.NEVER         },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
                { http2URI,              Redirect.NEVER         },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
                { https2URI,             Redirect.NEVER         },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
                { httpURIToMoreSecure,   Redirect.NEVER         },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
                { http2URIToMoreSecure,  Redirect.NEVER         },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
                { httpsURIToLessSecure,  Redirect.NEVER         },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
                { https2URIToLessSecure, Redirect.NEVER         },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
                { httpsURIToLessSecure,  Redirect.NORMAL        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
                { https2URIToLessSecure, Redirect.NORMAL        },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
        };
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
    @Test(dataProvider = "negative")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
    void testNegatives(String uriString,Redirect redirectPolicy) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
        out.printf("%n---- starting negative (%s, %s) ----%n", uriString, redirectPolicy);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
        HttpClient client = HttpClient.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
                .followRedirects(redirectPolicy)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
                .sslContext(sslContext)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
                .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
        URI uri = URI.create(uriString);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
        HttpRequest request = HttpRequest.newBuilder(uri).build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
        out.println("Initial request: " + request.uri());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
        for (int i=0; i< ITERATIONS; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
            out.println("iteration: " + i);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
            HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
            out.println("  Got response: " + response);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
            out.println("  Got body Path: " + response.body());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
            out.println("  Got response.request: " + response.request());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
            assertEquals(response.statusCode(), 302);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
            assertEquals(response.body(), "XY");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
            // asserts original URI in response.request().uri()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
            assertTrue(response.uri().equals(uri));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
            assertFalse(response.previousResponse().isPresent());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
    // -- Infrastructure
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
    @BeforeTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
    public void setup() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
        sslContext = new SimpleSSLContext().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
        if (sslContext == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
            throw new AssertionError("Unexpected null sslContext");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
        httpTestServer.addHandler(new BasicHttpRedirectHandler(), "/http1/same/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
        httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/same/redirect";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
        HttpsServer httpsServer = HttpsServer.create(sa, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
        httpsTestServer = HttpTestServer.of(httpsServer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
        httpsTestServer.addHandler(new BasicHttpRedirectHandler(),"/https1/same/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   226
        httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/same/redirect";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   227
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   228
        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   229
        http2TestServer.addHandler(new BasicHttpRedirectHandler(), "/http2/same/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   230
        http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/same/redirect";
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   231
        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
        https2TestServer.addHandler(new BasicHttpRedirectHandler(), "/https2/same/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
        https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/same/redirect";
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
        // HTTP to HTTPS redirect handler
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
        httpTestServer.addHandler(new ToSecureHttpRedirectHandler(httpsURI), "/http1/toSecure/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
        httpURIToMoreSecure = "http://" + httpTestServer.serverAuthority()+ "/http1/toSecure/redirect";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
        // HTTP2 to HTTP2S redirect handler
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   240
        http2TestServer.addHandler(new ToSecureHttpRedirectHandler(https2URI), "/http2/toSecure/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   241
        http2URIToMoreSecure = "http://" + http2TestServer.serverAuthority() + "/http2/toSecure/redirect";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
        // HTTPS to HTTP redirect handler
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
        httpsTestServer.addHandler(new ToLessSecureRedirectHandler(httpURI), "/https1/toLessSecure/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
        httpsURIToLessSecure = "https://" + httpsTestServer.serverAuthority() + "/https1/toLessSecure/redirect";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
        // HTTPS2 to HTTP2 redirect handler
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
        https2TestServer.addHandler(new ToLessSecureRedirectHandler(http2URI), "/https2/toLessSecure/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
        https2URIToLessSecure = "https://" + https2TestServer.serverAuthority() + "/https2/toLessSecure/redirect";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
        httpTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
        httpsTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
        http2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   253
        https2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   254
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   255
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
    @AfterTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
    public void teardown() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
        httpTestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
        httpsTestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
        http2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
        https2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
    // Redirects to same protocol
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
    static class BasicHttpRedirectHandler implements HttpTestHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   266
        // flip-flop between chunked/variable and fixed length redirect responses
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
        volatile int count;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   269
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   270
        public void handle(HttpTestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   271
            System.out.println("BasicHttpRedirectHandler for: " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   272
            readAllRequestData(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   273
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   274
            if (t.getRequestURI().getPath().endsWith("redirect")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   275
                String url = t.getRequestURI().resolve("message").toString();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   276
                t.getResponseHeaders().addHeader("Location", url);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   277
                int len = count % 2 == 0 ? 2 : -1;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   278
                t.sendResponseHeaders(302, len);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   279
                try (OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   280
                    os.write(new byte[]{'X', 'Y'});  // stuffing some response body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   281
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
                try (OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   284
                    byte[] bytes = MESSAGE.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   285
                    t.sendResponseHeaders(200, bytes.length);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   286
                    os.write(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   287
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   288
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   289
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   290
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   291
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   292
    // Redirects to a, possibly, more secure protocol, (HTTP to HTTPS)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   293
    static class ToSecureHttpRedirectHandler implements HttpTestHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   294
        final String targetURL;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   295
        ToSecureHttpRedirectHandler(String targetURL) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   296
            this.targetURL = targetURL;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   297
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   298
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   299
        public void handle(HttpTestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   300
            System.out.println("ToSecureHttpRedirectHandler for: " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   301
            readAllRequestData(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   302
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   303
            if (t.getRequestURI().getPath().endsWith("redirect")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   304
                t.getResponseHeaders().addHeader("Location", targetURL);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   305
                System.out.println("ToSecureHttpRedirectHandler redirecting to: " + targetURL);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   306
                t.sendResponseHeaders(302, 2); // fixed-length
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   307
                try (OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   308
                    os.write(new byte[]{'X', 'Y'});
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   309
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   310
            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   311
                Throwable ex = new RuntimeException("Unexpected request");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   312
                ex.printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   313
                t.sendResponseHeaders(500, 0);
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
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   317
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   318
    // Redirects to a, possibly, less secure protocol (HTTPS to HTTP)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   319
    static class ToLessSecureRedirectHandler implements HttpTestHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   320
        final String targetURL;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   321
        ToLessSecureRedirectHandler(String targetURL) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   322
            this.targetURL = targetURL;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   323
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   324
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   325
        public void handle(HttpTestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   326
            System.out.println("ToLessSecureRedirectHandler for: " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   327
            readAllRequestData(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   328
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   329
            if (t.getRequestURI().getPath().endsWith("redirect")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   330
                t.getResponseHeaders().addHeader("Location", targetURL);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   331
                System.out.println("ToLessSecureRedirectHandler redirecting to: " + targetURL);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   332
                t.sendResponseHeaders(302, -1);  // chunked/variable
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   333
                try (OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   334
                    os.write(new byte[]{'X', 'Y'});
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   335
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   336
            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   337
                Throwable ex = new RuntimeException("Unexpected request");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   338
                ex.printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   339
                t.sendResponseHeaders(500, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   340
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   341
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   342
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   343
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   344
    static void readAllRequestData(HttpTestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   345
        try (InputStream is = t.getRequestBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   346
            is.readAllBytes();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   347
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   348
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   349
}