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");
}
}
}));
}
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]);
}
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();
}
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]));
}
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;
}
Aggregations