Search in sources :

Example 11 with HttpServer

use of com.sun.net.httpserver.HttpServer in project jdk8u_jdk by JetBrains.

the class UnmodifiableMaps method test.

void test(String[] args) throws Exception {
    HttpServer server = startHttpServer();
    try {
        InetSocketAddress address = server.getAddress();
        URI uri = new URI("http://" + InetAddress.getLocalHost().getHostAddress() + ":" + address.getPort() + "/foo");
        doClient(uri);
    } finally {
        server.stop(0);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) URI(java.net.URI)

Example 12 with HttpServer

use of com.sun.net.httpserver.HttpServer in project jdk8u_jdk by JetBrains.

the class UnmodifiableMaps method startHttpServer.

// HTTP Server
HttpServer startHttpServer() throws IOException {
    HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
    httpServer.createContext("/foo", new SimpleHandler());
    httpServer.start();
    return httpServer;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer)

Example 13 with HttpServer

use of com.sun.net.httpserver.HttpServer in project jdk8u_jdk by JetBrains.

the class BasicLongCredentials method main.

public static void main(String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    try {
        Handler handler = new Handler();
        HttpContext ctx = server.createContext("/test", handler);
        BasicAuthenticator a = new BasicAuthenticator(REALM) {

            public boolean checkCredentials(String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();
        Authenticator.setDefault(new MyAuthenticator());
        URL url = new URL("http://localhost:" + server.getAddress().getPort() + "/test/");
        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read() != -1) {
            c++;
        }
        if (c != 0) {
            throw new RuntimeException("Test failed c = " + c);
        }
        if (error) {
            throw new RuntimeException("Test failed: error");
        }
        System.out.println("OK");
    } finally {
        server.stop(0);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) HttpServer(com.sun.net.httpserver.HttpServer) HttpContext(com.sun.net.httpserver.HttpContext) HttpHandler(com.sun.net.httpserver.HttpHandler) URL(java.net.URL)

Example 14 with HttpServer

use of com.sun.net.httpserver.HttpServer in project jdk8u_jdk by JetBrains.

the class HttpStreams method test.

void test() throws Exception {
    HttpServer server = null;
    try {
        server = startHttpServer();
        String baseUrl = "http://localhost:" + server.getAddress().getPort() + "/";
        client(baseUrl + "chunked/");
        client(baseUrl + "fixed/");
        client(baseUrl + "error/");
        client(baseUrl + "chunkedError/");
        // Test with a response cache
        ResponseCache ch = ResponseCache.getDefault();
        ResponseCache.setDefault(new TrivialCacheHandler());
        try {
            client(baseUrl + "chunked/");
            client(baseUrl + "fixed/");
            client(baseUrl + "error/");
            client(baseUrl + "chunkedError/");
        } finally {
            ResponseCache.setDefault(ch);
        }
    } finally {
        if (server != null)
            server.stop(0);
    }
    System.out.println("passed: " + pass + ", failed: " + fail);
    if (fail > 0)
        throw new RuntimeException("some tests failed check output");
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer)

Example 15 with HttpServer

use of com.sun.net.httpserver.HttpServer in project jdk8u_jdk by JetBrains.

the class NoCache method startHttpServer.

// HTTP Server
static HttpServer startHttpServer() throws IOException {
    HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
    httpServer.createContext("/NoCache/", new SimpleHandler());
    httpServer.start();
    return httpServer;
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer)

Aggregations

HttpServer (com.sun.net.httpserver.HttpServer)48 InetSocketAddress (java.net.InetSocketAddress)28 HttpHandler (com.sun.net.httpserver.HttpHandler)12 IOException (java.io.IOException)10 File (java.io.File)7 Test (org.junit.Test)7 HttpContext (com.sun.net.httpserver.HttpContext)6 HttpExchange (com.sun.net.httpserver.HttpExchange)6 JarFile (java.util.jar.JarFile)6 MockHttpServer (org.elasticsearch.mocksocket.MockHttpServer)6 URL (java.net.URL)5 BasicAuthenticator (com.sun.net.httpserver.BasicAuthenticator)3 HttpsServer (com.sun.net.httpserver.HttpsServer)3 InputStream (java.io.InputStream)3 ExecutorService (java.util.concurrent.ExecutorService)3 CompilerError (com.redhat.ceylon.compiler.java.test.CompilerError)2 HttpsConfigurator (com.sun.net.httpserver.HttpsConfigurator)2 FileNotFoundException (java.io.FileNotFoundException)2 OutputStream (java.io.OutputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2