test/jdk/java/net/URLConnection/Responses.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58365 73950479184b
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54746
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 2002, 2019, 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 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.*;
58365
73950479184b 8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents: 58009
diff changeset
    32
import static java.net.Proxy.NO_PROXY;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class Responses {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     * Test cases :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     *       "Response from server"     expected getResponse() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     *                                  getResponseMessage() results
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static Object[][] getTests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        return new Object[][] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            { "HTTP/1.1 200 OK",        "200",  "OK" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            { "HTTP/1.1 404 ",          "404",  null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            { "HTTP/1.1 200",           "200",  null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            { "HTTP/1.1",               "-1",   null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            { "Invalid",                "-1",   null },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            { null,                     "-1" ,  null },
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Simple http server used by test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * GET /<n> HTTP/1.x results in http response with the status line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * set to geTests()[<n>][0]  -- eg: GET /2 results in a response of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * "HTTP/1.1 404 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static class HttpServer implements Runnable {
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 54746
diff changeset
    60
        final ServerSocket ss;
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 54746
diff changeset
    61
        volatile boolean shutdown;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public HttpServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            try {
54746
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    65
                InetAddress loopback = InetAddress.getLoopbackAddress();
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    66
                ss = new ServerSocket();
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    67
                ss.bind(new InetSocketAddress(loopback, 0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                throw new Error("Unable to create ServerSocket: " + ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
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 int port() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            return ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
54746
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    77
        public String authority() {
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    78
            InetAddress address = ss.getInetAddress();
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    79
            String hostaddr = address.isAnyLocalAddress()
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    80
                ? "localhost" : address.getHostAddress();
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    81
            if (hostaddr.indexOf(':') > -1) {
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    82
                hostaddr = "[" + hostaddr + "]";
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    83
            }
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    84
            return hostaddr + ":" + port();
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    85
        }
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        public void shutdown() throws IOException {
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 54746
diff changeset
    88
            shutdown = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            Object[][] tests = getTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            try {
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 54746
diff changeset
    96
                while(!shutdown) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    Socket s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    BufferedReader in = new BufferedReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                              new InputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                                s.getInputStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    String req = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    int pos1 = req.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    int pos2 = req.indexOf(' ', pos1+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    int i = Integer.parseInt(req.substring(pos1+2, pos2));
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 54746
diff changeset
   107
                    System.out.println("Server replying to >" + tests[i][0] + "<");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    PrintStream out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                        new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                          s.getOutputStream() ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    out.print( tests[i][0] );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    out.print("Content-Length: 0\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    out.print("Connection: close\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    s.shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            } catch (Exception e) {
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 54746
diff changeset
   124
                if (!shutdown) {
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 54746
diff changeset
   125
                    e.printStackTrace();
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 54746
diff changeset
   126
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        /* start the http server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        HttpServer svr = new HttpServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        (new Thread(svr)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
54746
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
   138
        String authority = svr.authority();
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
   139
        System.out.println("Server listening on: " + authority);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * Iterate through each test case and check that getResponseCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         * returns the expected result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        Object tests[][] = getTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        for (int i=0; i<tests.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            System.out.println("******************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            System.out.println("Test with response: >" + tests[i][0] + "<");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
54746
61049e91eae5 8223463: Replace wildcard address with loopback or local host in tests - part 2
dfuchs
parents: 47216
diff changeset
   152
            URL url = new URL("http://" + authority + "/" + i);
58365
73950479184b 8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents: 58009
diff changeset
   153
            HttpURLConnection http = (HttpURLConnection)url.openConnection(NO_PROXY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                // test getResponseCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                int expectedCode = Integer.parseInt((String)tests[i][1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                int actualCode = http.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                if (actualCode != expectedCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    System.out.println("getResponseCode returned: " + actualCode +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        ", expected: " + expectedCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                // test getResponseMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                String expectedPhrase = (String)tests[i][2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                String actualPhrase = http.getResponseMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                if (actualPhrase == null && expectedPhrase == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (!actualPhrase.equals(expectedPhrase)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    System.out.println("getResponseMessage returned: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        actualPhrase + ", expected: " + expectedPhrase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } catch (IOException e) {
58009
0daf32316b47 8230435: Replace wildcard address with loopback or local host in tests - part 22
dfuchs
parents: 54746
diff changeset
   180
                System.err.println("Test failed for >" + tests[i][0] + "<: " + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        /* shutdown http server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        svr.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw new Exception(failures + " sub-test(s) failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}