Search in sources :

Example 6 with AsyncHttpServer

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();
    }
}
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) JsonObject(com.google.gson.JsonObject) ExecutionException(java.util.concurrent.ExecutionException) JsonParseException(com.google.gson.JsonParseException)

Example 7 with AsyncHttpServer

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();
    }
}
Also used : HeadersResponse(com.koushikdutta.ion.HeadersResponse) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) Response(com.koushikdutta.ion.Response) 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) Semaphore(java.util.concurrent.Semaphore)

Example 8 with AsyncHttpServer

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();
    }
}
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) JsonObject(com.google.gson.JsonObject)

Example 9 with AsyncHttpServer

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();
    }
}
Also used : HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) Part(com.koushikdutta.async.http.body.Part) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) File(java.io.File) MultipartFormDataBody(com.koushikdutta.async.http.body.MultipartFormDataBody)

Example 10 with AsyncHttpServer

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();
    }
}
Also used : Bitmap(android.graphics.Bitmap) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) Semaphore(java.util.concurrent.Semaphore)

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