jdk/test/java/net/URLConnection/Redirect307Test.java
author amurillo
Wed, 15 Aug 2012 16:49:38 -0700
changeset 13465 d3fc5d192448
parent 10699 c6b5b935cce7
child 14342 8435a30053c1
permissions -rw-r--r--
7191765: make jdk8 the default jprt release for hs24 Reviewed-by: jcoomes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6115
diff changeset
     2
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    26
 * @bug 4380568 7095949
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary  HttpURLConnection does not support 307 redirects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
class RedirServer extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    34
    static final int TIMEOUT = 10 * 1000;
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    35
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    36
    ServerSocket ss;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    39
    String reply1Part1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        "Server: Apache/1.3.14 (Unix)\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        "Location: http://localhost:";
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    43
    String reply1Part2 = "/redirected.html\r\n" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        "Connection: close\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        "<html>Hello</html>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    48
    RedirServer (ServerSocket ss) throws IOException {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    49
        this.ss = ss;
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    50
        this.ss.setSoTimeout(TIMEOUT);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    51
        port = this.ss.getLocalPort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    54
    String reply2 = "HTTP/1.1 200 Ok\r\n" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        "Server: Apache/1.3.14 (Unix)\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        "Connection: close\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        "World";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    61
    static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    62
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    63
    // Read until the end of a HTTP request
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    64
    void readOneRequest(InputStream is) throws IOException {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    65
        int requestEndCount = 0, r;
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    66
        while ((r = is.read()) != -1) {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    67
            if (r == requestEnd[requestEndCount]) {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    68
                requestEndCount++;
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    69
                if (requestEndCount == 4) {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    70
                    break;
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    71
                }
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    72
            } else {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    73
                requestEndCount = 0;
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    74
            }
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    75
        }
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    76
    }
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    77
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        try {
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    80
            try (Socket s = ss.accept()) {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    81
                s.setSoTimeout(TIMEOUT);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    82
                readOneRequest(s.getInputStream());
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    83
                String reply = reply1Part1 + port + reply1Part2;
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    84
                s.getOutputStream().write(reply.getBytes());
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    85
            }
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            /* wait for redirected connection */
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    88
            try (Socket s = ss.accept()) {
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    89
                s.setSoTimeout(TIMEOUT);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    90
                readOneRequest(s.getInputStream());
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    91
                s.getOutputStream().write(reply2.getBytes());
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    92
            }
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    93
        } catch (Exception e) {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    94
            e.printStackTrace();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    95
        } finally {
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
    96
            try { ss.close(); } catch (IOException unused) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
public class Redirect307Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static void main(String[] args) throws Exception {
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
   103
        ServerSocket sock = new ServerSocket(0);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
   104
        int port = sock.getLocalPort();
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
   105
        RedirServer server = new RedirServer(sock);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
   106
        server.start();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
   108
        URL url = new URL("http://localhost:" + port);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
   109
        URLConnection conURL =  url.openConnection();
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
   110
        conURL.setDoInput(true);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
   111
        conURL.setAllowUserInteraction(false);
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
   112
        conURL.setUseCaches(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
10699
c6b5b935cce7 7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
chegar
parents: 7668
diff changeset
   114
        try (InputStream in = conURL.getInputStream()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if ((in.read() != (int)'W') || (in.read()!=(int)'o')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                throw new RuntimeException ("Unexpected string read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}