jdk/test/com/sun/net/httpserver/Test3.java
author chegar
Wed, 21 Jul 2010 13:29:26 +0100
changeset 6115 7c523cf2bc8a
parent 5506 202f599c92aa
permissions -rw-r--r--
6969395: TEST_BUG: Tests in java/net sun/net problems Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3070
diff changeset
     2
 * Copyright (c) 2005, 2006, 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: 3070
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3070
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3070
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 6270015
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary  Light weight HTTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @run main/othervm -Dsun.net.httpserver.idleInterval=4 Test3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.regex.Pattern.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Test pipe-lining over http
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class Test3 extends Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static int count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        System.out.print ("Test3: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        Handler handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        InetSocketAddress addr = new InetSocketAddress (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        HttpServer server = HttpServer.create (addr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int port = server.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        HttpContext c2 = server.createContext ("/test", handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        c2.getAttributes().put ("name", "This is the http handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        ExecutorService exec = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        server.setExecutor (exec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            server.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            doClient(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            System.out.println ("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            delay();
3070
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    65
            if (server != null)
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    66
                server.stop(2);
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    67
            if (exec != null)
0175bbf9a833 6513803: httpserver regression test Test13 failing and causing NullPointerException
michaelm
parents: 2
diff changeset
    68
                exec.shutdown();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static class Handler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        volatile int invocation = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public void handle (HttpExchange t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            InputStream is = t.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            Headers map = t.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            Headers rmap = t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            int x = invocation ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            rmap.set ("XTest", Integer.toString (x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            switch (x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                try {Thread.sleep (2000); } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                checkBody (is, body1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                try {Thread.sleep (1000); } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                checkBody (is, body2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                checkBody (is, body3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                checkBody (is, body4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            t.sendResponseHeaders (200, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static void checkBody (InputStream is, String cmp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        byte [] b = new byte [1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int count = 0, c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        while ((c=is.read(b, count, b.length-count)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            count+=c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        String s = new String (b, 0, count, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (!s.equals (cmp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new RuntimeException ("strings not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static String body1 = "1234567890abcdefghij";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static String body2 = "2234567890abcdefghij0123456789";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    static String body3 = "3wertyuiop";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static String body4 = "4234567890";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static String result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        "HTTP/1.1 200 OK.*Xtest: 0.*"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        "HTTP/1.1 200 OK.*Xtest: 1.*"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        "HTTP/1.1 200 OK.*Xtest: 2.*"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        "HTTP/1.1 200 OK.*Xtest: 3.*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public static void doClient (int port) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        String s = "GET /test/1.html HTTP/1.1\r\nContent-length: 20\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        "\r\n" +body1 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        "GET /test/2.html HTTP/1.1\r\nContent-length: 30\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        "\r\n"+ body2 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        "GET /test/3.html HTTP/1.1\r\nContent-length: 10\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        "\r\n"+ body3 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        "GET /test/4.html HTTP/1.1\r\nContent-length: 10\r\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        "\r\n"+body4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        Socket socket = new Socket ("localhost", port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        OutputStream os = socket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        os.write (s.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        InputStream is = socket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        int c, count=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        byte[] b = new byte [1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        while ((c=is.read(b, count, b.length-count)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            count +=c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        socket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        s = new String (b,0,count, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (!compare (s, result)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new RuntimeException ("wrong string result");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    static boolean compare (String s, String result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        Pattern pattern = Pattern.compile (result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                Pattern.DOTALL|Pattern.CASE_INSENSITIVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        Matcher matcher = pattern.matcher (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return matcher.matches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}