Search in sources :

Example 21 with AsyncHttpServer

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

the class AuthTests method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    httpServer = new AsyncHttpServer();
    httpServer.listen(5555);
}
Also used : AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer)

Example 22 with AsyncHttpServer

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

the class HeadersTests method testHeaders.

public void testHeaders() throws Exception {
    gotHeaders = false;
    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);
        Ion.with(getContext()).load("http://localhost:5555/").onHeaders(new HeadersCallback() {

            @Override
            public void onHeaders(HeadersResponse headers) {
                assertEquals(headers.code(), 200);
                gotHeaders = true;
            }
        }).asString().get();
        assertTrue(gotHeaders);
    } finally {
        httpServer.stop();
        Ion.getDefault(getContext()).getServer().stop();
    }
}
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) HeadersCallback(com.koushikdutta.ion.HeadersCallback) HeadersResponse(com.koushikdutta.ion.HeadersResponse)

Example 23 with AsyncHttpServer

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

the class HttpTests method testPut.

public void testPut() throws Exception {
    AsyncHttpServer httpServer = new AsyncHttpServer();
    try {
        httpServer.addAction("PUT", "/", new HttpServerRequestCallback() {

            @Override
            public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
                response.send(request.getMethod());
            }
        });
        httpServer.listen(Ion.getDefault(getContext()).getServer(), 5555);
        Future<String> ret = Ion.with(getContext()).load("PUT", "http://localhost:5555/").asString();
        assertEquals(ret.get(), "PUT");
    } finally {
        httpServer.stop();
    }
}
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 24 with AsyncHttpServer

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

the class StreamTests method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    AsyncHttpServer server = new AsyncHttpServer();
    port = server.listen(0).getLocalPort();
    server.get("/", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
            response.code(200);
            ByteBuffer b = ByteBufferList.obtain(random.length);
            b.put(random);
            b.flip();
            ByteBufferList list = new ByteBufferList(b);
            Util.writeAll(response, list, new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    response.end();
                }
            });
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) ByteBuffer(java.nio.ByteBuffer)

Example 25 with AsyncHttpServer

use of com.koushikdutta.async.http.server.AsyncHttpServer 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"));
                }
            });
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) MultipartCallback(com.koushikdutta.async.http.body.MultipartFormDataBody.MultipartCallback) DataCallback(com.koushikdutta.async.callback.DataCallback) Part(com.koushikdutta.async.http.body.Part) DataEmitter(com.koushikdutta.async.DataEmitter) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

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