Skip to content

Commit

Permalink
Merge pull request square#1713 from square/jwilson_0620_test_1712
Browse files Browse the repository at this point in the history
Test that conditional misses update the cache.
  • Loading branch information
JakeWharton committed Jun 20, 2015
2 parents 037deb8 + 3bee7ac commit cf4b019
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions okhttp-tests/src/test/java/com/squareup/okhttp/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1644,7 +1644,7 @@ private RecordedRequest assertClientSuppliedCondition(MockResponse seed, String
}

@Test public void varyAsterisk() throws Exception {
server.enqueue( new MockResponse()
server.enqueue(new MockResponse()
.addHeader("Cache-Control: max-age=60")
.addHeader("Vary: *")
.setBody("A"));
Expand Down Expand Up @@ -1784,10 +1784,10 @@ public void assertCookies(URL url, String... expectedCookies) throws Exception {

@Test public void doNotCachePartialResponse() throws Exception {
assertNotCached(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_PARTIAL)
.addHeader("Date: " + formatDate(0, TimeUnit.HOURS))
.addHeader("Content-Range: bytes 100-100/200")
.addHeader("Cache-Control: max-age=60"));
.setResponseCode(HttpURLConnection.HTTP_PARTIAL)
.addHeader("Date: " + formatDate(0, TimeUnit.HOURS))
.addHeader("Content-Range: bytes 100-100/200")
.addHeader("Cache-Control: max-age=60"));
}

@Test public void conditionalHitUpdatesCache() throws Exception {
Expand Down Expand Up @@ -2127,6 +2127,31 @@ public void assertCookies(URL url, String... expectedCookies) throws Exception {
}
}

/** Test https://github.com/square/okhttp/issues/1712. */
@Test public void conditionalMissUpdatesCache() throws Exception {
server.enqueue(new MockResponse()
.addHeader("ETag: v1")
.setBody("A"));
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
server.enqueue(new MockResponse()
.addHeader("ETag: v2")
.setBody("B"));
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));

URL url = server.getUrl("/");
assertEquals("A", get(url).body().string());
assertEquals("A", get(url).body().string());
assertEquals("B", get(url).body().string());
assertEquals("B", get(url).body().string());

assertEquals(null, server.takeRequest().getHeader("If-None-Match"));
assertEquals("v1", server.takeRequest().getHeader("If-None-Match"));
assertEquals("v1", server.takeRequest().getHeader("If-None-Match"));
assertEquals("v2", server.takeRequest().getHeader("If-None-Match"));
}

private Response get(URL url) throws IOException {
Request request = new Request.Builder()
.url(url)
Expand Down

0 comments on commit cf4b019

Please sign in to comment.