Search in sources :

Example 11 with HttpServerRequestCallback

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

Example 12 with HttpServerRequestCallback

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

Example 13 with HttpServerRequestCallback

use of com.koushikdutta.async.http.server.HttpServerRequestCallback 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();
    }
}
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 14 with HttpServerRequestCallback

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

the class AuthTests method testBasicAuth.

public void testBasicAuth() throws Exception {
    httpServer.get("/", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
            try {
                JsonObject json = new JsonObject();
                String authorization = request.getHeaders().get("Authorization").replace("Basic ", "");
                authorization = new String(Base64.decode(authorization, Base64.DEFAULT));
                String[] parts = authorization.split(":");
                assertTrue(parts.length == 2);
                String username = parts[0];
                String password = parts[1];
                json.addProperty("username", username);
                json.addProperty("password", password);
                response.send(json.toString());
            } catch (Exception e) {
                e.printStackTrace();
                fail();
            }
        }
    });
    JsonObject result = Ion.with(getContext()).load("http://localhost:5555").setTimeout(500).basicAuthentication("foo", "bar").asJsonObject().get();
    assertEquals("bar", result.get("password").getAsString());
    assertEquals("foo", result.get("username").getAsString());
}
Also used : HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) JsonObject(com.google.gson.JsonObject)

Example 15 with HttpServerRequestCallback

use of com.koushikdutta.async.http.server.HttpServerRequestCallback 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)

Aggregations

AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)21 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)21 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)21 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)20 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)6 MultipartFormDataBody (com.koushikdutta.async.http.body.MultipartFormDataBody)4 JsonObject (com.google.gson.JsonObject)3 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)3 Part (com.koushikdutta.async.http.body.Part)3 AsyncServer (com.koushikdutta.async.AsyncServer)2 ByteBufferList (com.koushikdutta.async.ByteBufferList)2 UrlEncodedFormBody (com.koushikdutta.async.http.body.UrlEncodedFormBody)2 HeadersResponse (com.koushikdutta.ion.HeadersResponse)2 File (java.io.File)2 KeyStore (java.security.KeyStore)2 Semaphore (java.util.concurrent.Semaphore)2 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)2 SSLContext (javax.net.ssl.SSLContext)2 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)2 JsonParseException (com.google.gson.JsonParseException)1