aboutsummaryrefslogtreecommitdiffstats
path: root/okhttp-android-support/src/test
diff options
context:
space:
mode:
authorNeil Fuller <nfuller@google.com>2015-04-13 13:06:22 +0100
committerNeil Fuller <nfuller@google.com>2015-04-16 11:44:31 +0100
commita2cab72aa5ff730ba2ae987b45398faafffeb505 (patch)
tree283de306182e8f1faff93d4e6515298539d8c21d /okhttp-android-support/src/test
parent9fa0698523b8a573b8862c0e62c533be8bd53bda (diff)
downloadplatform_external_okhttp-a2cab72aa5ff730ba2ae987b45398faafffeb505.tar.gz
platform_external_okhttp-a2cab72aa5ff730ba2ae987b45398faafffeb505.tar.bz2
platform_external_okhttp-a2cab72aa5ff730ba2ae987b45398faafffeb505.zip
Roll-up of upstream OkHttp and Okio changes
OkHttp: From b609edd07864d7191dcda8ba1f6c833c9fe170ad to b40f99a950cb407eff52537a97420bd253a64f63 Okio: From 654ddf5e8f6311fda77e429c22d5e0e15f713b8d to b5811711b141b230e4e58f577c79cfbf4c2d4028 Both "to" are head as of 20150413. Patches applied cleanly without conflicts. This submission will break some CTS tests due to https://github.com/square/okhttp/issues/1552 Solutions will be made upstream and patched in. The CTS tests broken are related to SPDY/HTTP2 which are not used by Android's embedded OkHttp. Change-Id: I84d55b6f5c8dbc05148e86bd9421a2c393b563d4
Diffstat (limited to 'okhttp-android-support/src/test')
-rw-r--r--okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/JavaApiConverterTest.java33
-rw-r--r--okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/ResponseCacheTest.java44
2 files changed, 74 insertions, 3 deletions
diff --git a/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/JavaApiConverterTest.java b/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/JavaApiConverterTest.java
index d5dfcd8..7766da3 100644
--- a/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/JavaApiConverterTest.java
+++ b/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/JavaApiConverterTest.java
@@ -233,6 +233,30 @@ public class JavaApiConverterTest {
assertNull(response.handshake());
}
+ /** Test for https://code.google.com/p/android/issues/detail?id=160522 */
+ @Test public void createOkResponse_fromCacheResponseWithMissingStatusLine() throws Exception {
+ URI uri = new URI("http://foo/bar");
+ Request request = new Request.Builder().url(uri.toURL()).build();
+ CacheResponse cacheResponse = new CacheResponse() {
+ @Override public Map<String, List<String>> getHeaders() throws IOException {
+ Map<String, List<String>> headers = new HashMap<>();
+ // Headers is deliberately missing an entry with a null key.
+ headers.put("xyzzy", Arrays.asList("bar", "baz"));
+ return headers;
+ }
+
+ @Override public InputStream getBody() throws IOException {
+ return null; // Should never be called
+ }
+ };
+
+ try {
+ JavaApiConverter.createOkResponse(request, cacheResponse);
+ fail();
+ } catch (IOException expected) {
+ }
+ }
+
@Test public void createOkResponse_fromSecureCacheResponse() throws Exception {
final String statusLine = "HTTP/1.1 200 Fantastic";
final Principal localPrincipal = LOCAL_CERT.getSubjectX500Principal();
@@ -669,15 +693,18 @@ public class JavaApiConverterTest {
assertEquals(Arrays.asList("value2"), okHeaders.values("key2"));
}
- @Test public void extractStatusLine() {
+ @Test public void extractStatusLine() throws Exception {
Map<String, List<String>> javaResponseHeaders = new HashMap<>();
javaResponseHeaders.put(null, Arrays.asList("StatusLine"));
javaResponseHeaders.put("key1", Arrays.asList("value1_1", "value1_2"));
javaResponseHeaders.put("key2", Arrays.asList("value2"));
assertEquals("StatusLine", JavaApiConverter.extractStatusLine(javaResponseHeaders));
- assertNull(
- JavaApiConverter.extractStatusLine(Collections.<String, List<String>>emptyMap()));
+ try {
+ JavaApiConverter.extractStatusLine(Collections.<String, List<String>>emptyMap());
+ fail();
+ } catch (IOException expected) {
+ }
}
private URL configureServer(MockResponse mockResponse) throws Exception {
diff --git a/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/ResponseCacheTest.java b/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/ResponseCacheTest.java
index 3c91fb5..18956a3 100644
--- a/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/ResponseCacheTest.java
+++ b/okhttp-android-support/src/test/java/com/squareup/okhttp/internal/huc/ResponseCacheTest.java
@@ -40,12 +40,14 @@ import java.net.CacheResponse;
import java.net.CookieManager;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
+import java.net.ProtocolException;
import java.net.ResponseCache;
import java.net.SecureCacheResponse;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
+import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.security.cert.Certificate;
import java.util.ArrayList;
@@ -1304,6 +1306,48 @@ public final class ResponseCacheTest {
}
/**
+ * Fail if a badly-behaved cache returns a null status line header.
+ * https://code.google.com/p/android/issues/detail?id=160522
+ */
+ @Test public void responseCacheReturnsNullStatusLine() throws Exception {
+ String cachedContentString = "Hello";
+ final byte[] cachedContent = cachedContentString.getBytes(StandardCharsets.US_ASCII);
+
+ Internal.instance.setCache(client, new CacheAdapter(new AbstractResponseCache() {
+ @Override
+ public CacheResponse get(URI uri, String requestMethod,
+ Map<String, List<String>> requestHeaders)
+ throws IOException {
+ return new CacheResponse() {
+ @Override public Map<String, List<String>> getHeaders() throws IOException {
+ String contentType = "text/plain";
+ Map<String, List<String>> headers = new HashMap<>();
+ headers.put("Content-Length", Arrays.asList(Integer.toString(cachedContent.length)));
+ headers.put("Content-Type", Arrays.asList(contentType));
+ headers.put("Expires", Arrays.asList(formatDate(-1, TimeUnit.HOURS)));
+ headers.put("Cache-Control", Arrays.asList("max-age=60"));
+ // Crucially, the header with a null key is missing, which renders the cache response
+ // unusable because OkHttp only caches responses with cacheable response codes.
+ return headers;
+ }
+
+ @Override public InputStream getBody() throws IOException {
+ return new ByteArrayInputStream(cachedContent);
+ }
+ };
+ }
+ }));
+ HttpURLConnection connection = openConnection(server.getUrl("/"));
+ // If there was no status line from the cache an exception will be thrown. No network request
+ // should be made.
+ try {
+ connection.getResponseCode();
+ fail();
+ } catch (ProtocolException expected) {
+ }
+ }
+
+ /**
* @param delta the offset from the current date to use. Negative
* values yield dates in the past; positive values yield dates in the
* future.