jdk/test/java/net/URLConnection/Responses.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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 4635698
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check that HttpURLConnection.getResponseCode returns -1 for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          malformed status-lines in the http response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class Responses {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
     * Test cases :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     *       "Response from server"     expected getResponse() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     *                                  getResponseMessage() results
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static Object[][] getTests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        return new Object[][] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            { "HTTP/1.1 200 OK",        "200",  "OK" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            { "HTTP/1.1 404 ",          "404",  null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            { "HTTP/1.1 200",           "200",  null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            { "HTTP/1.1",               "-1",   null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            { "Invalid",                "-1",   null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            { null,                     "-1" ,  null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Simple http server used by test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * GET /<n> HTTP/1.x results in http response with the status line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * set to geTests()[<n>][0]  -- eg: GET /2 results in a response of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * "HTTP/1.1 404 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static class HttpServer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public HttpServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                throw new Error("Unable to create ServerSocket: " + ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        public int port() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            return ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        public void shutdown() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            Object[][] tests = getTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    Socket s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    BufferedReader in = new BufferedReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                              new InputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                                s.getInputStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    String req = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    int pos1 = req.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    int pos2 = req.indexOf(' ', pos1+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    int i = Integer.parseInt(req.substring(pos1+2, pos2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    PrintStream out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                        new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                          s.getOutputStream() ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    out.print( tests[i][0] );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    out.print("Content-Length: 0\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    out.print("Connection: close\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    s.shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        /* start the http server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        HttpServer svr = new HttpServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        (new Thread(svr)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int port = svr.port();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         * Iterate through each test case and check that getResponseCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         * returns the expected result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        Object tests[][] = getTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        for (int i=0; i<tests.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            System.out.println("******************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            System.out.println("Test with response: >" + tests[i][0] + "<");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            URL url = new URL("http://localhost:" + port + "/" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            HttpURLConnection http = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                // test getResponseCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                int expectedCode = Integer.parseInt((String)tests[i][1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                int actualCode = http.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (actualCode != expectedCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    System.out.println("getResponseCode returned: " + actualCode +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        ", expected: " + expectedCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                // test getResponseMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                String expectedPhrase = (String)tests[i][2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                String actualPhrase = http.getResponseMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (actualPhrase == null && expectedPhrase == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if (!actualPhrase.equals(expectedPhrase)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    System.out.println("getResponseMessage returned: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        actualPhrase + ", expected: " + expectedPhrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        /* shutdown http server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        svr.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new Exception(failures + " sub-test(s) failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}