J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 6270015 |
| 27 | * @summary Light weight HTTP server |
| 28 | */ |
| 29 | |
| 30 | import java.net.*; |
| 31 | import java.util.*; |
| 32 | import java.util.concurrent.*; |
| 33 | import java.io.*; |
| 34 | import com.sun.net.httpserver.*; |
| 35 | |
| 36 | public class Test11 { |
| 37 | static class Handler implements HttpHandler { |
| 38 | public void handle(HttpExchange t) throws IOException { |
| 39 | read (t.getRequestBody()); |
| 40 | String response = "response"; |
| 41 | t.sendResponseHeaders (200, response.length()); |
| 42 | OutputStream os = t.getResponseBody(); |
| 43 | os.write (response.getBytes ("ISO8859_1")); |
| 44 | t.close(); |
| 45 | } |
| 46 | |
| 47 | void read (InputStream is ) throws IOException { |
| 48 | byte[] b = new byte [8096]; |
| 49 | while (is.read (b) != -1) {} |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public static void main (String[] args) throws Exception { |
| 54 | System.out.print ("Test 11: "); |
| 55 | HttpServer server = HttpServer.create (new InetSocketAddress(0), 0); |
| 56 | HttpContext ctx = server.createContext ( |
| 57 | "/foo/bar/", new Handler () |
| 58 | ); |
| 59 | ExecutorService s = Executors.newCachedThreadPool(); |
| 60 | server.setExecutor (s); |
| 61 | server.start (); |
| 62 | URL url = new URL ("http://localhost:" + server.getAddress().getPort()+ |
| 63 | "/Foo/bar/test.html"); |
| 64 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection(); |
| 65 | int r = urlc.getResponseCode(); |
| 66 | System.out.println ("OK"); |
| 67 | s.shutdown(); |
| 68 | server.stop(5); |
| 69 | if (r == 200) { |
| 70 | throw new RuntimeException ("wrong response received"); |
| 71 | } |
| 72 | } |
| 73 | } |