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 metrics by dropwizard.
the class InstrumentedHttpClientsTest method registersExpectedExceptionMetrics.
@Test
public void registersExpectedExceptionMetrics() throws Exception {
HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
final HttpGet get = new HttpGet("http://localhost:" + httpServer.getAddress().getPort() + "/");
final String requestMetricName = "request";
final String exceptionMetricName = "exception";
httpServer.createContext("/", HttpExchange::close);
httpServer.start();
when(metricNameStrategy.getNameFor(any(), any(HttpRequest.class))).thenReturn(requestMetricName);
when(metricNameStrategy.getNameFor(any(), any(Exception.class))).thenReturn(exceptionMetricName);
try {
client.execute(get);
fail();
} catch (NoHttpResponseException expected) {
assertThat(metricRegistry.getMeters()).containsKey("exception");
} finally {
httpServer.stop(0);
}
}
use of com.sun.net.httpserver.HttpExchange in project tomee by apache.
the class HttpConnectionTest method init.
@Before
public void init() throws Exception {
server = HttpServer.create(new InetSocketAddress(0), 5);
server.createContext("/e", new HttpHandler() {
@Override
public void handle(final HttpExchange exchange) throws IOException {
exchange.getResponseHeaders().set("Content-Type", "text/plain");
exchange.sendResponseHeaders(200, 0);
final OutputStream responseBody = exchange.getResponseBody();
responseBody.write("secure page".getBytes());
final String query = exchange.getRequestURI().getQuery();
if (query != null) {
responseBody.write(query.getBytes());
}
final String authorization = exchange.getRequestHeaders().getFirst("Authorization");
if (authorization != null) {
responseBody.write(authorization.getBytes("UTF-8"));
}
final String authorization2 = exchange.getRequestHeaders().getFirst("AltAuthorization");
if (authorization2 != null) {
responseBody.write(("alt" + authorization2).getBytes("UTF-8"));
}
responseBody.close();
}
});
server.start();
}
use of com.sun.net.httpserver.HttpExchange in project pinot by linkedin.
the class ServerTableSizeReaderTest method createHandler.
private HttpHandler createHandler(final int status, final TableSizeInfo tableSize, final int sleepTimeMs) {
return new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
if (sleepTimeMs > 0) {
try {
Thread.sleep(sleepTimeMs);
} catch (InterruptedException e) {
LOGGER.info("Handler interrupted during sleep");
}
}
String json = new ObjectMapper().writeValueAsString(tableSize);
httpExchange.sendResponseHeaders(status, json.length());
OutputStream responseBody = httpExchange.getResponseBody();
responseBody.write(json.getBytes());
responseBody.close();
}
};
}
use of com.sun.net.httpserver.HttpExchange in project pinot by linkedin.
the class TableSizeReaderTest method createHandler.
private HttpHandler createHandler(final int status, final List<SegmentSizeInfo> segmentSizes, final int sleepTimeMs) {
return new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
if (sleepTimeMs > 0) {
try {
Thread.sleep(sleepTimeMs);
} catch (InterruptedException e) {
LOGGER.info("Handler interrupted during sleep");
}
}
TableSizeInfo tableInfo = new TableSizeInfo("myTable", 0);
tableInfo.segments = segmentSizes;
for (SegmentSizeInfo segmentSize : segmentSizes) {
tableInfo.diskSizeInBytes += segmentSize.diskSizeInBytes;
}
String json = new ObjectMapper().writeValueAsString(tableInfo);
httpExchange.sendResponseHeaders(status, json.length());
OutputStream responseBody = httpExchange.getResponseBody();
responseBody.write(json.getBytes());
responseBody.close();
}
};
}
Aggregations