test/jdk/sun/net/www/protocol/http/6550798/test.java
author aeubanks
Wed, 27 Mar 2019 09:06:43 -0700
changeset 54314 46cf212cdcca
parent 47216 71c04702a3d5
child 54787 7748aa47b4e2
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:
6685
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     1
/*
38557
5c485e1ea6fa 8157635: Fix module dependencies for /sun/* tests
jjiang
parents: 6685
diff changeset
     2
 * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
6685
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     4
 *
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     8
 *
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    13
 * accompanied this code).
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    14
 *
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    18
 *
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    21
 * questions.
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    22
 */
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    23
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    24
/**
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    25
 * @test
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    26
 * @bug 6550798
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    27
 * @library /test/lib
6685
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    28
 * @summary Using InputStream.skip with ResponseCache will cause partial data to be cached
38557
5c485e1ea6fa 8157635: Fix module dependencies for /sun/* tests
jjiang
parents: 6685
diff changeset
    29
 * @modules jdk.httpserver
6685
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    30
 * @run main/othervm test
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    31
 */
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    32
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    33
import java.net.*;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    34
import com.sun.net.httpserver.*;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    35
import java.io.*;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    36
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    37
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
    38
6685
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    39
public class test {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    40
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    41
    final static int LEN = 16 * 1024;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    42
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    43
    public static void main(String[] args)  throws Exception {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    44
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    45
        TestCache.reset();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    46
        HttpServer s = HttpServer.create (new InetSocketAddress(0), 10);
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    47
        s.createContext ("/", new HttpHandler () {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    48
            public void handle (HttpExchange e) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    49
                try {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    50
                    byte[] buf = new byte [LEN];
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    51
                    OutputStream o = e.getResponseBody();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    52
                    e.sendResponseHeaders(200, LEN);
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    53
                    o.write (buf);
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    54
                    e.close();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    55
                } catch (IOException ex) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    56
                    ex.printStackTrace();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    57
                    TestCache.fail = true;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    58
                }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    59
            }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    60
        });
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    61
        s.start();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    62
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    63
        System.out.println("http request with cache hander");
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    64
        URL u = URIBuilder.newBuilder()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    65
            .scheme("http")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    66
            .loopback()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    67
            .port(s.getAddress().getPort())
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    68
            .path("/f")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    69
            .toURL();
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    70
        System.out.println("URL: " + u);
6685
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    71
        URLConnection conn = u.openConnection();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    72
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    73
        InputStream is = null;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    74
        try {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    75
            // this calls into TestCache.get
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    76
            byte[] buf = new byte[8192];
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    77
            is = new BufferedInputStream(conn.getInputStream());
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    78
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    79
            is.skip(1000);
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    80
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    81
            while (is.read(buf) != -1) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    82
            }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    83
        } finally {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    84
            if (is != null) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    85
                // this calls into TestCache.put
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    86
                // TestCache.put will check if the resource
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    87
                // should be cached
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    88
                is.close();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    89
            }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    90
            s.stop(0);
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    91
        }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    92
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    93
        if (TestCache.fail) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    94
            System.out.println ("TEST FAILED");
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    95
            throw new RuntimeException ();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    96
        } else {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    97
            System.out.println ("TEST OK");
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    98
        }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    99
    }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
   100
}