Search in sources :

Example 26 with AsyncHttpServer

use of com.koushikdutta.async.http.server.AsyncHttpServer 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!");
        }
    });
}
Also used : HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse)

Example 27 with AsyncHttpServer

use of com.koushikdutta.async.http.server.AsyncHttpServer 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);
    }
}
Also used : AsyncServerSocket(com.koushikdutta.async.AsyncServerSocket) AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) ResponseCacheMiddleware(com.koushikdutta.async.http.cache.ResponseCacheMiddleware) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) File(java.io.File) Date(java.util.Date) HttpDate(com.koushikdutta.async.http.HttpDate) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient)

Example 28 with AsyncHttpServer

use of com.koushikdutta.async.http.server.AsyncHttpServer in project AndroidAsync by koush.

the class WebSocketTests 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.websocket("/ws", new WebSocketRequestCallback() {

        @Override
        public void onConnected(final WebSocket webSocket, AsyncHttpServerRequest request) {
            webSocket.setStringCallback(new StringCallback() {

                @Override
                public void onStringAvailable(String s) {
                    webSocket.send(s);
                }
            });
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) WebSocketRequestCallback(com.koushikdutta.async.http.server.AsyncHttpServer.WebSocketRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) StringCallback(com.koushikdutta.async.http.WebSocket.StringCallback) WebSocket(com.koushikdutta.async.http.WebSocket)

Example 29 with AsyncHttpServer

use of com.koushikdutta.async.http.server.AsyncHttpServer 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();
    }
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse)

Aggregations

AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)29 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)22 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)20 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)20 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)7 MultipartFormDataBody (com.koushikdutta.async.http.body.MultipartFormDataBody)4 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)3 Part (com.koushikdutta.async.http.body.Part)3 Semaphore (java.util.concurrent.Semaphore)3 ConnectivityManager (android.net.ConnectivityManager)2 NetworkInfo (android.net.NetworkInfo)2 Handlebars (com.github.jknack.handlebars.Handlebars)2 JsonObject (com.google.gson.JsonObject)2 AsyncServer (com.koushikdutta.async.AsyncServer)2 ByteBufferList (com.koushikdutta.async.ByteBufferList)2 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)2 WebSocket (com.koushikdutta.async.http.WebSocket)2 UrlEncodedFormBody (com.koushikdutta.async.http.body.UrlEncodedFormBody)2 HeadersResponse (com.koushikdutta.ion.HeadersResponse)2 File (java.io.File)2