Search in sources :

Example 1 with Ion

use of com.koushikdutta.ion.Ion in project ion by koush.

the class CookieTests method testCookies.

public void testCookies() throws Exception {
    Ion ion = Ion.getDefault(getContext());
    ion.getCookieMiddleware().clear();
    CookieManager manager = new CookieManager(null, null);
    Headers headers = new Headers();
    headers.set("Set-Cookie", "foo=bar");
    URI uri = URI.create("http://example.com");
    manager.put(uri, headers.getMultiMap());
    headers.set("Set-Cookie", "poop=scoop");
    manager.put(uri, headers.getMultiMap());
    headers.set("Set-Cookie", "foo=goop");
    manager.put(uri, headers.getMultiMap());
    Headers newHeaders = new Headers();
    Map<String, List<String>> cookies = manager.get(uri, newHeaders.getMultiMap());
    manager.get(uri, cookies);
    CookieMiddleware.addCookies(cookies, newHeaders);
    assertTrue(newHeaders.get("Cookie").contains("foo=goop"));
    assertTrue(newHeaders.get("Cookie").contains("poop=scoop"));
    assertFalse(newHeaders.get("Cookie").contains("bar"));
}
Also used : Headers(com.koushikdutta.async.http.Headers) Ion(com.koushikdutta.ion.Ion) List(java.util.List) URI(java.net.URI) CookieManager(java.net.CookieManager)

Example 2 with Ion

use of com.koushikdutta.ion.Ion in project ion by koush.

the class HttpTests method testCookie.

public void testCookie() throws Exception {
    Ion ion = Ion.getDefault(getContext());
    ion.getCookieMiddleware().clear();
    ion.build(getContext()).load("http://google.com").asString().get();
    for (HttpCookie cookie : ion.getCookieMiddleware().getCookieStore().get(URI.create("http://www.google.com"))) {
        Log.i("CookieTest", cookie.getName() + ": " + cookie.getValue());
    }
    assertTrue(ion.getCookieMiddleware().getCookieManager().get(URI.create("http://www.google.com/test/path"), new Multimap()).size() > 0);
    CookieMiddleware deserialize = new CookieMiddleware(ion);
    assertTrue(deserialize.getCookieManager().get(URI.create("http://www.google.com/test/path"), new Multimap()).size() > 0);
}
Also used : Multimap(com.koushikdutta.async.http.Multimap) CookieMiddleware(com.koushikdutta.ion.cookie.CookieMiddleware) Ion(com.koushikdutta.ion.Ion) HttpCookie(java.net.HttpCookie)

Example 3 with Ion

use of com.koushikdutta.ion.Ion in project ion by koush.

the class SelfSignedCertificateTests method testKeys.

public void testKeys() throws Exception {
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(getContext().getResources().openRawResource(R.raw.keystore), "storepass".toCharArray());
    kmf.init(ks, "storepass".toCharArray());
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    KeyStore ts = KeyStore.getInstance(KeyStore.getDefaultType());
    ts.load(getContext().getResources().openRawResource(R.raw.keystore), "storepass".toCharArray());
    tmf.init(ts);
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
    AsyncHttpServer httpServer = new AsyncHttpServer();
    httpServer.listenSecure(8888, sslContext);
    httpServer.get("/", new HttpServerRequestCallback() {

        @Override
        public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
            response.send("hello");
        }
    });
    Thread.sleep(1000);
    Ion ion = Ion.getInstance(getContext(), "CustomSSL");
    ion.getHttpClient().getSSLSocketMiddleware().setSSLContext(sslContext);
    ion.getHttpClient().getSSLSocketMiddleware().setTrustManagers(tmf.getTrustManagers());
    ion.build(getContext()).load("https://localhost:8888/").asString().get();
}
Also used : HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) Ion(com.koushikdutta.ion.Ion) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 4 with Ion

use of com.koushikdutta.ion.Ion in project ion by koush.

the class CookieTests method testReinit.

public void testReinit() throws Exception {
    CookieMiddleware middleware = Ion.getDefault(getContext()).getCookieMiddleware();
    Ion ion = Ion.getDefault(getContext());
    ion.getCookieMiddleware().clear();
    Headers headers = new Headers();
    headers.set("Set-Cookie", "foo=bar");
    URI uri = URI.create("http://example.com");
    middleware.put(uri, headers);
    headers.set("Set-Cookie", "poop=scoop");
    middleware.put(uri, headers);
    headers.set("Set-Cookie", "foo=goop");
    middleware.put(uri, headers);
    middleware.reinit();
    CookieManager manager = middleware.getCookieManager();
    Headers newHeaders = new Headers();
    Map<String, List<String>> cookies = manager.get(uri, newHeaders.getMultiMap());
    manager.get(uri, cookies);
    CookieMiddleware.addCookies(cookies, newHeaders);
    assertTrue(newHeaders.get("Cookie").contains("foo=goop"));
    assertTrue(newHeaders.get("Cookie").contains("poop=scoop"));
    assertFalse(newHeaders.get("Cookie").contains("bar"));
}
Also used : CookieMiddleware(com.koushikdutta.ion.cookie.CookieMiddleware) Headers(com.koushikdutta.async.http.Headers) Ion(com.koushikdutta.ion.Ion) List(java.util.List) URI(java.net.URI) CookieManager(java.net.CookieManager)

Aggregations

Ion (com.koushikdutta.ion.Ion)4 Headers (com.koushikdutta.async.http.Headers)2 CookieMiddleware (com.koushikdutta.ion.cookie.CookieMiddleware)2 CookieManager (java.net.CookieManager)2 URI (java.net.URI)2 List (java.util.List)2 Multimap (com.koushikdutta.async.http.Multimap)1 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)1 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)1 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)1 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)1 HttpCookie (java.net.HttpCookie)1 KeyStore (java.security.KeyStore)1 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)1 SSLContext (javax.net.ssl.SSLContext)1 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)1