use of com.koushikdutta.async.http.server.AsyncHttpServer 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.AsyncHttpServer 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.AsyncHttpServer 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();
}
}
use of com.koushikdutta.async.http.server.AsyncHttpServer 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.AsyncHttpServer in project ion by koush.
the class BitmapTests method test404.
public void test404() throws Exception {
AsyncHttpServer httpServer = new AsyncHttpServer();
httpServer.listen(5566);
try {
final Semaphore semaphore = new Semaphore(0);
Ion.with(getContext()).load("http://localhost:5566/foo.png").asBitmap().setCallback(new FutureCallback<Bitmap>() {
@Override
public void onCompleted(Exception e, Bitmap result) {
semaphore.release();
assertNotNull(e);
}
});
semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS);
Ion.with(getContext()).load("http://localhost:5566/foo.png").asBitmap().setCallback(new FutureCallback<Bitmap>() {
@Override
public void onCompleted(Exception e, Bitmap result) {
semaphore.release();
assertNotNull(e);
}
});
semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS);
} finally {
httpServer.stop();
AsyncServer.getDefault().stop();
}
}
Aggregations