use of com.koushikdutta.async.http.server.AsyncHttpServerRequest 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.AsyncHttpServerRequest 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();
}
}
use of com.koushikdutta.async.http.server.AsyncHttpServerRequest in project ion by koush.
the class Issues method testIssue146.
public void testIssue146() throws Exception {
AsyncHttpServer httpServer = new AsyncHttpServer();
httpServer.get("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
response.getHeaders().set("Cache-Control", "max-age=300");
response.send(request.getQuery().size() + "");
}
});
AsyncServer asyncServer = new AsyncServer();
try {
int localPort = httpServer.listen(asyncServer, 0).getLocalPort();
String s1 = Ion.with(getContext()).load("http://localhost:" + localPort).addQuery("query1", "q").asString().get();
String s2 = Ion.with(getContext()).load("http://localhost:" + localPort).addQuery("query1", "q").addQuery("query2", "qq").asString().get();
String s3 = Ion.with(getContext()).load("http://localhost:" + localPort).addQuery("query1", "q").asString().get();
assertEquals(s1, "1");
assertEquals(s2, "2");
assertEquals(s3, "1");
} finally {
asyncServer.stop();
}
}
use of com.koushikdutta.async.http.server.AsyncHttpServerRequest in project ion by koush.
the class Issues method testIssue329.
public void testIssue329() throws Exception {
AsyncHttpServer httpServer = new AsyncHttpServer();
httpServer.post("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
UrlEncodedFormBody body = (UrlEncodedFormBody) request.getBody();
response.send(body.get().getString("電"));
}
});
AsyncServer asyncServer = new AsyncServer();
try {
int localPort = httpServer.listen(asyncServer, 0).getLocalPort();
String s1 = Ion.with(getContext()).load("http://localhost:" + localPort).setBodyParameter("電", "電").asString().get();
assertEquals(s1, "電");
} finally {
asyncServer.stop();
}
}
use of com.koushikdutta.async.http.server.AsyncHttpServerRequest in project ion by koush.
the class RedirectTests method testFinalLocation.
public void testFinalLocation() throws Exception {
try {
AsyncHttpServer server = new AsyncHttpServer();
server.listen(Ion.getDefault(getContext()).getServer(), 5555);
server.get("/", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
response.redirect("/foo");
}
});
server.get("/foo", new HttpServerRequestCallback() {
@Override
public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
response.send("bar");
}
});
Response<String> response = Ion.with(getContext()).load("http://localhost:5555").asString().withResponse().get();
assertEquals(response.getResult(), "bar");
assertEquals(response.getRequest().getUri().toString(), "http://localhost:5555/foo");
} finally {
Ion.getDefault(getContext()).getServer().stop();
}
}
Aggregations