Skip to content

Commit

Permalink
Merge pull request square#1113 from square/jwilson_1101_not_shared
Browse files Browse the repository at this point in the history
Change the cache to have non-shared semantics.
  • Loading branch information
swankjesse committed Nov 1, 2014
2 parents 20929e3 + 112f020 commit 6e7f147
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 103 deletions.
34 changes: 1 addition & 33 deletions okhttp-tests/src/test/java/com/squareup/okhttp/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1216,40 +1216,8 @@ private RecordedRequest assertClientSuppliedCondition(MockResponse seed, String
assertEquals("", response.body().string());
}

@Test public void authorizationRequestHeaderPreventsCaching() throws Exception {
@Test public void authorizationRequestFullyCached() throws Exception {
server.enqueue(new MockResponse()
.addHeader("Last-Modified: " + formatDate(-2, TimeUnit.MINUTES))
.addHeader("Cache-Control: max-age=60")
.setBody("A"));
server.enqueue(new MockResponse()
.setBody("B"));

URL url = server.getUrl("/");
Request request = new Request.Builder()
.url(url)
.header("Authorization", "password")
.build();
Response response = client.newCall(request).execute();
assertEquals("A", response.body().string());
assertEquals("B", get(url).body().string());
}

@Test public void authorizationResponseCachedWithSMaxAge() throws Exception {
assertAuthorizationRequestFullyCached(
new MockResponse().addHeader("Cache-Control: s-maxage=60"));
}

@Test public void authorizationResponseCachedWithPublic() throws Exception {
assertAuthorizationRequestFullyCached(new MockResponse().addHeader("Cache-Control: public"));
}

@Test public void authorizationResponseCachedWithMustRevalidate() throws Exception {
assertAuthorizationRequestFullyCached(
new MockResponse().addHeader("Cache-Control: must-revalidate"));
}

public void assertAuthorizationRequestFullyCached(MockResponse mockResponse) throws Exception {
server.enqueue(mockResponse
.addHeader("Cache-Control: max-age=60")
.setBody("A"));
server.enqueue(new MockResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1098,36 +1098,8 @@ private RecordedRequest assertClientSuppliedCondition(MockResponse seed, String
assertEquals("", readAscii(connection));
}

@Test public void authorizationRequestHeaderPreventsCaching() throws Exception {
server.enqueue(
new MockResponse().addHeader("Last-Modified: " + formatDate(-2, TimeUnit.MINUTES))
.addHeader("Cache-Control: max-age=60")
.setBody("A"));
server.enqueue(new MockResponse().setBody("B"));

URL url = server.getUrl("/");
URLConnection connection = client.open(url);
connection.addRequestProperty("Authorization", "password");
assertEquals("A", readAscii(connection));
assertEquals("B", readAscii(client.open(url)));
}

@Test public void authorizationResponseCachedWithSMaxAge() throws Exception {
assertAuthorizationRequestFullyCached(
new MockResponse().addHeader("Cache-Control: s-maxage=60"));
}

@Test public void authorizationResponseCachedWithPublic() throws Exception {
assertAuthorizationRequestFullyCached(new MockResponse().addHeader("Cache-Control: public"));
}

@Test public void authorizationResponseCachedWithMustRevalidate() throws Exception {
assertAuthorizationRequestFullyCached(
new MockResponse().addHeader("Cache-Control: must-revalidate"));
}

public void assertAuthorizationRequestFullyCached(MockResponse response) throws Exception {
server.enqueue(response.addHeader("Cache-Control: max-age=60").setBody("A"));
@Test public void authorizationRequestFullyCached() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));

URL url = server.getUrl("/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -917,36 +917,8 @@ private RecordedRequest assertClientSuppliedCondition(MockResponse seed, String
assertEquals("", readAscii(connection));
}

@Test public void authorizationRequestHeaderPreventsCaching() throws Exception {
server.enqueue(
new MockResponse().addHeader("Last-Modified: " + formatDate(-2, TimeUnit.MINUTES))
.addHeader("Cache-Control: max-age=60")
.setBody("A"));
server.enqueue(new MockResponse().setBody("B"));

URL url = server.getUrl("/");
URLConnection connection = openConnection(url);
connection.addRequestProperty("Authorization", "password");
assertEquals("A", readAscii(connection));
assertEquals("B", readAscii(openConnection(url)));
}

@Test public void authorizationResponseCachedWithSMaxAge() throws Exception {
assertAuthorizationRequestFullyCached(
new MockResponse().addHeader("Cache-Control: s-maxage=60"));
}

@Test public void authorizationResponseCachedWithPublic() throws Exception {
assertAuthorizationRequestFullyCached(new MockResponse().addHeader("Cache-Control: public"));
}

@Test public void authorizationResponseCachedWithMustRevalidate() throws Exception {
assertAuthorizationRequestFullyCached(
new MockResponse().addHeader("Cache-Control: must-revalidate"));
}

public void assertAuthorizationRequestFullyCached(MockResponse response) throws Exception {
server.enqueue(response.addHeader("Cache-Control: max-age=60").setBody("A"));
@Test public void authorizationRequestFullyCached() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));

URL url = server.getUrl("/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,8 @@ public static boolean isCacheable(Response response, Request request) {
return false;
}

// Responses to authorized requests aren't cacheable unless they include
// a 'public', 'must-revalidate' or 's-maxage' directive.
CacheControl responseCaching = response.cacheControl();
if (request.header("Authorization") != null
&& !responseCaching.isPublic()
&& !responseCaching.mustRevalidate()
&& responseCaching.sMaxAgeSeconds() == -1) {
return false;
}

// A 'no-store' directive on request or response prevents the response from being cached.
CacheControl responseCaching = response.cacheControl();
CacheControl requestCaching = request.cacheControl();
if (responseCaching.noStore() || requestCaching.noStore()) {
return false;
Expand Down

0 comments on commit 6e7f147

Please sign in to comment.