use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method requestOnlyIfCachedWithUnhelpfulResponseCached.
@Test
public void requestOnlyIfCachedWithUnhelpfulResponseCached() throws IOException {
server.enqueue(new MockResponse().setBody("A"));
assertThat(get(server.url("/")).body().string()).isEqualTo("A");
Request request = new Request.Builder().url(server.url("/")).header("Cache-Control", "only-if-cached").build();
Response response = client.newCall(request).execute();
assertThat(response.body().source().exhausted()).isTrue();
assertThat(response.code()).isEqualTo(504);
assertThat(cache.requestCount()).isEqualTo(2);
assertThat(cache.networkCount()).isEqualTo(1);
assertThat(cache.hitCount()).isEqualTo(0);
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method varyMatchesRemovedRequestHeaderField.
@Test
public void varyMatchesRemovedRequestHeaderField() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Foo").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
Request request = new Request.Builder().url(server.url("/")).header("Foo", "bar").build();
Response fooresponse = client.newCall(request).execute();
assertThat(fooresponse.body().string()).isEqualTo("A");
assertThat(get(server.url("/")).body().string()).isEqualTo("B");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method conditionalMissUpdatesCache.
/**
* 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));
HttpUrl url = server.url("/");
assertThat(get(url).body().string()).isEqualTo("A");
assertThat(get(url).body().string()).isEqualTo("A");
assertThat(get(url).body().string()).isEqualTo("B");
assertThat(get(url).body().string()).isEqualTo("B");
assertThat(server.takeRequest().getHeader("If-None-Match")).isNull();
assertThat(server.takeRequest().getHeader("If-None-Match")).isEqualTo("v1");
assertThat(server.takeRequest().getHeader("If-None-Match")).isEqualTo("v1");
assertThat(server.takeRequest().getHeader("If-None-Match")).isEqualTo("v2");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class ConnectionCoalescingTest method skipsOnRedirectWhenCertificatePinningFails.
@Test
public void skipsOnRedirectWhenCertificatePinningFails() throws Exception {
CertificatePinner pinner = new CertificatePinner.Builder().add("san.com", "sha1/afwiKY3RxoMmLkuRW1l7QsPZTJPwDS2pdDROQjXw8ig=").build();
client = client.newBuilder().certificatePinner(pinner).build();
server.enqueue(new MockResponse().setResponseCode(301).addHeader("Location", url.newBuilder().host("san.com").build()));
server.enqueue(new MockResponse());
try {
execute(url);
fail("expected a failed attempt to connect");
} catch (SSLPeerUnverifiedException expected) {
}
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class ConnectionCoalescingTest method commonThenAlternative.
/**
* Test connecting to the main host then an alternative, although only subject alternative names
* are used if present no special consideration of common name.
*/
@Test
public void commonThenAlternative() throws Exception {
server.enqueue(new MockResponse());
server.enqueue(new MockResponse());
assert200Http2Response(execute(url), server.getHostName());
HttpUrl sanUrl = url.newBuilder().host("san.com").build();
assert200Http2Response(execute(sanUrl), "san.com");
assertThat(client.connectionPool().connectionCount()).isEqualTo(1);
}
Aggregations