test/jdk/java/net/HttpURLConnection/HttpURLProxySelectionTest.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57888 41b68dc5e0b9
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57879
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
     1
/*
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
     4
 *
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
     8
 *
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    13
 * accompanied this code).
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    14
 *
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    18
 *
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    21
 * questions.
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    22
 */
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    23
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    24
import com.sun.net.httpserver.HttpExchange;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    25
import com.sun.net.httpserver.HttpHandler;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    26
import com.sun.net.httpserver.HttpServer;
57888
41b68dc5e0b9 8230220: java/net/HttpURLConnection/HttpURLProxySelectionTest.java fails intermittently
jpai
parents: 57879
diff changeset
    27
import jdk.test.lib.net.URIBuilder;
57879
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    28
import org.testng.Assert;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    29
import org.testng.annotations.AfterTest;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    30
import org.testng.annotations.BeforeTest;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    31
import org.testng.annotations.Test;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    32
import sun.net.spi.DefaultProxySelector;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    33
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    34
import java.io.IOException;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    35
import java.net.HttpURLConnection;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    36
import java.net.InetAddress;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    37
import java.net.InetSocketAddress;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    38
import java.net.Proxy;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    39
import java.net.ProxySelector;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    40
import java.net.URI;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    41
import java.net.URISyntaxException;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    42
import java.net.URL;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    43
import java.util.List;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    44
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    45
/**
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    46
 * @test
57888
41b68dc5e0b9 8230220: java/net/HttpURLConnection/HttpURLProxySelectionTest.java fails intermittently
jpai
parents: 57879
diff changeset
    47
 * @bug 6563286 6797318 8177648 8230220
57879
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    48
 * @summary Tests that sun.net.www.protocol.http.HttpURLConnection when dealing with
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    49
 * sun.net.spi.DefaultProxySelector#select() handles any IllegalArgumentException
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    50
 * correctly
57888
41b68dc5e0b9 8230220: java/net/HttpURLConnection/HttpURLProxySelectionTest.java fails intermittently
jpai
parents: 57879
diff changeset
    51
 * @library /test/lib
57879
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    52
 * @run testng HttpURLProxySelectionTest
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    53
 * @modules java.base/sun.net.spi:+open
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    54
 */
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    55
public class HttpURLProxySelectionTest {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    56
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    57
    private static final String WEB_APP_CONTEXT = "/httpurlproxytest";
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    58
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    59
    private HttpServer server;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    60
    private SimpleHandler handler;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    61
    private ProxySelector previousDefault;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    62
    private CustomProxySelector ourProxySelector = new CustomProxySelector();
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    63
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    64
    @BeforeTest
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    65
    public void beforeTest() throws Exception {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    66
        previousDefault = ProxySelector.getDefault();
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    67
        ProxySelector.setDefault(ourProxySelector);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    68
        handler = new SimpleHandler();
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    69
        server = createServer(handler);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    70
    }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    71
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    72
    @AfterTest
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    73
    public void afterTest() {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    74
        try {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    75
            if (server != null) {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    76
                final int delaySeconds = 0;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    77
                server.stop(delaySeconds);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    78
            }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    79
        } finally {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    80
            ProxySelector.setDefault(previousDefault);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    81
        }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    82
    }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    83
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    84
    /**
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    85
     * - Test initiates a HTTP request to server
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    86
     * - Server receives request and sends a 301 redirect to an URI which doesn't have a "host"
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    87
     * - Redirect is expected to fail with IOException (caused by IllegalArgumentException from DefaultProxySelector)
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    88
     *
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    89
     * @throws Exception
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    90
     */
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    91
    @Test
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    92
    public void test() throws Exception {
57888
41b68dc5e0b9 8230220: java/net/HttpURLConnection/HttpURLProxySelectionTest.java fails intermittently
jpai
parents: 57879
diff changeset
    93
        final URL targetURL = URIBuilder.newBuilder()
41b68dc5e0b9 8230220: java/net/HttpURLConnection/HttpURLProxySelectionTest.java fails intermittently
jpai
parents: 57879
diff changeset
    94
                .scheme("http")
41b68dc5e0b9 8230220: java/net/HttpURLConnection/HttpURLProxySelectionTest.java fails intermittently
jpai
parents: 57879
diff changeset
    95
                .host(server.getAddress().getAddress())
41b68dc5e0b9 8230220: java/net/HttpURLConnection/HttpURLProxySelectionTest.java fails intermittently
jpai
parents: 57879
diff changeset
    96
                .port(server.getAddress().getPort())
41b68dc5e0b9 8230220: java/net/HttpURLConnection/HttpURLProxySelectionTest.java fails intermittently
jpai
parents: 57879
diff changeset
    97
                .path(WEB_APP_CONTEXT)
41b68dc5e0b9 8230220: java/net/HttpURLConnection/HttpURLProxySelectionTest.java fails intermittently
jpai
parents: 57879
diff changeset
    98
                .toURL();
57879
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
    99
        System.out.println("Sending request to " + targetURL);
57888
41b68dc5e0b9 8230220: java/net/HttpURLConnection/HttpURLProxySelectionTest.java fails intermittently
jpai
parents: 57879
diff changeset
   100
        final HttpURLConnection conn = (HttpURLConnection) targetURL.openConnection();
57879
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   101
        try {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   102
            conn.getResponseCode();
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   103
            Assert.fail("Request to " + targetURL + " was expected to fail during redirect");
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   104
        } catch (IOException ioe) {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   105
            // expected because of the redirect to an invalid URL, for which a proxy can't be selected
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   106
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   107
            // make sure the it was indeed a redirect
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   108
            Assert.assertTrue(handler.redirectSent, "Server was expected to send a redirect, but didn't");
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   109
            Assert.assertTrue(ourProxySelector.selectorUsedForRedirect, "Proxy selector wasn't used for redirect");
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   110
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   111
            // make sure the IOException was caused by an IllegalArgumentException
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   112
            Assert.assertTrue(ioe.getCause() instanceof IllegalArgumentException, "Unexpected cause in the IOException");
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   113
        }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   114
    }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   115
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   116
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   117
    private static HttpServer createServer(final HttpHandler handler) throws IOException {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   118
        final InetSocketAddress serverAddr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   119
        final int backlog = -1;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   120
        final HttpServer server = HttpServer.create(serverAddr, backlog);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   121
        // setup the handler
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   122
        server.createContext(WEB_APP_CONTEXT, handler);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   123
        // start the server
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   124
        server.start();
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   125
        System.out.println("Server started on " + server.getAddress());
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   126
        return server;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   127
    }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   128
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   129
    private static class SimpleHandler implements HttpHandler {
57888
41b68dc5e0b9 8230220: java/net/HttpURLConnection/HttpURLProxySelectionTest.java fails intermittently
jpai
parents: 57879
diff changeset
   130
        private volatile boolean redirectSent = false;
57879
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   131
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   132
        @Override
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   133
        public void handle(final HttpExchange httpExchange) throws IOException {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   134
            final String redirectURL;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   135
            try {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   136
                redirectURL = new URI("http", "/irrelevant", null).toString();
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   137
            } catch (URISyntaxException e) {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   138
                throw new IOException(e);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   139
            }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   140
            httpExchange.getResponseHeaders().add("Location", redirectURL);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   141
            final URI requestURI = httpExchange.getRequestURI();
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   142
            System.out.println("Handling " + httpExchange.getRequestMethod() + " request "
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   143
                    + requestURI + " responding with redirect to " + redirectURL);
57888
41b68dc5e0b9 8230220: java/net/HttpURLConnection/HttpURLProxySelectionTest.java fails intermittently
jpai
parents: 57879
diff changeset
   144
            this.redirectSent = true;
57879
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   145
            httpExchange.sendResponseHeaders(301, -1);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   146
        }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   147
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   148
    }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   149
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   150
    private static class CustomProxySelector extends DefaultProxySelector {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   151
57888
41b68dc5e0b9 8230220: java/net/HttpURLConnection/HttpURLProxySelectionTest.java fails intermittently
jpai
parents: 57879
diff changeset
   152
        private volatile boolean selectorUsedForRedirect = false;
57879
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   153
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   154
        @Override
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   155
        public List<Proxy> select(final URI uri) {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   156
            if (uri.toString().contains("/irrelevant")) {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   157
                this.selectorUsedForRedirect = true;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   158
            }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   159
            return super.select(uri);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   160
        }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   161
    }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents:
diff changeset
   162
}