jdk/test/java/net/URLConnection/Redirect307Test.java
author chegar
Wed, 21 Jul 2010 13:29:26 +0100
changeset 6115 7c523cf2bc8a
parent 5506 202f599c92aa
child 7668 d4a77089c587
permissions -rw-r--r--
6969395: TEST_BUG: Tests in java/net sun/net problems Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4380568
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    ServerSocket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    Socket   s1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    InputStream  is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    OutputStream os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    40
    String reply1Part1 = "HTTP/1.1 307 Temporary Redirect\r\n" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        "Server: Apache/1.3.14 (Unix)\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        "Location: http://localhost:";
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    44
    String reply1Part2 = "/redirected.html\r\n" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        "Connection: close\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        "<html>Hello</html>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    RedirServer (ServerSocket y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        s = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        port = s.getLocalPort();
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    52
        System.out.println("Server created listening on " + port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    55
    String reply2 = "HTTP/1.1 200 Ok\r\n" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        "Server: Apache/1.3.14 (Unix)\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        "Connection: close\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        "Content-Type: text/html; charset=iso-8859-1\r\n\r\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        "World";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            s1 = s.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            is = s1.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            os = s1.getOutputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            is.read ();
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    68
            String reply = reply1Part1 + port + reply1Part2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            os.write (reply.getBytes());
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    70
            os.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            /* wait for redirected connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            s.setSoTimeout (5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            s1 = s.accept ();
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    74
            is = s1.getInputStream ();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            os = s1.getOutputStream ();
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    76
            is.read();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    77
            os.write (reply2.getBytes());
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    78
            os.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            /* Just need thread to terminate */
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    82
            System.out.println("Server: caught " + e);
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    83
            e.printStackTrace();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    84
        } finally {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    85
            try { s.close(); } catch (IOException unused) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
public class Redirect307Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static final int DELAY = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static void main(String[] args) throws Exception {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    96
        int port;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        RedirServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        ServerSocket sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            sock = new ServerSocket (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            port = sock.getLocalPort ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            System.out.println ("Exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        server = new RedirServer(sock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        server.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        try  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            String s = "http://localhost:" + port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            URL url = new URL(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            URLConnection conURL =  url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            conURL.setDoInput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            conURL.setAllowUserInteraction(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            conURL.setUseCaches(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            InputStream in = conURL.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if ((in.read() != (int)'W') || (in.read()!=(int)'o')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                throw new RuntimeException ("Unexpected string read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        catch(IOException e) {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   128
            e.printStackTrace();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   129
            throw new RuntimeException ("Exception caught + " + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}