diff options
author | Koushik Dutta <koushd@gmail.com> | 2014-07-28 20:33:28 -0700 |
---|---|---|
committer | Koushik Dutta <koushd@gmail.com> | 2014-07-28 20:33:28 -0700 |
commit | 06987e72af6bad8d4c88a8b587218375f1955810 (patch) | |
tree | d4f14444fb01d1ac8794f07c81b84f15fc088071 /AndroidAsync | |
parent | 1280d7b44e14dbac19f773136742224bcab91302 (diff) | |
download | AndroidAsync-06987e72af6bad8d4c88a8b587218375f1955810.tar.gz AndroidAsync-06987e72af6bad8d4c88a8b587218375f1955810.tar.bz2 AndroidAsync-06987e72af6bad8d4c88a8b587218375f1955810.zip |
fix all the tests.
Diffstat (limited to 'AndroidAsync')
8 files changed, 19 insertions, 296 deletions
diff --git a/AndroidAsync/src/com/koushikdutta/async/http/cache/ResponseHeaders.java b/AndroidAsync/src/com/koushikdutta/async/http/cache/ResponseHeaders.java index 3275647..d71cf9d 100644 --- a/AndroidAsync/src/com/koushikdutta/async/http/cache/ResponseHeaders.java +++ b/AndroidAsync/src/com/koushikdutta/async/http/cache/ResponseHeaders.java @@ -160,7 +160,7 @@ final class ResponseHeaders { varyFields = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); } for (String varyField : value.split(",")) { - varyFields.add(varyField.trim()); + varyFields.add(varyField.trim().toLowerCase()); } } else if ("Content-Encoding".equalsIgnoreCase(fieldName)) { contentEncoding = value; diff --git a/AndroidAsync/test/src/com/koushikdutta/async/test/CacheTests.java b/AndroidAsync/test/src/com/koushikdutta/async/test/CacheTests.java index 78f3301..2edd1ae 100644 --- a/AndroidAsync/test/src/com/koushikdutta/async/test/CacheTests.java +++ b/AndroidAsync/test/src/com/koushikdutta/async/test/CacheTests.java @@ -1,6 +1,6 @@ package com.koushikdutta.async.test; -import android.os.Environment; +import android.test.AndroidTestCase; import com.koushikdutta.async.AsyncServer; import com.koushikdutta.async.AsyncServerSocket; @@ -13,18 +13,16 @@ import com.koushikdutta.async.http.server.AsyncHttpServerRequest; import com.koushikdutta.async.http.server.AsyncHttpServerResponse; import com.koushikdutta.async.http.server.HttpServerRequestCallback; -import junit.framework.TestCase; - import java.io.File; import java.util.Date; /** * Created by koush on 6/13/13. */ -public class CacheTests extends TestCase { +public class CacheTests extends AndroidTestCase { public void testMaxAgePrivate() throws Exception { AsyncHttpClient client = new AsyncHttpClient(AsyncServer.getDefault()); - ResponseCacheMiddleware cache = ResponseCacheMiddleware.addCache(client, new File(Environment.getExternalStorageDirectory(), "AndroidAsyncTest"), 1024 * 1024 * 10); + ResponseCacheMiddleware cache = ResponseCacheMiddleware.addCache(client, new File(getContext().getFilesDir(), "AndroidAsyncTest"), 1024 * 1024 * 10); AsyncHttpServer httpServer = new AsyncHttpServer(); try { httpServer.get("/uname/(.*)", new HttpServerRequestCallback() { diff --git a/AndroidAsync/test/src/com/koushikdutta/async/test/ConscryptTests.java b/AndroidAsync/test/src/com/koushikdutta/async/test/ConscryptTests.java index cd50148..2bcfd5a 100644 --- a/AndroidAsync/test/src/com/koushikdutta/async/test/ConscryptTests.java +++ b/AndroidAsync/test/src/com/koushikdutta/async/test/ConscryptTests.java @@ -17,24 +17,11 @@ package com.koushikdutta.async.test; -import com.koushikdutta.async.ByteBufferList; -import com.koushikdutta.async.http.spdy.ErrorCode; -import com.koushikdutta.async.http.spdy.FrameReader; -import com.koushikdutta.async.http.spdy.Header; -import com.koushikdutta.async.http.spdy.HeadersMode; -import com.koushikdutta.async.http.spdy.Settings; -import com.koushikdutta.async.http.spdy.Spdy3; -import com.koushikdutta.async.http.spdy.okio.BufferedSource; -import com.koushikdutta.async.http.spdy.okio.ByteString; -import com.koushikdutta.async.http.spdy.okio.Okio; - import junit.framework.TestCase; import org.conscrypt.OpenSSLEngineImpl; import org.conscrypt.OpenSSLProvider; -import java.io.ByteArrayInputStream; -import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Field; @@ -44,7 +31,6 @@ import java.net.Socket; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.security.Security; -import java.util.List; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; @@ -193,74 +179,6 @@ public class ConscryptTests extends TestCase { System.out.println("negotiated protocol was: " + protoString); assertEquals(protoString, "spdy/3.1"); - dummy.clear(); - SSLEngineResult res = engine.unwrap(unwrap, dummy); - dummy.flip(); - byte[] frame = new byte[dummy.remaining()]; - dummy.get(frame ); - Spdy3 spdy3 = new Spdy3(); - BufferedSource source = Okio.buffer(Okio.source(new ByteArrayInputStream(frame))); - FrameReader frameReader = spdy3.newReader(source, true); - ByteBufferList bb = new ByteBufferList(ByteBuffer.wrap(frame)); - assertTrue(frameReader.canProcessFrame(bb)); - - frameReader.nextFrame(new FrameReader.Handler() { - @Override - public void data(boolean inFinished, int streamId, BufferedSource source, int length) throws IOException { - - } - - @Override - public void headers(boolean outFinished, boolean inFinished, int streamId, int associatedStreamId, List<Header> headerBlock, HeadersMode headersMode) { - - } - - @Override - public void rstStream(int streamId, ErrorCode errorCode) { - - } - - @Override - public void settings(boolean clearPrevious, Settings settings) { - - } - - @Override - public void ackSettings() { - - } - - @Override - public void ping(boolean ack, int payload1, int payload2) { - - } - - @Override - public void goAway(int lastGoodStreamId, ErrorCode errorCode, ByteString debugData) { - - } - - @Override - public void windowUpdate(int streamId, long windowSizeIncrement) { - - } - - @Override - public void priority(int streamId, int streamDependency, int weight, boolean exclusive) { - - } - - @Override - public void pushPromise(int streamId, int promisedStreamId, List<Header> requestHeaders) throws IOException { - - } - - @Override - public void alternateService(int streamId, String origin, ByteString protocol, String host, int port, long maxAge) { - - } - }); - - + socket.close(); } } diff --git a/AndroidAsync/test/src/com/koushikdutta/async/test/FileTests.java b/AndroidAsync/test/src/com/koushikdutta/async/test/FileTests.java index 1d37239..4642bd2 100644 --- a/AndroidAsync/test/src/com/koushikdutta/async/test/FileTests.java +++ b/AndroidAsync/test/src/com/koushikdutta/async/test/FileTests.java @@ -1,5 +1,7 @@ package com.koushikdutta.async.test; +import android.test.AndroidTestCase; + import com.koushikdutta.async.AsyncServer; import com.koushikdutta.async.FileDataEmitter; import com.koushikdutta.async.future.Future; @@ -16,11 +18,11 @@ import java.util.concurrent.TimeUnit; /** * Created by koush on 5/22/13. */ -public class FileTests extends TestCase { +public class FileTests extends AndroidTestCase { public static final long TIMEOUT = 1000L; public void testFileDataEmitter() throws Exception { final Semaphore semaphore = new Semaphore(0); - File f = new File("/sdcard/test.txt"); + File f = getContext().getFileStreamPath("test.txt"); StreamUtility.writeFile(f, "hello world"); FileDataEmitter fdm = new FileDataEmitter(AsyncServer.getDefault(), f); final Md5 md5 = Md5.createInstance(); diff --git a/AndroidAsync/test/src/com/koushikdutta/async/test/Handshake.java b/AndroidAsync/test/src/com/koushikdutta/async/test/Handshake.java deleted file mode 100644 index 597436b..0000000 --- a/AndroidAsync/test/src/com/koushikdutta/async/test/Handshake.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.koushikdutta.async.test; - -import com.koushikdutta.async.http.spdy.Util; - -import java.security.Principal; -import java.security.cert.Certificate; -import java.security.cert.X509Certificate; -import java.util.Collections; -import java.util.List; - -import javax.net.ssl.SSLPeerUnverifiedException; -import javax.net.ssl.SSLSession; - -/** - * A record of a TLS handshake. For HTTPS clients, the client is <i>local</i> - * and the remote server is its <i>peer</i>. - * - * <p>This value object describes a completed handshake. Use {@link - * javax.net.ssl.SSLSocketFactory} to set policy for new handshakes. - */ -public final class Handshake { - private final String cipherSuite; - private final List<Certificate> peerCertificates; - private final List<Certificate> localCertificates; - - private Handshake( - String cipherSuite, List<Certificate> peerCertificates, List<Certificate> localCertificates) { - this.cipherSuite = cipherSuite; - this.peerCertificates = peerCertificates; - this.localCertificates = localCertificates; - } - - public static Handshake get(SSLSession session) { - String cipherSuite = session.getCipherSuite(); - if (cipherSuite == null) throw new IllegalStateException("cipherSuite == null"); - - Certificate[] peerCertificates; - try { - peerCertificates = session.getPeerCertificates(); - } catch (SSLPeerUnverifiedException ignored) { - peerCertificates = null; - } - List<Certificate> peerCertificatesList = peerCertificates != null - ? Util.immutableList(peerCertificates) - : Collections.<Certificate>emptyList(); - - Certificate[] localCertificates = session.getLocalCertificates(); - List<Certificate> localCertificatesList = localCertificates != null - ? Util.immutableList(localCertificates) - : Collections.<Certificate>emptyList(); - - return new Handshake(cipherSuite, peerCertificatesList, localCertificatesList); - } - - public static Handshake get( - String cipherSuite, List<Certificate> peerCertificates, List<Certificate> localCertificates) { - if (cipherSuite == null) throw new IllegalArgumentException("cipherSuite == null"); - return new Handshake(cipherSuite, Util.immutableList(peerCertificates), - Util.immutableList(localCertificates)); - } - - /** Returns a cipher suite name like "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA". */ - public String cipherSuite() { - return cipherSuite; - } - - /** Returns a possibly-empty list of certificates that identify the remote peer. */ - public List<Certificate> peerCertificates() { - return peerCertificates; - } - - /** Returns the remote peer's principle, or null if that peer is anonymous. */ - public Principal peerPrincipal() { - return !peerCertificates.isEmpty() - ? ((X509Certificate) peerCertificates.get(0)).getSubjectX500Principal() - : null; - } - - /** Returns a possibly-empty list of certificates that identify this peer. */ - public List<Certificate> localCertificates() { - return localCertificates; - } - - /** Returns the local principle, or null if this peer is anonymous. */ - public Principal localPrincipal() { - return !localCertificates.isEmpty() - ? ((X509Certificate) localCertificates.get(0)).getSubjectX500Principal() - : null; - } - - @Override public boolean equals(Object other) { - if (!(other instanceof Handshake)) return false; - Handshake that = (Handshake) other; - return cipherSuite.equals(that.cipherSuite) - && peerCertificates.equals(that.peerCertificates) - && localCertificates.equals(that.localCertificates); - } - - @Override public int hashCode() { - int result = 17; - result = 31 * result + cipherSuite.hashCode(); - result = 31 * result + peerCertificates.hashCode(); - result = 31 * result + localCertificates.hashCode(); - return result; - } -}
\ No newline at end of file diff --git a/AndroidAsync/test/src/com/koushikdutta/async/test/HttpClientTests.java b/AndroidAsync/test/src/com/koushikdutta/async/test/HttpClientTests.java index 5be77f9..4ba7c7d 100644 --- a/AndroidAsync/test/src/com/koushikdutta/async/test/HttpClientTests.java +++ b/AndroidAsync/test/src/com/koushikdutta/async/test/HttpClientTests.java @@ -2,6 +2,7 @@ package com.koushikdutta.async.test; import android.net.Uri; import android.os.Environment; +import android.test.AndroidTestCase; import android.text.TextUtils; import android.util.Log; @@ -20,17 +21,14 @@ import com.koushikdutta.async.http.AsyncHttpHead; import com.koushikdutta.async.http.AsyncHttpPost; import com.koushikdutta.async.http.AsyncHttpRequest; import com.koushikdutta.async.http.AsyncHttpResponse; -import com.koushikdutta.async.http.cache.ResponseCacheMiddleware; import com.koushikdutta.async.http.body.JSONObjectBody; +import com.koushikdutta.async.http.cache.ResponseCacheMiddleware; import com.koushikdutta.async.http.callback.HttpConnectCallback; -import com.koushikdutta.async.http.server.AsyncHttpServer; import com.koushikdutta.async.http.server.AsyncHttpServerRequest; import com.koushikdutta.async.http.server.AsyncHttpServerResponse; import com.koushikdutta.async.http.server.AsyncProxyServer; -import com.koushikdutta.async.http.server.HttpServerRequestCallback; import junit.framework.Assert; -import junit.framework.TestCase; import org.json.JSONObject; @@ -41,7 +39,7 @@ import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -public class HttpClientTests extends TestCase { +public class HttpClientTests extends AndroidTestCase { AsyncHttpClient client; AsyncServer server = new AsyncServer(); @@ -227,7 +225,7 @@ public class HttpClientTests extends TestCase { } public void testCache() throws Exception { - ResponseCacheMiddleware cache = ResponseCacheMiddleware.addCache(client, new File(Environment.getExternalStorageDirectory(), "AndroidAsyncTest"), 1024 * 1024 * 10); + ResponseCacheMiddleware cache = ResponseCacheMiddleware.addCache(client, new File(getContext().getFilesDir(), "AndroidAsyncTest"), 1024 * 1024 * 10); try { // clear the old cache cache.clear(); @@ -245,7 +243,8 @@ public class HttpClientTests extends TestCase { Future<File> fileFuture; public void testFileCancel() throws Exception { final Semaphore semaphore = new Semaphore(0); - fileFuture = client.executeFile(new AsyncHttpGet(github), "/sdcard/hello.txt", new AsyncHttpClient.FileCallback() { + 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(); @@ -274,7 +273,7 @@ public class HttpClientTests extends TestCase { } // Thread.sleep(1000); // assertTrue("timeout", semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS)); - assertFalse(new File("/sdcard/hello.txt").exists()); + assertFalse(f.exists()); } boolean wasProxied; diff --git a/AndroidAsync/test/src/com/koushikdutta/async/test/MultipartTests.java b/AndroidAsync/test/src/com/koushikdutta/async/test/MultipartTests.java index cace2a5..0a4a5a2 100644 --- a/AndroidAsync/test/src/com/koushikdutta/async/test/MultipartTests.java +++ b/AndroidAsync/test/src/com/koushikdutta/async/test/MultipartTests.java @@ -1,6 +1,7 @@ package com.koushikdutta.async.test; import android.os.Environment; +import android.test.AndroidTestCase; import com.koushikdutta.async.AsyncServer; import com.koushikdutta.async.ByteBufferList; @@ -26,7 +27,7 @@ import java.io.File; import java.io.FileOutputStream; import java.util.concurrent.TimeUnit; -public class MultipartTests extends TestCase { +public class MultipartTests extends AndroidTestCase { AsyncHttpServer httpServer; @Override @@ -81,7 +82,7 @@ public class MultipartTests extends TestCase { } public void testUpload() throws Exception { - File dummy = new File(Environment.getExternalStorageDirectory(), "AndroidAsync/dummy.txt"); + File dummy = getContext().getFileStreamPath("dummy.txt"); final String FIELD_VAL = "bar"; dummy.getParentFile().mkdirs(); FileOutputStream fout = new FileOutputStream(dummy); diff --git a/AndroidAsync/test/src/com/koushikdutta/async/test/OkHttpTest.java b/AndroidAsync/test/src/com/koushikdutta/async/test/OkHttpTest.java deleted file mode 100644 index 48fbf5b..0000000 --- a/AndroidAsync/test/src/com/koushikdutta/async/test/OkHttpTest.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.koushikdutta.async.test; - - -import android.test.AndroidTestCase; - -import com.koushikdutta.async.ByteBufferList; -import com.koushikdutta.async.http.Protocol; -import com.koushikdutta.async.util.Charsets; - -import org.conscrypt.OpenSSLProvider; - -import java.lang.reflect.Method; -import java.net.InetSocketAddress; -import java.net.Socket; -import java.nio.ByteBuffer; -import java.security.Security; - -import javax.net.SocketFactory; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSocket; - -public class OkHttpTest extends AndroidTestCase { - public void testOkHttp() throws Exception { -// Context context = getContext().getApplicationContext(); -// Context gms = context.createPackageContext("com.google.android.gms", Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY); -// gms -// .getClassLoader() -// .loadClass("com.google.android.gms.common.security.ProviderInstallerImpl") -// .getMethod("insertProvider", Context.class) -// .invoke(null, context); - Security.insertProviderAt(new OpenSSLProvider("MyNameBlah"), 1); - - Class<?> openSslSocketClass; - Method setUseSessionTickets; - Method setHostname; - openSslSocketClass = Class.forName("org.conscrypt.OpenSSLSocketImpl"); - setUseSessionTickets = openSslSocketClass.getMethod("setUseSessionTickets", boolean.class); - setHostname = openSslSocketClass.getMethod("setHostname", String.class); - Method trafficStatsTagSocket = null; - Method trafficStatsUntagSocket = null; - Class<?> trafficStats = Class.forName("android.net.TrafficStats"); - trafficStatsTagSocket = trafficStats.getMethod("tagSocket", Socket.class); - trafficStatsUntagSocket = trafficStats.getMethod("untagSocket", Socket.class); - - // Attempt to find Android 4.1+ APIs. - Method setNpnProtocols = null; - Method getNpnSelectedProtocol = null; - setNpnProtocols = openSslSocketClass.getMethod("setNpnProtocols", byte[].class); - getNpnSelectedProtocol = openSslSocketClass.getMethod("getNpnSelectedProtocol"); - - -// Platform p = Platform.get(); - - SSLContext ctx = SSLContext.getInstance("TLS"); - ctx.init(null, null, null); - Socket socket = SocketFactory.getDefault().createSocket(); - socket.connect(new InetSocketAddress("www.google.com", 443)); - socket = ctx.getSocketFactory().createSocket(socket, "www.google.com", 443, true); - SSLSocket sslSocket = (SSLSocket) socket; - - setUseSessionTickets.invoke(sslSocket, true); - setHostname.invoke(sslSocket, "www.google.com"); - setNpnProtocols.invoke(sslSocket, new Object[] { concatLengthPrefixed(Protocol.HTTP_1_1, Protocol.SPDY_3) }); - - - sslSocket.startHandshake(); - Handshake handshake = Handshake.get(sslSocket.getSession()); - - String proto = new String((byte[])getNpnSelectedProtocol.invoke(sslSocket)); - -// InputStream is = sslSocket.getInputStream(); -// StreamUtility.eat(is); - - System.out.println(proto); - } - - static byte[] concatLengthPrefixed(Protocol... protocols) { - ByteBuffer result = ByteBuffer.allocate(8192); - for (Protocol protocol: protocols) { - if (protocol == Protocol.HTTP_1_0) continue; // No HTTP/1.0 for NPN. - result.put((byte) protocol.toString().length()); - result.put(protocol.toString().getBytes(Charsets.UTF_8)); - } - result.flip(); - byte[] ret = new ByteBufferList(result).getAllByteArray(); - return ret; - } - -}
\ No newline at end of file |