test/jdk/sun/net/www/protocol/http/6550798/test.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 38557 jdk/test/sun/net/www/protocol/http/6550798/test.java@5c485e1ea6fa
child 54314 46cf212cdcca
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    27
 * @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
    28
 * @modules jdk.httpserver
6685
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    29
 * @run main/othervm test
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    30
 */
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
import java.net.*;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    33
import com.sun.net.httpserver.*;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    34
import java.io.*;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    35
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    36
public class test {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    37
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    38
    final static int LEN = 16 * 1024;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    39
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    40
    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
    41
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    42
        TestCache.reset();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    43
        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
    44
        s.createContext ("/", new HttpHandler () {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    45
            public void handle (HttpExchange e) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    46
                try {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    47
                    byte[] buf = new byte [LEN];
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    48
                    OutputStream o = e.getResponseBody();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    49
                    e.sendResponseHeaders(200, LEN);
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    50
                    o.write (buf);
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    51
                    e.close();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    52
                } catch (IOException ex) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    53
                    ex.printStackTrace();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    54
                    TestCache.fail = true;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    55
                }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    56
            }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    57
        });
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    58
        s.start();
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
        System.out.println("http request with cache hander");
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    61
        URL u = new URL("http://127.0.0.1:"+s.getAddress().getPort()+"/f");
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    62
        URLConnection conn = u.openConnection();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    63
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    64
        InputStream is = null;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    65
        try {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    66
            // this calls into TestCache.get
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    67
            byte[] buf = new byte[8192];
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    68
            is = new BufferedInputStream(conn.getInputStream());
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    69
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    70
            is.skip(1000);
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    71
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    72
            while (is.read(buf) != -1) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    73
            }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    74
        } finally {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    75
            if (is != null) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    76
                // this calls into TestCache.put
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    77
                // 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
    78
                // should be cached
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    79
                is.close();
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
            s.stop(0);
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
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    84
        if (TestCache.fail) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    85
            System.out.println ("TEST FAILED");
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    86
            throw new RuntimeException ();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    87
        } else {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    88
            System.out.println ("TEST OK");
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
    }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    91
}