Test10.java revision 6073:cea72c2bf071
1/*
2 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @bug 7005016
27 * @summary  pit jdk7 b121  sqe test jhttp/HttpServer150013 failing
28 * @run main/othervm -Dsun.net.httpserver.clockTick=1000 -Dsun.net.httpserver.idleInterval=3 Test10
29 */
30
31import com.sun.net.httpserver.*;
32
33import java.io.*;
34import java.net.*;
35import java.util.concurrent.*;
36
37/*
38 * Test handling of empty Http headers
39 */
40
41public class Test10 extends Test {
42    public static void main (String[] args) throws Exception {
43        System.out.print ("Test10: ");
44        Handler handler = new Handler();
45        InetSocketAddress addr = new InetSocketAddress (0);
46        HttpServer server = HttpServer.create (addr, 0);
47        int port = server.getAddress().getPort();
48        HttpContext c2 = server.createContext ("/test", handler);
49
50        ExecutorService exec = Executors.newCachedThreadPool();
51        server.setExecutor (exec);
52        try {
53            server.start ();
54            doClient(port);
55            System.out.println ("OK");
56        } finally {
57            delay();
58            if (server != null)
59                server.stop(2);
60            if (exec != null)
61                exec.shutdown();
62        }
63    }
64
65    static class Handler implements HttpHandler {
66        volatile int invocation = 0;
67        public void handle (HttpExchange t)
68            throws IOException
69        {
70            InputStream is = t.getRequestBody();
71            while (is.read() != -1);
72            Headers map = t.getRequestHeaders();
73            t.sendResponseHeaders (200, -1);
74            t.close();
75        }
76    }
77
78    public static void doClient (int port) throws Exception {
79        String s = "GET /test/1.html HTTP/1.1\r\n\r\n";
80
81        Socket socket = new Socket ("localhost", port);
82        OutputStream os = socket.getOutputStream();
83        os.write (s.getBytes());
84        socket.setSoTimeout (10 * 1000);
85        InputStream is = socket.getInputStream();
86        int c;
87        byte[] b = new byte [1024];
88        while ((c=is.read(b)) != -1) ;
89        is.close();
90        socket.close();
91    }
92}
93