aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/com/android/volley/CacheDispatcherTest.java19
-rw-r--r--src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java17
-rw-r--r--src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java9
-rw-r--r--src/test/java/com/android/volley/toolbox/HurlStackTest.java104
4 files changed, 136 insertions, 13 deletions
diff --git a/src/test/java/com/android/volley/CacheDispatcherTest.java b/src/test/java/com/android/volley/CacheDispatcherTest.java
index 2592a0b..aef6785 100644
--- a/src/test/java/com/android/volley/CacheDispatcherTest.java
+++ b/src/test/java/com/android/volley/CacheDispatcherTest.java
@@ -140,6 +140,25 @@ public class CacheDispatcherTest {
assertSame(entry, mRequest.getCacheEntry());
}
+ // An fresh cache hit with parse error, does not post a response and queues to the network.
+ @Test
+ public void freshCacheHit_parseError() throws Exception {
+ Request request = mock(Request.class);
+ when(request.parseNetworkResponse(any(NetworkResponse.class)))
+ .thenReturn(Response.error(new ParseError()));
+ when(request.getCacheKey()).thenReturn("cache/key");
+ Cache.Entry entry = CacheTestUtils.makeRandomCacheEntry(null, false, false);
+ when(mCache.get(anyString())).thenReturn(entry);
+
+ mDispatcher.processRequest(request);
+
+ verifyNoResponse(mDelivery);
+ verify(mNetworkQueue).put(request);
+ assertNull(request.getCacheEntry());
+ verify(mCache).invalidate("cache/key", true);
+ verify(request).addMarker("cache-parsing-failed");
+ }
+
@Test
public void duplicateCacheMiss() throws Exception {
StringRequest secondRequest =
diff --git a/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java b/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java
index e499a37..ccf68fa 100644
--- a/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java
+++ b/src/test/java/com/android/volley/toolbox/DiskBasedCacheTest.java
@@ -587,11 +587,28 @@ public class DiskBasedCacheTest {
public void publicMethods() throws Exception {
// Catch-all test to find API-breaking changes.
assertNotNull(DiskBasedCache.class.getConstructor(File.class, int.class));
+ assertNotNull(
+ DiskBasedCache.class.getConstructor(DiskBasedCache.FileSupplier.class, int.class));
assertNotNull(DiskBasedCache.class.getConstructor(File.class));
+ assertNotNull(DiskBasedCache.class.getConstructor(DiskBasedCache.FileSupplier.class));
assertNotNull(DiskBasedCache.class.getMethod("getFileForKey", String.class));
}
+ @Test
+ public void initializeIfRootDirectoryDeleted() {
+ temporaryFolder.delete();
+
+ Cache.Entry entry = randomData(101);
+ cache.put("key1", entry);
+
+ assertThat(cache.get("key1"), is(nullValue()));
+
+ // confirm that we can now store entries
+ cache.put("key2", entry);
+ assertThatEntriesAreEqual(cache.get("key2"), entry);
+ }
+
/* Test helpers */
private void assertThatEntriesAreEqual(Cache.Entry actual, Cache.Entry expected) {
diff --git a/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java b/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java
index 9b670f9..7780c3e 100644
--- a/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java
+++ b/src/test/java/com/android/volley/toolbox/HttpHeaderParserTest.java
@@ -67,6 +67,12 @@ public class HttpHeaderParserTest {
}
@Test
+ public void parseCacheHeaders_nullHeaders() {
+ response = new NetworkResponse(0, null, null, false);
+ assertNull(HttpHeaderParser.parseCacheHeaders(response));
+ }
+
+ @Test
public void parseCacheHeaders_headersSet() {
headers.put("MyCustomHeader", "42");
@@ -282,6 +288,9 @@ public class HttpHeaderParserTest {
// None specified, extra semicolon
headers.put("Content-Type", "text/plain;");
assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers));
+
+ // No headers, use default charset
+ assertEquals("utf-8", HttpHeaderParser.parseCharset(null, "utf-8"));
}
@Test
diff --git a/src/test/java/com/android/volley/toolbox/HurlStackTest.java b/src/test/java/com/android/volley/toolbox/HurlStackTest.java
index c1fc92d..7508244 100644
--- a/src/test/java/com/android/volley/toolbox/HurlStackTest.java
+++ b/src/test/java/com/android/volley/toolbox/HurlStackTest.java
@@ -17,6 +17,7 @@
package com.android.volley.toolbox;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.never;
@@ -24,11 +25,16 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.android.volley.Header;
+import com.android.volley.Request;
import com.android.volley.Request.Method;
import com.android.volley.mock.TestRequest;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.FilterInputStream;
+import java.io.FilterOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
@@ -62,6 +68,26 @@ public class HurlStackTest {
protected HttpURLConnection createConnection(URL url) {
return mMockConnection;
}
+
+ @Override
+ protected InputStream createInputStream(
+ Request<?> request, HttpURLConnection connection) {
+ return new MonitoringInputStream(
+ super.createInputStream(request, connection));
+ }
+
+ @Override
+ protected OutputStream createOutputStream(
+ Request<?> request, HttpURLConnection connection, int length)
+ throws IOException {
+ if (request instanceof MonitoredRequest) {
+ return new MonitoringOutputStream(
+ super.createOutputStream(request, connection, length),
+ (MonitoredRequest) request,
+ length);
+ }
+ return super.createOutputStream(request, connection, length);
+ }
};
}
@@ -70,7 +96,7 @@ public class HurlStackTest {
TestRequest.DeprecatedGet request = new TestRequest.DeprecatedGet();
assertEquals(request.getMethod(), Method.DEPRECATED_GET_OR_POST);
- HurlStack.setConnectionParametersForRequest(mMockConnection, request);
+ mHurlStack.setConnectionParametersForRequest(mMockConnection, request);
verify(mMockConnection, never()).setRequestMethod(anyString());
verify(mMockConnection, never()).setDoOutput(true);
}
@@ -80,7 +106,7 @@ public class HurlStackTest {
TestRequest.DeprecatedPost request = new TestRequest.DeprecatedPost();
assertEquals(request.getMethod(), Method.DEPRECATED_GET_OR_POST);
- HurlStack.setConnectionParametersForRequest(mMockConnection, request);
+ mHurlStack.setConnectionParametersForRequest(mMockConnection, request);
verify(mMockConnection).setRequestMethod("POST");
verify(mMockConnection).setDoOutput(true);
}
@@ -90,7 +116,7 @@ public class HurlStackTest {
TestRequest.Get request = new TestRequest.Get();
assertEquals(request.getMethod(), Method.GET);
- HurlStack.setConnectionParametersForRequest(mMockConnection, request);
+ mHurlStack.setConnectionParametersForRequest(mMockConnection, request);
verify(mMockConnection).setRequestMethod("GET");
verify(mMockConnection, never()).setDoOutput(true);
}
@@ -100,7 +126,7 @@ public class HurlStackTest {
TestRequest.Post request = new TestRequest.Post();
assertEquals(request.getMethod(), Method.POST);
- HurlStack.setConnectionParametersForRequest(mMockConnection, request);
+ mHurlStack.setConnectionParametersForRequest(mMockConnection, request);
verify(mMockConnection).setRequestMethod("POST");
verify(mMockConnection, never()).setDoOutput(true);
}
@@ -110,7 +136,7 @@ public class HurlStackTest {
TestRequest.PostWithBody request = new TestRequest.PostWithBody();
assertEquals(request.getMethod(), Method.POST);
- HurlStack.setConnectionParametersForRequest(mMockConnection, request);
+ mHurlStack.setConnectionParametersForRequest(mMockConnection, request);
verify(mMockConnection).setRequestMethod("POST");
verify(mMockConnection).setDoOutput(true);
}
@@ -120,7 +146,7 @@ public class HurlStackTest {
TestRequest.Put request = new TestRequest.Put();
assertEquals(request.getMethod(), Method.PUT);
- HurlStack.setConnectionParametersForRequest(mMockConnection, request);
+ mHurlStack.setConnectionParametersForRequest(mMockConnection, request);
verify(mMockConnection).setRequestMethod("PUT");
verify(mMockConnection, never()).setDoOutput(true);
}
@@ -130,7 +156,7 @@ public class HurlStackTest {
TestRequest.PutWithBody request = new TestRequest.PutWithBody();
assertEquals(request.getMethod(), Method.PUT);
- HurlStack.setConnectionParametersForRequest(mMockConnection, request);
+ mHurlStack.setConnectionParametersForRequest(mMockConnection, request);
verify(mMockConnection).setRequestMethod("PUT");
verify(mMockConnection).setDoOutput(true);
}
@@ -140,7 +166,7 @@ public class HurlStackTest {
TestRequest.Delete request = new TestRequest.Delete();
assertEquals(request.getMethod(), Method.DELETE);
- HurlStack.setConnectionParametersForRequest(mMockConnection, request);
+ mHurlStack.setConnectionParametersForRequest(mMockConnection, request);
verify(mMockConnection).setRequestMethod("DELETE");
verify(mMockConnection, never()).setDoOutput(true);
}
@@ -150,7 +176,7 @@ public class HurlStackTest {
TestRequest.Head request = new TestRequest.Head();
assertEquals(request.getMethod(), Method.HEAD);
- HurlStack.setConnectionParametersForRequest(mMockConnection, request);
+ mHurlStack.setConnectionParametersForRequest(mMockConnection, request);
verify(mMockConnection).setRequestMethod("HEAD");
verify(mMockConnection, never()).setDoOutput(true);
}
@@ -160,7 +186,7 @@ public class HurlStackTest {
TestRequest.Options request = new TestRequest.Options();
assertEquals(request.getMethod(), Method.OPTIONS);
- HurlStack.setConnectionParametersForRequest(mMockConnection, request);
+ mHurlStack.setConnectionParametersForRequest(mMockConnection, request);
verify(mMockConnection).setRequestMethod("OPTIONS");
verify(mMockConnection, never()).setDoOutput(true);
}
@@ -170,7 +196,7 @@ public class HurlStackTest {
TestRequest.Trace request = new TestRequest.Trace();
assertEquals(request.getMethod(), Method.TRACE);
- HurlStack.setConnectionParametersForRequest(mMockConnection, request);
+ mHurlStack.setConnectionParametersForRequest(mMockConnection, request);
verify(mMockConnection).setRequestMethod("TRACE");
verify(mMockConnection, never()).setDoOutput(true);
}
@@ -180,7 +206,7 @@ public class HurlStackTest {
TestRequest.Patch request = new TestRequest.Patch();
assertEquals(request.getMethod(), Method.PATCH);
- HurlStack.setConnectionParametersForRequest(mMockConnection, request);
+ mHurlStack.setConnectionParametersForRequest(mMockConnection, request);
verify(mMockConnection).setRequestMethod("PATCH");
verify(mMockConnection, never()).setDoOutput(true);
}
@@ -190,7 +216,7 @@ public class HurlStackTest {
TestRequest.PatchWithBody request = new TestRequest.PatchWithBody();
assertEquals(request.getMethod(), Method.PATCH);
- HurlStack.setConnectionParametersForRequest(mMockConnection, request);
+ mHurlStack.setConnectionParametersForRequest(mMockConnection, request);
verify(mMockConnection).setRequestMethod("PATCH");
verify(mMockConnection).setDoOutput(true);
}
@@ -256,4 +282,56 @@ public class HurlStackTest {
expected.add(new Header("HeaderB", "ValueB_2"));
assertEquals(expected, result);
}
+
+ @Test
+ public void interceptResponseStream() throws Exception {
+ when(mMockConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
+ when(mMockConnection.getInputStream())
+ .thenReturn(new ByteArrayInputStream("hello".getBytes(StandardCharsets.UTF_8)));
+ HttpResponse response =
+ mHurlStack.executeRequest(
+ new TestRequest.Get(), Collections.<String, String>emptyMap());
+ assertTrue(response.getContent() instanceof MonitoringInputStream);
+ }
+
+ @Test
+ public void interceptRequestStream() throws Exception {
+ MonitoredRequest request = new MonitoredRequest();
+ mHurlStack.executeRequest(request, Collections.<String, String>emptyMap());
+ assertTrue(request.totalRequestBytes > 0);
+ assertEquals(request.totalRequestBytes, request.requestBytesRead);
+ }
+
+ private static class MonitoringInputStream extends FilterInputStream {
+ private MonitoringInputStream(InputStream in) {
+ super(in);
+ }
+ }
+
+ private static class MonitoringOutputStream extends FilterOutputStream {
+ private MonitoredRequest request;
+
+ private MonitoringOutputStream(OutputStream out, MonitoredRequest request, int length) {
+ super(out);
+ this.request = request;
+ this.request.totalRequestBytes = length;
+ }
+
+ @Override
+ public void write(int b) throws IOException {
+ this.request.requestBytesRead++;
+ out.write(b);
+ }
+
+ @Override
+ public void write(byte[] b, int off, int len) throws IOException {
+ this.request.requestBytesRead += len;
+ out.write(b, off, len);
+ }
+ }
+
+ private static class MonitoredRequest extends TestRequest.PostWithBody {
+ int requestBytesRead = 0;
+ int totalRequestBytes = 0;
+ }
}