use of com.koushikdutta.async.http.server.AsyncHttpServerResponse in project AndroidAsync by koush.
the class MultipartTests 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.post("/", new HttpServerRequestCallback() {
int gotten = 0;
@Override
public void onRequest(final AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
final MultipartFormDataBody body = (MultipartFormDataBody) request.getBody();
body.setMultipartCallback(new MultipartCallback() {
@Override
public void onPart(Part part) {
if (part.isFile()) {
body.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
gotten += bb.remaining();
bb.recycle();
}
});
}
}
});
request.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
response.send(body.getField("baz") + gotten + body.getField("foo"));
}
});
}
});
}
use of com.koushikdutta.async.http.server.AsyncHttpServerResponse in project AndroidAsync by koush.
the class RedirectTests method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
AsyncHttpServer server = new AsyncHttpServer();
server.listen(6003);
server.get("/foo", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
response.redirect("/bar");
}
});
server.get("/bar", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
response.send("BORAT!");
}
});
server.get("/foo/poo", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
response.redirect("../poo");
}
});
server.get("/poo", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
response.send("SWEET!");
}
});
server.get("/foo/bar", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
response.redirect("baz");
}
});
server.get("/foo/baz", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
response.send("SUCCESS!");
}
});
}
use of com.koushikdutta.async.http.server.AsyncHttpServerResponse in project AndroidAsync by koush.
the class CacheTests method testMaxAgePrivate.
public void testMaxAgePrivate() throws Exception {
AsyncHttpClient client = new AsyncHttpClient(AsyncServer.getDefault());
ResponseCacheMiddleware cache = ResponseCacheMiddleware.addCache(client, new File(getContext().getFilesDir(), "AndroidAsyncTest"), 1024 * 1024 * 10);
AsyncHttpServer httpServer = new AsyncHttpServer();
try {
httpServer.get("/uname/(.*)", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
response.getHeaders().set("Date", HttpDate.format(new Date()));
response.getHeaders().set("Cache-Control", "private, max-age=10000");
response.send(request.getMatcher().group(1));
}
});
AsyncServerSocket socket = httpServer.listen(AsyncServer.getDefault(), 0);
int port = socket.getLocalPort();
// clear the old cache
cache.clear();
client.executeString(new AsyncHttpGet("http://localhost:" + port + "/uname/43434"), null).get();
client.executeString(new AsyncHttpGet("http://localhost:" + port + "/uname/43434"), null).get();
assertEquals(cache.getCacheHitCount(), 1);
assertEquals(cache.getNetworkCount(), 1);
} finally {
AsyncServer.getDefault().stop();
client.getMiddleware().remove(cache);
}
}
use of com.koushikdutta.async.http.server.AsyncHttpServerResponse in project AndroidAsync by koush.
the class HttpClientTests method testProxy.
public void testProxy() throws Exception {
wasProxied = false;
final AsyncServer proxyServer = new AsyncServer();
try {
AsyncProxyServer httpServer = new AsyncProxyServer(proxyServer) {
@Override
protected boolean onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
wasProxied = true;
return super.onRequest(request, response);
}
};
AsyncServerSocket socket = httpServer.listen(proxyServer, 0);
// client.getSocketMiddleware().enableProxy("localhost", 5555);
AsyncHttpGet get = new AsyncHttpGet("http://www.clockworkmod.com");
get.enableProxy("localhost", socket.getLocalPort());
Future<String> ret = client.executeString(get, null);
String data;
assertNotNull(data = ret.get(TIMEOUT, TimeUnit.MILLISECONDS));
assertTrue(data.contains("ClockworkMod"));
assertTrue(wasProxied);
} finally {
proxyServer.stop();
}
}
use of com.koushikdutta.async.http.server.AsyncHttpServerResponse in project AndroidAsync by koush.
the class Issue59 method testIssue.
public void testIssue() throws Exception {
AsyncHttpServer httpServer = new AsyncHttpServer();
try {
httpServer.get("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
// setting this to empty is a hacky way of telling the framework not to use
// transfer-encoding. It will get removed.
response.getHeaders().set("Transfer-Encoding", "");
response.code(200);
Util.writeAll(response, "foobarbeepboop".getBytes(), new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
response.end();
}
});
}
});
httpServer.listen(5959);
AsyncHttpGet get = new AsyncHttpGet("http://localhost:5959/");
get.setLogging("issue59", Log.VERBOSE);
get.getHeaders().removeAll("Connection");
get.getHeaders().removeAll("Accept-Encoding");
assertEquals("foobarbeepboop", AsyncHttpClient.getDefaultInstance().executeString(get, null).get(1000, TimeUnit.MILLISECONDS));
} finally {
httpServer.stop();
AsyncServer.getDefault().stop();
}
}
Aggregations