use of com.koushikdutta.async.http.server.HttpServerRequestCallback in project AndroidAsync by koush.
the class HttpServerTests method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
httpServer = new AsyncHttpServer();
httpServer.setErrorCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
fail();
}
});
httpServer.listen(AsyncServer.getDefault(), 5000);
httpServer.get("/hello", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
assertNotNull(request.getHeaders().get("Host"));
response.send("hello");
}
});
httpServer.post("/echo", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
try {
assertNotNull(request.getHeaders().get("Host"));
JSONObject json = new JSONObject();
if (request.getBody() instanceof UrlEncodedFormBody) {
UrlEncodedFormBody body = (UrlEncodedFormBody) request.getBody();
for (NameValuePair pair : body.get()) {
json.put(pair.getName(), pair.getValue());
}
} else if (request.getBody() instanceof JSONObjectBody) {
json = ((JSONObjectBody) request.getBody()).get();
} else if (request.getBody() instanceof StringBody) {
json.put("foo", ((StringBody) request.getBody()).get());
} else if (request.getBody() instanceof MultipartFormDataBody) {
MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
for (NameValuePair pair : body.get()) {
json.put(pair.getName(), pair.getValue());
}
}
response.send(json);
} catch (Exception e) {
}
}
});
}
use of com.koushikdutta.async.http.server.HttpServerRequestCallback in project AndroidAsync by koush.
the class SSLTests 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);
AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setSSLContext(sslContext);
AsyncHttpClient.getDefaultInstance().getSSLSocketMiddleware().setTrustManagers(tmf.getTrustManagers());
AsyncHttpClient.getDefaultInstance().executeString(new AsyncHttpGet("https://localhost:8888/"), null).get();
}
use of com.koushikdutta.async.http.server.HttpServerRequestCallback in project ion by koush.
the class GsonTests method testJunkPayload.
public void testJunkPayload() throws Exception {
AsyncHttpServer httpServer = new AsyncHttpServer();
try {
httpServer.get("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
response.send("not json!");
}
});
httpServer.listen(5555);
Future<JsonObject> ret = Ion.with(getContext()).load("PUT", "http://localhost:5555/").asJsonObject();
ret.get();
fail();
} catch (ExecutionException e) {
assertTrue(e.getCause() instanceof JsonParseException);
} finally {
httpServer.stop();
AsyncServer.getDefault().stop();
}
}
use of com.koushikdutta.async.http.server.HttpServerRequestCallback in project ion by koush.
the class HeadersTests method testHeadersCallback.
public void testHeadersCallback() throws Exception {
AsyncHttpServer httpServer = new AsyncHttpServer();
try {
httpServer.get("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
response.send("hello");
}
});
httpServer.listen(Ion.getDefault(getContext()).getServer(), 5555);
final Semaphore semaphore = new Semaphore(0);
Ion.with(getContext()).load("http://localhost:5555/").asString().withResponse().setCallback(new FutureCallback<Response<String>>() {
@Override
public void onCompleted(Exception e, Response<String> result) {
assertEquals(result.getHeaders().code(), 200);
semaphore.release();
}
});
assertTrue(semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS));
} finally {
httpServer.stop();
Ion.getDefault(getContext()).getServer().stop();
}
}
use of com.koushikdutta.async.http.server.HttpServerRequestCallback in project ion by koush.
the class HeadersTests method testBustedJson.
public void testBustedJson() throws Exception {
AsyncHttpServer httpServer = new AsyncHttpServer();
try {
httpServer.get("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
response.send("hello");
}
});
httpServer.listen(Ion.getDefault(getContext()).getServer(), 5555);
Response<JsonObject> response = Ion.with(getContext()).load("http://localhost:5555/").asJsonObject().withResponse().get();
assertNull(response.getResult());
assertNotNull(response.getException());
} finally {
httpServer.stop();
Ion.getDefault(getContext()).getServer().stop();
}
}
Aggregations