Path: blob/master/test/jdk/com/sun/net/httpserver/Test5.java
41152 views
/*1* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 627001526* @summary Light weight HTTP server27* @run main/othervm -Dsun.net.httpserver.idleInterval=4 Test528* @run main/othervm -Djava.net.preferIPv6Addresses=true29* -Dsun.net.httpserver.idleInterval=4 Test530*/3132import com.sun.net.httpserver.*;3334import java.util.*;35import java.util.concurrent.*;36import java.util.regex.*;37import java.io.*;38import java.net.*;39import java.security.*;40import javax.net.ssl.*;4142/**43* Test pipe-lining (no block)44*/4546public class Test5 extends Test {47static int count = 1;48public static void main (String[] args) throws Exception {49System.out.print ("Test5: ");50Handler handler = new Handler();51InetAddress loopback = InetAddress.getLoopbackAddress();52InetSocketAddress addr = new InetSocketAddress(loopback, 0);53HttpServer server = HttpServer.create (addr, 0);54int port = server.getAddress().getPort();55HttpContext c2 = server.createContext ("/test", handler);56c2.getAttributes().put ("name", "This is the http handler");5758ExecutorService exec = Executors.newCachedThreadPool();59server.setExecutor (exec);60try {61server.start ();62doClient(port);63System.out.println ("OK");64} finally {65delay ();66if (server != null)67server.stop(2);68if (exec != null)69exec.shutdown();70}71}7273static class Handler implements HttpHandler {74volatile int invocation = 0;75public void handle (HttpExchange t)76throws IOException77{78InputStream is = t.getRequestBody();79Headers map = t.getRequestHeaders();80Headers rmap = t.getResponseHeaders();81int x = invocation ++;82rmap.set ("XTest", Integer.toString (x));8384switch (x) {85case 0:86checkBody (is, body1);87break;88case 1:89checkBody (is, body2);90break;91case 2:92checkBody (is, body3);93break;94case 3:95checkBody (is, body4);96break;97}98t.sendResponseHeaders (200, -1);99t.close();100}101}102103static void checkBody (InputStream is, String cmp) throws IOException {104byte [] b = new byte [1024];105int count = 0, c;106while ((c=is.read(b, count, b.length-count)) != -1) {107count+=c;108}109is.close();110String s = new String (b, 0, count, "ISO8859_1");111if (!s.equals (cmp)) {112throw new RuntimeException ("strings not equal");113}114}115116static String body1 = "1234567890abcdefghij";117static String body2 = "2234567890abcdefghij0123456789";118static String body3 = "3wertyuiop";119static String body4 = "4234567890";120121static String result =122"HTTP/1.1 200 OK.*Xtest: 0.*"+123"HTTP/1.1 200 OK.*Xtest: 1.*"+124"HTTP/1.1 200 OK.*Xtest: 2.*"+125"HTTP/1.1 200 OK.*Xtest: 3.*";126127public static void doClient (int port) throws Exception {128String s = "GET /test/1.html HTTP/1.1\r\nContent-length: 20\r\n"+129"\r\n" +body1 +130"GET /test/2.html HTTP/1.1\r\nContent-length: 30\r\n"+131"\r\n"+ body2 +132"GET /test/3.html HTTP/1.1\r\nContent-length: 10\r\n"+133"\r\n"+ body3 +134"GET /test/4.html HTTP/1.1\r\nContent-length: 10\r\n"+135"\r\n"+body4;136137Socket socket = new Socket (InetAddress.getLoopbackAddress(), port);138OutputStream os = socket.getOutputStream();139os.write (s.getBytes());140InputStream is = socket.getInputStream();141int c, count=0;142byte[] b = new byte [1024];143while ((c=is.read(b, count, b.length-count)) != -1) {144count +=c;145}146is.close();147socket.close();148s = new String (b,0,count, "ISO8859_1");149if (!compare (s, result)) {150System.err.println(" Expected [" + result + "]\n actual [" + s + "]");151throw new RuntimeException ("wrong string result");152}153}154155static boolean compare (String s, String result) {156Pattern pattern = Pattern.compile (result,157Pattern.DOTALL|Pattern.CASE_INSENSITIVE158);159Matcher matcher = pattern.matcher (s);160return matcher.matches();161}162}163164165