Path: blob/master/test/jdk/com/sun/net/httpserver/Test14.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* @library /test/lib28* @run main Test1429* @run main/othervm -Djava.net.preferIPv6Addresses=true Test1430*/3132import com.sun.net.httpserver.*;3334import java.util.*;35import java.util.concurrent.*;36import java.io.*;37import java.net.*;38import java.security.*;39import javax.security.auth.callback.*;40import javax.net.ssl.*;41import jdk.test.lib.net.URIBuilder;4243/**44* Test filters45*/4647public class Test14 extends Test {4849static final String test_input = "Hello world";50static final String test_output = "Ifmmp!xpsme";5152/* an outputstream which transforms the output data53* by adding one to each byte54*/55static class OffsetOutputStream extends FilterOutputStream {56OffsetOutputStream (OutputStream os) {57super (os);58}59public void write (int b) throws IOException {60super.write (b+1);61}62}6364static class OffsetFilter extends Filter {65public String description() {66return "Translates outgoing data";67}6869public void destroy(HttpContext c) {}70public void init(HttpContext c) {}7172public void doFilter (HttpExchange exchange, Filter.Chain chain)73throws IOException {74exchange.setStreams (null, new OffsetOutputStream(75exchange.getResponseBody()76));77chain.doFilter (exchange);78}79}8081public static void main (String[] args) throws Exception {82Handler handler = new Handler();83InetAddress loopback = InetAddress.getLoopbackAddress();84InetSocketAddress addr = new InetSocketAddress(loopback, 0);85HttpServer server = HttpServer.create (addr, 0);86HttpContext ctx = server.createContext ("/test", handler);8788File logfile = new File (89System.getProperty ("test.classes")+ "/log.txt"90);9192ctx.getFilters().add (new OffsetFilter());93ctx.getFilters().add (new LogFilter(logfile));94if (ctx.getFilters().size() != 2) {95throw new RuntimeException ("wrong filter list size");96}97ExecutorService executor = Executors.newCachedThreadPool();98server.setExecutor (executor);99server.start ();100101URL url = URIBuilder.newBuilder()102.scheme("http")103.loopback()104.port(server.getAddress().getPort())105.path("/test/foo.html")106.toURL();107System.out.print ("Test14: " );108HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);109InputStream is = urlc.getInputStream();110int x = 0;111String output="";112while ((x=is.read())!= -1) {113output = output + (char)x;114}115error = !output.equals (test_output);116server.stop(2);117executor.shutdown();118if (error ) {119throw new RuntimeException ("test failed error");120}121System.out.println ("OK");122123}124125public static boolean error = false;126127static class Handler implements HttpHandler {128int invocation = 1;129public void handle (HttpExchange t)130throws IOException131{132InputStream is = t.getRequestBody();133Headers map = t.getRequestHeaders();134Headers rmap = t.getResponseHeaders();135while (is.read () != -1) ;136is.close();137String response = test_input;138t.sendResponseHeaders (200, response.length());139OutputStream os = t.getResponseBody();140os.write (response.getBytes());141t.close();142}143}144}145146147