use of com.koushikdutta.async.http.server.HttpServerRequestCallback in project ion by koush.
the class HttpTests method testMultipartFileContentType.
public void testMultipartFileContentType() throws Exception {
File f = getContext().getFileStreamPath("empty");
f.getParentFile().mkdirs();
f.createNewFile();
AsyncHttpServer httpServer = new AsyncHttpServer();
httpServer.post("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
body.setMultipartCallback(new MultipartFormDataBody.MultipartCallback() {
@Override
public void onPart(Part part) {
response.send(part.getContentType());
}
});
}
});
try {
httpServer.listen(AsyncServer.getDefault(), 6666);
String mime = Ion.with(getContext()).load("http://localhost:6666/").setMultipartFile("foo", "test/mime", f).asString().get(1000, TimeUnit.MILLISECONDS);
assertEquals(mime, "test/mime");
} finally {
httpServer.stop();
}
}
use of com.koushikdutta.async.http.server.HttpServerRequestCallback in project ion by koush.
the class ExceptionTests method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
AsyncHttpServer server = new AsyncHttpServer();
server.listen(5555);
server.get("/contentlength", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
response.code(200);
response.getHeaders().set("Content-Length", "10");
Util.writeAll(response, "five!".getBytes(), new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
// close the socket prematurely
response.getSocket().close();
}
});
}
});
server.get("/chunked", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
response.code(200);
Util.writeAll(response, "five!".getBytes(), new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
// close the socket prematurely
response.getSocket().close();
}
});
}
});
}
use of com.koushikdutta.async.http.server.HttpServerRequestCallback in project ion by koush.
the class Issues method testIssue714.
public void testIssue714() throws Exception {
AsyncHttpServer server = new AsyncHttpServer();
server.post("/test", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
response.send(request.getHeaders().get("Authorization"));
}
});
int port = server.listen(Ion.getDefault(getContext()).getServer(), 0).getLocalPort();
String auth = "";
for (int i = 0; i < 2048; i++) {
auth += (char) ('0' + (i % 10));
}
System.out.println(auth);
assertEquals(auth, Ion.with(getContext()).load("http://localhost:" + port + "/test").setHeader("Authorization", auth).setStringBody("testtest").asString().get());
}
use of com.koushikdutta.async.http.server.HttpServerRequestCallback in project ion by koush.
the class ProgressTests method testUpload.
public void testUpload() throws Exception {
AsyncHttpServer httpServer = new AsyncHttpServer();
try {
httpServer.listen(Ion.getDefault(getContext()).getServer(), 5000);
httpServer.post("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
MultipartFormDataBody multipartFormDataBody = (MultipartFormDataBody) request.getBody();
multipartFormDataBody.setMultipartCallback(new MultipartFormDataBody.MultipartCallback() {
@Override
public void onPart(Part part) {
}
});
multipartFormDataBody.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
response.send("Got parts!");
}
});
}
});
final Semaphore semaphore = new Semaphore(0);
Ion.with(getContext()).load("http://localhost:5000/").uploadProgress(new ProgressCallback() {
@Override
public void onProgress(long downloaded, long total) {
semaphore.release();
}
}).setMultipartParameter("foo", "bar").asString().get();
assertTrue(semaphore.tryAcquire());
} finally {
Ion.getDefault(getContext()).getServer().stop();
}
}
use of com.koushikdutta.async.http.server.HttpServerRequestCallback in project ion by koush.
the class SelfSignedCertificateTests method testKeys.
public void testKeys() throws Exception {
KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(getContext().getResources().openRawResource(R.raw.keystore), "storepass".toCharArray());
kmf.init(ks, "storepass".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore ts = KeyStore.getInstance(KeyStore.getDefaultType());
ts.load(getContext().getResources().openRawResource(R.raw.keystore), "storepass".toCharArray());
tmf.init(ts);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
AsyncHttpServer httpServer = new AsyncHttpServer();
httpServer.listenSecure(8888, sslContext);
httpServer.get("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
response.send("hello");
}
});
Thread.sleep(1000);
Ion ion = Ion.getInstance(getContext(), "CustomSSL");
ion.getHttpClient().getSSLSocketMiddleware().setSSLContext(sslContext);
ion.getHttpClient().getSSLSocketMiddleware().setTrustManagers(tmf.getTrustManagers());
ion.build(getContext()).load("https://localhost:8888/").asString().get();
}
Aggregations