use of io.cdap.http.NettyHttpService in project cdap by caskdata.
the class HttpHandlerGeneratorTest method testContentConsumer.
@Test
public void testContentConsumer() throws Exception {
HttpHandlerFactory factory = new HttpHandlerFactory("/content", TransactionControl.IMPLICIT);
// Create the file upload handler and starts a netty server with it
final File outputDir = TEMP_FOLDER.newFolder();
HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(FileHandler.class), new AbstractDelegatorContext<FileHandler>() {
@Override
protected FileHandler createHandler() {
return new FileHandler(outputDir);
}
}, new NoopMetricsContext());
// Creates a Netty http server with 1K request buffer
NettyHttpService service = NettyHttpService.builder("test-content-consumer").setHttpHandlers(httpHandler).setHttpChunkLimit(1024).build();
service.start();
try {
InetSocketAddress bindAddress = service.getBindAddress();
testUpload(outputDir, bindAddress, "");
testUpload(outputDir, bindAddress, "-no-tx");
} finally {
service.stop();
}
}
use of io.cdap.http.NettyHttpService in project cdap by caskdata.
the class HttpHandlerGeneratorTest method testContentProducer.
@Test
public void testContentProducer() throws Exception {
HttpHandlerFactory factory = new HttpHandlerFactory("/content", TransactionControl.IMPLICIT);
// Create the file upload handler and starts a netty server with it
final File outputDir = TEMP_FOLDER.newFolder();
HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(FileHandler.class), new AbstractDelegatorContext<FileHandler>() {
@Override
protected FileHandler createHandler() {
return new FileHandler(outputDir);
}
}, new NoopMetricsContext());
NettyHttpService service = NettyHttpService.builder("test-content-producer").setHttpHandlers(httpHandler).build();
service.start();
try {
// Generate a 100K file
File file = TEMP_FOLDER.newFile();
Files.write(Strings.repeat("0123456789", 10240).getBytes(Charsets.UTF_8), file);
InetSocketAddress bindAddress = service.getBindAddress();
// Upload the generated file
URL uploadURL = new URL(String.format("http://%s:%d/content/upload/test.txt", bindAddress.getHostName(), bindAddress.getPort()));
HttpURLConnection urlConn = (HttpURLConnection) uploadURL.openConnection();
try {
urlConn.setDoOutput(true);
urlConn.setRequestMethod("PUT");
Files.copy(file, urlConn.getOutputStream());
Assert.assertEquals(200, urlConn.getResponseCode());
} finally {
urlConn.disconnect();
}
// Download the file
File downloadFile = TEMP_FOLDER.newFile();
urlConn = (HttpURLConnection) new URL(String.format("http://%s:%d/content/download/test.txt", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
try {
ByteStreams.copy(urlConn.getInputStream(), Files.newOutputStreamSupplier(downloadFile));
} finally {
urlConn.disconnect();
}
// Compare if the file content are the same
Assert.assertTrue(Files.equal(file, downloadFile));
// Download a file that doesn't exist
urlConn = (HttpURLConnection) new URL(String.format("http://%s:%d/content/download/test2.txt", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
try {
Assert.assertEquals(500, urlConn.getResponseCode());
} finally {
urlConn.disconnect();
}
// Upload the file to the POST endpoint. The endpoint should response with the same file content
downloadFile = TEMP_FOLDER.newFile();
urlConn = (HttpURLConnection) uploadURL.openConnection();
try {
urlConn.setDoOutput(true);
urlConn.setRequestMethod("POST");
Files.copy(file, urlConn.getOutputStream());
ByteStreams.copy(urlConn.getInputStream(), Files.newOutputStreamSupplier(downloadFile));
Assert.assertEquals(200, urlConn.getResponseCode());
Assert.assertTrue(Files.equal(file, downloadFile));
} finally {
urlConn.disconnect();
}
} finally {
service.stop();
}
}
Aggregations