Search in sources :

Example 6 with AsyncHttpGet

use of com.koushikdutta.async.http.AsyncHttpGet 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 7 with AsyncHttpGet

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

the class RedirectTests method testRelativeRedirect.

public void testRelativeRedirect() throws Exception {
    String ret = AsyncHttpClient.getDefaultInstance().executeString(new AsyncHttpGet("http://localhost:6003/foo/bar"), null).get();
    assertEquals(ret, "SUCCESS!");
    ret = AsyncHttpClient.getDefaultInstance().executeString(new AsyncHttpGet("http://localhost:6003/foo"), null).get();
    assertEquals(ret, "BORAT!");
    ret = AsyncHttpClient.getDefaultInstance().executeString(new AsyncHttpGet("http://localhost:6003/foo/poo"), null).get();
    assertEquals(ret, "SWEET!");
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet)

Example 8 with AsyncHttpGet

use of com.koushikdutta.async.http.AsyncHttpGet 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 9 with AsyncHttpGet

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

the class HttpClientTests method testGithubRandomData.

public void testGithubRandomData() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final Md5 md5 = Md5.createInstance();
    AsyncHttpGet get = new AsyncHttpGet(github);
    get.setLogging("AsyncTest", Log.VERBOSE);
    client.execute(get, new HttpConnectCallback() {

        @Override
        public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
            assertNull(ex);
            // make sure gzip decoding works, as that is generally what github sends.
            // this broke sometime in 03/2014
            //                Assert.assertEquals("gzip", response.getHeaders().getContentEncoding());
            response.setDataCallback(new DataCallback() {

                @Override
                public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                    md5.update(bb);
                }
            });
            response.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    semaphore.release();
                }
            });
        }
    });
    assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
    assertEquals(md5.digest(), dataNameAndHash);
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) ByteBufferList(com.koushikdutta.async.ByteBufferList) HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) DataEmitter(com.koushikdutta.async.DataEmitter) Semaphore(java.util.concurrent.Semaphore) DataCallback(com.koushikdutta.async.callback.DataCallback) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with AsyncHttpGet

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

the class HttpClientTests method testFileCancel.

public void testFileCancel() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    File f = getContext().getFileStreamPath("test.txt");
    fileFuture = client.executeFile(new AsyncHttpGet(github), f.getAbsolutePath(), new AsyncHttpClient.FileCallback() {

        @Override
        public void onCompleted(Exception e, AsyncHttpResponse source, File result) {
            fail();
        }

        @Override
        public void onProgress(AsyncHttpResponse response, long downloaded, long total) {
            semaphore.release();
        }
    }).setCallback(new FutureCallback<File>() {

        @Override
        public void onCompleted(Exception e, File result) {
            assertTrue(e instanceof CancellationException);
        }
    });
    try {
        assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
        assertTrue(fileFuture.cancel());
        fileFuture.get();
        fail();
    } catch (ExecutionException ex) {
        assertTrue(ex.getCause() instanceof CancellationException);
    }
    //        Thread.sleep(1000);
    //        assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
    assertFalse(f.exists());
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) CancellationException(java.util.concurrent.CancellationException) Semaphore(java.util.concurrent.Semaphore) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.koushikdutta.async.future.FutureCallback) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient)

Aggregations

AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)14 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)4 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)4 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)3 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)3 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)3 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)3 CancellationException (java.util.concurrent.CancellationException)3 ExecutionException (java.util.concurrent.ExecutionException)3 Semaphore (java.util.concurrent.Semaphore)3 TimeoutException (java.util.concurrent.TimeoutException)3 AsyncServer (com.koushikdutta.async.AsyncServer)2 AsyncServerSocket (com.koushikdutta.async.AsyncServerSocket)2 ByteBufferList (com.koushikdutta.async.ByteBufferList)2 AsyncHttpResponse (com.koushikdutta.async.http.AsyncHttpResponse)2 File (java.io.File)2 Uri (android.net.Uri)1 DataEmitter (com.koushikdutta.async.DataEmitter)1 DataCallback (com.koushikdutta.async.callback.DataCallback)1 FutureCallback (com.koushikdutta.async.future.FutureCallback)1