Skip to content

Commit 1b577c9

Browse files
committed
More native OkHttp API tweaks.
1 parent ce1df3d commit 1b577c9

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

picasso/src/main/java/com/squareup/picasso/OkHttpDownloader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ private static OkHttpClient defaultOkHttpClient() {
2929
OkHttpClient client = new OkHttpClient();
3030
client.setConnectTimeout(Utils.DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
3131
client.setReadTimeout(Utils.DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
32+
client.setWriteTimeout(Utils.DEFAULT_WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
3233
return client;
3334
}
3435

@@ -94,7 +95,7 @@ protected final OkHttpClient getClient() {
9495
new com.squareup.okhttp.Request.Builder().url(uri.toString());
9596

9697
if (localCacheOnly) {
97-
requestBuilder.addHeader("Cache-Control", "only-if-cached,max-age=" + Integer.MAX_VALUE);
98+
requestBuilder.addHeader("Cache-Control", "only-if-cached,max-age=2147483647");
9899
}
99100

100101
com.squareup.okhttp.Response response = client.newCall(requestBuilder.build()).execute();

picasso/src/main/java/com/squareup/picasso/Utils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ final class Utils {
4949
static final String THREAD_PREFIX = "Picasso-";
5050
static final String THREAD_IDLE_NAME = THREAD_PREFIX + "Idle";
5151
static final int DEFAULT_READ_TIMEOUT_MILLIS = 20 * 1000; // 20s
52+
static final int DEFAULT_WRITE_TIMEOUT_MILLIS = 20 * 1000; // 20s
5253
static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 15 * 1000; // 15s
5354
private static final String PICASSO_CACHE = "picasso-cache";
5455
private static final int KEY_PADDING = 50; // Determined by exact science.

picasso/src/test/java/com/squareup/picasso/OkHttpDownloaderTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class OkHttpDownloaderTest {
4646
uri = Uri.parse(server.getUrl("/").toString());
4747
}
4848

49-
@Test public void responseSourceHeaderSetsResponseValue() throws Exception {
49+
@Test public void cachedResponse() throws Exception {
5050
server.enqueue(new MockResponse()
5151
.setHeader("Cache-Control", "max-age=31536000")
5252
.setBody("Hi"));
@@ -60,6 +60,21 @@ public class OkHttpDownloaderTest {
6060
assertThat(response2.cached).isTrue();
6161
}
6262

63+
@Test public void offlineStaleResponse() throws Exception {
64+
server.enqueue(new MockResponse()
65+
.setHeader("Cache-Control", "max-age=1")
66+
.setHeader("Expires", "Mon, 29 Dec 2014 21:44:55 GMT")
67+
.setBody("Hi"));
68+
69+
Downloader.Response response1 = downloader.load(uri, false);
70+
assertThat(response1.cached).isFalse();
71+
// Exhaust input stream to ensure response is cached.
72+
Okio.buffer(Okio.source(response1.getInputStream())).readByteArray();
73+
74+
Downloader.Response response2 = downloader.load(uri, true);
75+
assertThat(response2.cached).isTrue();
76+
}
77+
6378
@Test public void readsContentLengthHeader() throws Exception {
6479
server.enqueue(new MockResponse().addHeader("Content-Length", 1024));
6580

0 commit comments

Comments
 (0)