use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method notModifiedSpecifiesEncoding.
@Test
public void notModifiedSpecifiesEncoding() throws Exception {
server.enqueue(new MockResponse().setBody(gzip("ABCABCABC")).addHeader("Content-Encoding: gzip").addHeader("Last-Modified: " + formatDate(-2, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(-1, TimeUnit.HOURS)));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED).addHeader("Content-Encoding: gzip"));
server.enqueue(new MockResponse().setBody("DEFDEFDEF"));
assertThat(get(server.url("/")).body().string()).isEqualTo("ABCABCABC");
assertThat(get(server.url("/")).body().string()).isEqualTo("ABCABCABC");
assertThat(get(server.url("/")).body().string()).isEqualTo("DEFDEFDEF");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method varyMultipleFieldsWithNoMatch.
@Test
public void varyMultipleFieldsWithNoMatch() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language, Accept-Charset").addHeader("Vary: Accept-Encoding").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
HttpUrl url = server.url("/");
Request frRequest = new Request.Builder().url(url).header("Accept-Language", "fr-CA").header("Accept-Charset", "UTF-8").header("Accept-Encoding", "identity").build();
Response frResponse = client.newCall(frRequest).execute();
assertThat(frResponse.body().string()).isEqualTo("A");
Request enRequest = new Request.Builder().url(url).header("Accept-Language", "en-CA").header("Accept-Charset", "UTF-8").header("Accept-Encoding", "identity").build();
Response enResponse = client.newCall(enRequest).execute();
assertThat(enResponse.body().string()).isEqualTo("B");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method redirectToCachedResult.
@Test
public void redirectToCachedResult() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("ABC"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_PERM).addHeader("Location: /foo"));
server.enqueue(new MockResponse().setBody("DEF"));
Request request1 = new Request.Builder().url(server.url("/foo")).build();
Response response1 = client.newCall(request1).execute();
assertThat(response1.body().string()).isEqualTo("ABC");
RecordedRequest recordedRequest1 = server.takeRequest();
assertThat(recordedRequest1.getRequestLine()).isEqualTo("GET /foo HTTP/1.1");
assertThat(recordedRequest1.getSequenceNumber()).isEqualTo(0);
Request request2 = new Request.Builder().url(server.url("/bar")).build();
Response response2 = client.newCall(request2).execute();
assertThat(response2.body().string()).isEqualTo("ABC");
RecordedRequest recordedRequest2 = server.takeRequest();
assertThat(recordedRequest2.getRequestLine()).isEqualTo("GET /bar HTTP/1.1");
assertThat(recordedRequest2.getSequenceNumber()).isEqualTo(1);
// an unrelated request should reuse the pooled connection
Request request3 = new Request.Builder().url(server.url("/baz")).build();
Response response3 = client.newCall(request3).execute();
assertThat(response3.body().string()).isEqualTo("DEF");
RecordedRequest recordedRequest3 = server.takeRequest();
assertThat(recordedRequest3.getRequestLine()).isEqualTo("GET /baz HTTP/1.1");
assertThat(recordedRequest3.getSequenceNumber()).isEqualTo(2);
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method varyMultipleFieldsWithMatch.
@Test
public void varyMultipleFieldsWithMatch() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language, Accept-Charset").addHeader("Vary: Accept-Encoding").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
HttpUrl url = server.url("/");
Request request = new Request.Builder().url(url).header("Accept-Language", "fr-CA").header("Accept-Charset", "UTF-8").header("Accept-Encoding", "identity").build();
Response response1 = client.newCall(request).execute();
assertThat(response1.body().string()).isEqualTo("A");
Request request1 = new Request.Builder().url(url).header("Accept-Language", "fr-CA").header("Accept-Charset", "UTF-8").header("Accept-Encoding", "identity").build();
Response response2 = client.newCall(request1).execute();
assertThat(response2.body().string()).isEqualTo("A");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method pragmaNoCacheAndExpirationDateInTheFuture.
@Test
public void pragmaNoCacheAndExpirationDateInTheFuture() throws Exception {
String lastModifiedDate = formatDate(-2, TimeUnit.HOURS);
RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).addHeader("Pragma: no-cache"));
assertThat(conditionalRequest.getHeader("If-Modified-Since")).isEqualTo(lastModifiedDate);
}
Aggregations