Search in sources :

Example 1 with AsyncServer

use of com.koushikdutta.async.AsyncServer in project AndroidAsync by koush.

the class SSLTests method disabled__testClientCertificateIssue163.

public void disabled__testClientCertificateIssue163() throws Exception {
    // https://security.springthroughtest.com/hello.json
    AsyncServer server = new AsyncServer();
    try {
        AsyncHttpClient client = new AsyncHttpClient(server);
        JSONObject json = client.executeJSONObject(new AsyncHttpGet("https://security.springthroughtest.com/hello.json"), null).get();
    } finally {
        server.stop();
    }
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) JSONObject(org.json.JSONObject) AsyncServer(com.koushikdutta.async.AsyncServer) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient)

Example 2 with AsyncServer

use of com.koushikdutta.async.AsyncServer in project ion by koush.

the class RequestBodyUploadObserver method write.

@Override
public void write(AsyncHttpRequest request, final DataSink sink, final CompletedCallback completed) {
    final int length = body.length();
    body.write(request, new DataSink() {

        int totalWritten;

        @Override
        public void write(ByteBufferList bb) {
            int start = bb.remaining();
            sink.write(bb);
            int wrote = start - bb.remaining();
            totalWritten += wrote;
            callback.onProgress(totalWritten, length);
        }

        @Override
        public void setWriteableCallback(WritableCallback handler) {
            sink.setWriteableCallback(handler);
        }

        @Override
        public WritableCallback getWriteableCallback() {
            return sink.getWriteableCallback();
        }

        @Override
        public boolean isOpen() {
            return sink.isOpen();
        }

        @Override
        public void end() {
            sink.end();
        }

        @Override
        public void setClosedCallback(CompletedCallback handler) {
            sink.setClosedCallback(handler);
        }

        @Override
        public CompletedCallback getClosedCallback() {
            return sink.getClosedCallback();
        }

        @Override
        public AsyncServer getServer() {
            return sink.getServer();
        }
    }, completed);
}
Also used : DataSink(com.koushikdutta.async.DataSink) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) AsyncServer(com.koushikdutta.async.AsyncServer) WritableCallback(com.koushikdutta.async.callback.WritableCallback)

Example 3 with AsyncServer

use of com.koushikdutta.async.AsyncServer 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 4 with AsyncServer

use of com.koushikdutta.async.AsyncServer 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 5 with AsyncServer

use of com.koushikdutta.async.AsyncServer in project ion by koush.

the class HttpTests 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 s = httpServer.listen(proxyServer, 0);
        Future<String> ret = Ion.with(getContext()).load("http://www.clockworkmod.com").proxy("localhost", s.getLocalPort()).asString();
        String data;
        assertNotNull(data = ret.get(10000, TimeUnit.MILLISECONDS));
        assertTrue(data.contains("ClockworkMod"));
        assertTrue(wasProxied);
    } finally {
        proxyServer.stop();
    }
}
Also used : AsyncServerSocket(com.koushikdutta.async.AsyncServerSocket) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncServer(com.koushikdutta.async.AsyncServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) AsyncProxyServer(com.koushikdutta.async.http.server.AsyncProxyServer)

Aggregations

AsyncServer (com.koushikdutta.async.AsyncServer)7 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)4 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)4 AsyncServerSocket (com.koushikdutta.async.AsyncServerSocket)2 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)2 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)2 AsyncProxyServer (com.koushikdutta.async.http.server.AsyncProxyServer)2 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)2 AsyncSocket (com.koushikdutta.async.AsyncSocket)1 ByteBufferList (com.koushikdutta.async.ByteBufferList)1 DataSink (com.koushikdutta.async.DataSink)1 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 ConnectCallback (com.koushikdutta.async.callback.ConnectCallback)1 WritableCallback (com.koushikdutta.async.callback.WritableCallback)1 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)1 UrlEncodedFormBody (com.koushikdutta.async.http.body.UrlEncodedFormBody)1 UnknownHostException (java.net.UnknownHostException)1 Semaphore (java.util.concurrent.Semaphore)1 JSONObject (org.json.JSONObject)1