test/jdk/java/net/httpclient/RetryWithCookie.java
author chegar
Wed, 02 May 2018 15:47:57 +0100
branchhttp-client-branch
changeset 56507 2294c51eae30
parent 56487 6fc81d0648aa
parent 49944 4690a2871b44
child 56771 73a6534bce94
permissions -rw-r--r--
http-client-branch: Merge with default
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
 * @bug 8199943
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
 * @summary Test for cookie handling when retrying after close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
 * @modules java.base/sun.net.www.http
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
 *          java.net.http/jdk.internal.net.http.common
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
 *          java.net.http/jdk.internal.net.http.frame
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.hpack
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
 *          java.logging
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
 *          jdk.httpserver
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
 * @library /lib/testlibrary /test/lib http2/server
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
 * @build Http2TestServer
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
 * @build jdk.testlibrary.SimpleSSLContext ReferenceTracker
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
 * @run testng/othervm
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
 *       -Djdk.httpclient.HttpClient.log=trace,headers,requests
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
 *       RetryWithCookie
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import com.sun.net.httpserver.HttpServer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import com.sun.net.httpserver.HttpsConfigurator;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
import com.sun.net.httpserver.HttpsServer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
import jdk.testlibrary.SimpleSSLContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
import org.testng.annotations.AfterTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
import org.testng.annotations.BeforeTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
import org.testng.annotations.DataProvider;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
import org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
import javax.net.ssl.SSLContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
import java.io.InputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
import java.io.OutputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
import java.net.CookieManager;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
import java.net.InetAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
import java.net.InetSocketAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
import java.net.URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
import java.net.http.HttpClient.Redirect;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
import java.net.http.HttpResponse.BodyHandlers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
import java.util.ArrayList;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
import java.util.HashMap;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
import java.util.Map;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
import java.util.concurrent.ConcurrentHashMap;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
import java.util.concurrent.atomic.AtomicLong;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
import static java.lang.System.out;
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
    72
import static java.net.http.HttpClient.Builder.NO_PROXY;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
import static java.nio.charset.StandardCharsets.UTF_8;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
import static org.testng.Assert.assertEquals;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
import static org.testng.Assert.assertTrue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
public class RetryWithCookie implements HttpServerAdapters {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
    SSLContext sslContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
    HttpTestServer httpTestServer;        // HTTP/1.1    [ 4 servers ]
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
    HttpTestServer httpsTestServer;       // HTTPS/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
    HttpTestServer http2TestServer;       // HTTP/2 ( h2c )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
    HttpTestServer https2TestServer;      // HTTP/2 ( h2  )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
    String httpURI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
    String httpsURI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
    String http2URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
    String https2URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
    static final String MESSAGE = "BasicRedirectTest message body";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
    static final int ITERATIONS = 3;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
    @DataProvider(name = "positive")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
    public Object[][] positive() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
        return new Object[][] {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
                { httpURI,    },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
                { httpsURI,   },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
                { http2URI,   },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
                { https2URI,  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
    static final AtomicLong requestCounter = new AtomicLong();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
    final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
    @Test(dataProvider = "positive")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
    void test(String uriString) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
        out.printf("%n---- starting (%s) ----%n", uriString);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
        CookieManager cookieManager = new CookieManager();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
        HttpClient client = HttpClient.newBuilder()
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   110
                .proxy(NO_PROXY)
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
                .followRedirects(Redirect.ALWAYS)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
                .cookieHandler(cookieManager)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
                .sslContext(sslContext)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
                .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
        TRACKER.track(client);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
        assert client.cookieHandler().isPresent();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
        URI uri = URI.create(uriString);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
        List<String> cookies = new ArrayList<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
        cookies.add("CUSTOMER=ARTHUR_DENT");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
        Map<String, List<String>> cookieHeaders = new HashMap<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
        cookieHeaders.put("Set-Cookie", cookies);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
        cookieManager.put(uri, cookieHeaders);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
        HttpRequest request = HttpRequest.newBuilder(uri)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
                .header("X-uuid", "uuid-" + requestCounter.incrementAndGet())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
                .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
        out.println("Initial request: " + request.uri());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
        for (int i=0; i< ITERATIONS; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
            out.println("iteration: " + i);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
            HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
            out.println("  Got response: " + response);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
            out.println("  Got body Path: " + response.body());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
            assertEquals(response.statusCode(), 200);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
            assertEquals(response.body(), MESSAGE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
            assertEquals(response.headers().allValues("X-Request-Cookie"), cookies);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
            request = HttpRequest.newBuilder(uri)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
                    .header("X-uuid", "uuid-" + requestCounter.incrementAndGet())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
                    .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
    // -- Infrastructure
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
    @BeforeTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
    public void setup() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
        sslContext = new SimpleSSLContext().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
        if (sslContext == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
            throw new AssertionError("Unexpected null sslContext");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
        httpTestServer.addHandler(new CookieRetryHandler(), "/http1/cookie/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
        httpURI = "http://" + httpTestServer.serverAuthority() + "/http1/cookie/retry";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
        HttpsServer httpsServer = HttpsServer.create(sa, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
        httpsTestServer = HttpTestServer.of(httpsServer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
        httpsTestServer.addHandler(new CookieRetryHandler(),"/https1/cookie/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
        httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1/cookie/retry";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
        http2TestServer.addHandler(new CookieRetryHandler(), "/http2/cookie/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
        http2URI = "http://" + http2TestServer.serverAuthority() + "/http2/cookie/retry";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
        https2TestServer.addHandler(new CookieRetryHandler(), "/https2/cookie/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
        https2URI = "https://" + https2TestServer.serverAuthority() + "/https2/cookie/retry";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
        httpTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
        httpsTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
        http2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
        https2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
    @AfterTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
    public void teardown() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
        Thread.sleep(100);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
        AssertionError fail = TRACKER.check(500);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
            httpTestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
            httpsTestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
            http2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
            https2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
        } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
            if (fail != null) throw fail;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
    static class CookieRetryHandler implements HttpTestHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
        ConcurrentHashMap<String,String> closedRequests = new ConcurrentHashMap<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
        public void handle(HttpTestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
            System.out.println("CookieRetryHandler for: " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
            List<String> uuids = t.getRequestHeaders().get("X-uuid");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
            if (uuids == null || uuids.size() != 1) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
                readAllRequestData(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
                try (OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
                    String msg = "Incorrect uuid header values:[" + uuids + "]";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
                    (new RuntimeException(msg)).printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
                    t.sendResponseHeaders(500, -1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
                    os.write(msg.getBytes(UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
                return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
            String uuid = uuids.get(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
            // retrying
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
            if (closedRequests.putIfAbsent(uuid, t.getRequestURI().toString()) == null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
                if (t.getExchangeVersion() == HttpClient.Version.HTTP_1_1) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
                    // Throwing an exception here only causes a retry
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
                    // with HTTP_1_1 - where it forces the server to close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
                    // the connection.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
                    // For HTTP/2 then throwing an IOE would cause the server
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
                    // to close the stream, and throwing anything else would
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
                    // cause it to close the connection, but neither would
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
                    // cause the client to retry.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
                    // So we simply do not try to retry with HTTP/2 and just verify
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
                    // we have received the expected cookie
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
                    throw new IOException("Closing on first request");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   226
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   227
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   228
            // not retrying
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   229
            readAllRequestData(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   230
            try (OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   231
                List<String> cookie = t.getRequestHeaders().get("Cookie");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
                if (cookie == null || cookie.size() == 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   234
                    String msg = "No cookie header present";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   235
                    (new RuntimeException(msg)).printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   236
                    t.sendResponseHeaders(500, -1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
                    os.write(msg.getBytes(UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
                } else if (!cookie.get(0).equals("CUSTOMER=ARTHUR_DENT")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
                    String msg = "Incorrect cookie header value:[" + cookie.get(0) + "]";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   240
                    (new RuntimeException(msg)).printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   241
                    t.sendResponseHeaders(500, -1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
                    os.write(msg.getBytes(UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
                } else if (cookie.size() > 1) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
                    String msg = "Incorrect cookie header values:[" + cookie + "]";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
                    (new RuntimeException(msg)).printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
                    t.sendResponseHeaders(500, -1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
                    os.write(msg.getBytes(UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
                } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
                    assert cookie.get(0).equals("CUSTOMER=ARTHUR_DENT");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
                    byte[] bytes = MESSAGE.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
                    for (String value : cookie) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
                        t.getResponseHeaders().addHeader("X-Request-Cookie", value);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   253
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   254
                    t.sendResponseHeaders(200, bytes.length);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   255
                    os.write(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
            closedRequests.remove(uuid);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
    static void readAllRequestData(HttpTestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
        try (InputStream is = t.getRequestBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
            is.readAllBytes();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   266
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
}