Search in sources :

Example 21 with HttpExchange

use of com.sun.net.httpserver.HttpExchange in project gradle by gradle.

the class GitHttpRepository method expectCloneSomething.

public void expectCloneSomething() {
    server.expect(server.get(backingRepo.getName() + "/info/refs", getRefsAction()));
    server.expect(server.post(backingRepo.getName() + "/git-upload-pack", new ErroringAction<HttpExchange>() {

        @Override
        protected void doExecute(HttpExchange httpExchange) throws Exception {
            httpExchange.getResponseHeaders().add("content-type", "application/x-git-upload-pack-result");
            httpExchange.sendResponseHeaders(200, 0);
            ProcessBuilder builder = new ProcessBuilder();
            final Process process = builder.command("git", "upload-pack", "--stateless-rpc", backingRepo.getWorkTree().getAbsolutePath()).redirectErrorStream(true).start();
            InputStream instream = new GZIPInputStream(httpExchange.getRequestBody());
            ByteArrayOutputStream requestContent = new ByteArrayOutputStream();
            IOUtils.copy(instream, requestContent);
            byte[] bytes = requestContent.toByteArray();
            process.getOutputStream().write(bytes);
            process.getOutputStream().flush();
            IOUtils.copy(process.getInputStream(), httpExchange.getResponseBody());
            int result = process.waitFor();
            if (result != 0) {
                throw new RuntimeException("Failed to run git upload-pack");
            }
        }
    }));
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ErroringAction(org.gradle.internal.ErroringAction) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 22 with HttpExchange

use of com.sun.net.httpserver.HttpExchange in project ceylon by eclipse.

the class BootstrapTests method testBootstrapDownloadReadTimeoutInitial.

/**
 * Test the download when the first connection to the server experiences
 * a read timeout from the client side after 0 bytes served
 */
@Test
public void testBootstrapDownloadReadTimeoutInitial() throws Throwable {
    Assert.assertTrue(distZip.exists());
    BootstrapHttpHandler reliableHandler = new BootstrapHttpHandler() {

        @Override
        public void handle(HttpExchange exchange) throws IOException {
            if (handlerInvoked[0] == 0) {
                handlerInvoked[0]++;
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                super.handle(exchange);
            } else {
                super.handle(exchange);
            }
        }
    };
    HttpServer server = startServer(reliableHandler);
    BootstrapTester bootstrap = null;
    try {
        bootstrap = new BootstrapTester(server.getAddress());
        Assert.assertEquals(0, bootstrap.runInternal(""));
    } finally {
        server.stop(1);
        if (bootstrap != null) {
            bootstrap.deleteTmpFiles();
        }
    }
    // First connection should've read timed-out
    Assert.assertEquals(2, reliableHandler.handlerInvoked[0]);
}
Also used : HttpExchange(com.sun.net.httpserver.HttpExchange) HttpServer(com.sun.net.httpserver.HttpServer) Test(org.junit.Test)

Example 23 with HttpExchange

use of com.sun.net.httpserver.HttpExchange in project heron by twitter.

the class NetworkUtilsTest method testSendHttpResponse.

@Test
public void testSendHttpResponse() throws Exception {
    HttpExchange exchange = Mockito.mock(HttpExchange.class);
    Mockito.doNothing().when(exchange).sendResponseHeaders(Matchers.anyInt(), Matchers.anyLong());
    OutputStream os = Mockito.mock(OutputStream.class);
    Mockito.doReturn(os).when(exchange).getResponseBody();
    Mockito.doNothing().when(os).write(Matchers.any(byte[].class));
    Mockito.doNothing().when(os).close();
    Assert.assertTrue(NetworkUtils.sendHttpResponse(exchange, new byte[0]));
    Mockito.verify(exchange).getResponseBody();
    Mockito.verify(os, Mockito.atLeastOnce()).write(Matchers.any(byte[].class));
    Mockito.verify(os, Mockito.atLeastOnce()).close();
}
Also used : OutputStream(java.io.OutputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 24 with HttpExchange

use of com.sun.net.httpserver.HttpExchange in project heron by twitter.

the class NetworkUtilsTest method testSendHttpResponseFail.

@Test
public void testSendHttpResponseFail() throws Exception {
    HttpExchange exchange = Mockito.mock(HttpExchange.class);
    Mockito.doThrow(new IOException("Designed IO exception for testing")).when(exchange).sendResponseHeaders(Matchers.anyInt(), Matchers.anyLong());
    Assert.assertFalse(NetworkUtils.sendHttpResponse(exchange, new byte[0]));
    Mockito.verify(exchange, Mockito.never()).getResponseBody();
    Mockito.doNothing().when(exchange).sendResponseHeaders(Matchers.anyInt(), Matchers.anyLong());
    OutputStream os = Mockito.mock(OutputStream.class);
    Mockito.doReturn(os).when(exchange).getResponseBody();
    Mockito.doThrow(new IOException("Designed IO exception for testing")).when(os).write(Matchers.any(byte[].class));
    Assert.assertFalse(NetworkUtils.sendHttpResponse(exchange, new byte[0]));
    Mockito.verify(os, Mockito.atLeastOnce()).close();
    Mockito.doNothing().when(os).write(Matchers.any(byte[].class));
    Mockito.doThrow(new IOException("Designed IO exception for testing")).when(os).close();
    Assert.assertFalse(NetworkUtils.sendHttpResponse(exchange, new byte[0]));
}
Also used : OutputStream(java.io.OutputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 25 with HttpExchange

use of com.sun.net.httpserver.HttpExchange in project cloudstack by apache.

the class PrometheusExporterServerImpl method start.

@Override
public boolean start() {
    if (EnablePrometheusExporter.value()) {
        try {
            httpServer = HttpServer.create(new InetSocketAddress(PrometheusExporterServerPort.value()), 0);
            httpServer.createContext("/metrics", new ExporterHandler(prometheusExporter));
            httpServer.createContext("/", new HttpHandler() {

                @Override
                public void handle(HttpExchange httpExchange) throws IOException {
                    final String response = "<html><head><title>CloudStack Exporter</title></head>" + "<body><h1>CloudStack Exporter</h1>" + "<p><a href=\"/metrics\">Metrics</a></p>" + "</body></html>";
                    httpExchange.sendResponseHeaders(200, response.length());
                    final OutputStream os = httpExchange.getResponseBody();
                    os.write(response.getBytes());
                    os.close();
                }
            });
            httpServer.start();
            LOG.debug("Started prometheus exporter http server");
        } catch (final IOException e) {
            LOG.info("Failed to start prometheus exporter http server due to: ", e);
        }
    }
    return true;
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) HttpExchange(com.sun.net.httpserver.HttpExchange) IOException(java.io.IOException)

Aggregations

HttpExchange (com.sun.net.httpserver.HttpExchange)56 Test (org.junit.Test)36 IOException (java.io.IOException)25 HttpHandler (com.sun.net.httpserver.HttpHandler)22 InetSocketAddress (java.net.InetSocketAddress)19 OutputStream (java.io.OutputStream)18 Mockito.doAnswer (org.mockito.Mockito.doAnswer)16 InvocationOnMock (org.mockito.invocation.InvocationOnMock)16 Answer (org.mockito.stubbing.Answer)16 CountDownLatch (java.util.concurrent.CountDownLatch)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 HttpServer (com.sun.net.httpserver.HttpServer)11 Matchers.anyString (org.mockito.Matchers.anyString)11 Headers (com.sun.net.httpserver.Headers)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 StreamQueryDescriptor (com.urbanairship.connect.client.model.StreamQueryDescriptor)9 InputStream (java.io.InputStream)9 JsonObject (com.google.gson.JsonObject)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4