test/jdk/sun/net/www/protocol/http/B8012625.java
author aeubanks
Wed, 27 Mar 2019 09:06:43 -0700
changeset 54314 46cf212cdcca
parent 47216 71c04702a3d5
child 54681 edd709e64ea1
permissions -rw-r--r--
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder Reviewed-by: dfuchs, chegar Contributed-by: aeubanks@google.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17485
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     1
/*
38557
5c485e1ea6fa 8157635: Fix module dependencies for /sun/* tests
jjiang
parents: 17485
diff changeset
     2
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
17485
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     4
 *
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     8
 *
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    13
 * accompanied this code).
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    14
 *
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    18
 *
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    21
 * questions.
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    22
 */
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    23
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    24
/**
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    25
 * @test
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    26
 * @bug 8012625
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    27
 * @library /test/lib
38557
5c485e1ea6fa 8157635: Fix module dependencies for /sun/* tests
jjiang
parents: 17485
diff changeset
    28
 * @modules jdk.httpserver
17485
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    29
 * @run main B8012625
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    30
 */
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    31
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    32
import java.net.*;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    33
import java.io.*;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    34
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    35
import java.net.*;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    36
import java.io.*;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    37
import java.util.concurrent.*;
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    38
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    39
import jdk.test.lib.net.URIBuilder;
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    40
17485
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    41
import com.sun.net.httpserver.*;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    42
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    43
public class B8012625 implements HttpHandler {
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    44
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    45
    public static void main (String[] args) throws Exception {
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    46
        B8012625 test = new B8012625();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    47
        test.run();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    48
    }
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    49
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    50
    public void run() throws Exception {
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    51
        URL url = URIBuilder.newBuilder()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    52
            .scheme("http")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    53
            .loopback()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    54
            .port(port)
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    55
            .path("/foo")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    56
            .toURL();
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    57
        System.out.println("URL: " + url);
17485
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    58
        HttpURLConnection uc = (HttpURLConnection)url.openConnection();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    59
        uc.setDoOutput(true);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    60
        uc.setRequestMethod("POST");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    61
        uc.addRequestProperty("Expect", "100-Continue");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    62
        //uc.setFixedLengthStreamingMode(256);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    63
        System.out.println ("Client: getting outputstream");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    64
        long before = System.currentTimeMillis();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    65
        OutputStream os = uc.getOutputStream();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    66
        long after = System.currentTimeMillis();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    67
        System.out.println ("Client: writing to outputstream");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    68
        byte[] buf = new byte[256];
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    69
        os.write(buf);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    70
        System.out.println ("Client: done writing ");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    71
        int r = uc.getResponseCode();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    72
        System.out.println ("Client: received response code " + r);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    73
        server.stop(1);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    74
        ex.shutdownNow();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    75
        if (after - before >= 5000) {
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    76
            throw new RuntimeException("Error: 5 second delay seen");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    77
        }
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    78
    }
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    79
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    80
    int port;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    81
    HttpServer server;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    82
    ExecutorService ex;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    83
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    84
    public B8012625 () throws Exception {
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    85
        server = HttpServer.create(new InetSocketAddress(0), 10);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    86
        HttpContext ctx = server.createContext("/", this);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    87
        ex = Executors.newFixedThreadPool(5);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    88
        server.setExecutor(ex);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    89
        server.start();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    90
        port = server.getAddress().getPort();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    91
   }
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    92
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    93
    public void handle(HttpExchange ex) throws IOException {
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    94
        String s = ex.getRequestMethod();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    95
        if (!s.equals("POST")) {
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    96
            ex.getResponseHeaders().set("Allow", "POST");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    97
            ex.sendResponseHeaders(500, -1);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    98
            ex.close();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
    99
            return;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
   100
        }
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
   101
        System.out.println ("Server: reading request body");
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
   102
        InputStream is = ex.getRequestBody();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
   103
        // read request
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
   104
        byte[] buf = new byte [1024];
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
   105
        while (is.read(buf) != -1) ;
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
   106
        is.close();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
   107
        ex.sendResponseHeaders(200, -1);
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
   108
        ex.close();
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
   109
   }
feb8cd6a4c7d 8012625: Incorrect handling of HTTP/1.1 " Expect: 100-continue " in HttpURLConnection
michaelm
parents:
diff changeset
   110
}