use of okhttp3.HttpUrl in project okhttp by square.
the class CacheTest method authorizationRequestFullyCached.
@Test
public void authorizationRequestFullyCached() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
HttpUrl url = server.url("/");
Request request = new Request.Builder().url(url).header("Authorization", "password").build();
Response response = client.newCall(request).execute();
assertEquals("A", response.body().string());
assertEquals("A", get(url).body().string());
}
use of okhttp3.HttpUrl in project okhttp by square.
the class CallTest method httpsWithIpAddress.
@Test
public void httpsWithIpAddress() throws Exception {
String localIpAddress = InetAddress.getLoopbackAddress().getHostAddress();
// Create a certificate with an IP address in the subject alt name.
HeldCertificate heldCertificate = new HeldCertificate.Builder().commonName("example.com").subjectAlternativeName(localIpAddress).build();
SslClient sslClient = new SslClient.Builder().certificateChain(heldCertificate.keyPair, heldCertificate.certificate).addTrustedCertificate(heldCertificate.certificate).build();
// Use that certificate on the server and trust it on the client.
server.useHttps(sslClient.socketFactory, false);
client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).protocols(Collections.singletonList(Protocol.HTTP_1_1)).build();
// Make a request.
server.enqueue(new MockResponse());
HttpUrl url = server.url("/").newBuilder().host(localIpAddress).build();
Request request = new Request.Builder().url(url).build();
executeSynchronously(request).assertCode(200);
// Confirm that the IP address was used in the host header.
RecordedRequest recordedRequest = server.takeRequest();
assertEquals(localIpAddress + ":" + server.getPort(), recordedRequest.getHeader("Host"));
}
use of okhttp3.HttpUrl in project okhttp by square.
the class CacheTest method varyMatchesChangedRequestHeaderField.
@Test
public void varyMatchesChangedRequestHeaderField() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
HttpUrl url = server.url("/");
Request frRequest = new Request.Builder().url(url).header("Accept-Language", "fr-CA").build();
Response frResponse = client.newCall(frRequest).execute();
assertEquals("A", frResponse.body().string());
Request enRequest = new Request.Builder().url(url).header("Accept-Language", "en-US").build();
Response enResponse = client.newCall(enRequest).execute();
assertEquals("B", enResponse.body().string());
}
use of okhttp3.HttpUrl in project okhttp by square.
the class CacheTest method varyFieldsAreCaseInsensitive.
@Test
public void varyFieldsAreCaseInsensitive() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: ACCEPT-LANGUAGE").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
HttpUrl url = server.url("/");
Request request = new Request.Builder().url(url).header("Accept-Language", "fr-CA").build();
Response response1 = client.newCall(request).execute();
assertEquals("A", response1.body().string());
Request request1 = new Request.Builder().url(url).header("accept-language", "fr-CA").build();
Response response2 = client.newCall(request1).execute();
assertEquals("A", response2.body().string());
}
use of okhttp3.HttpUrl in project okhttp by square.
the class CacheTest method postInvalidatesCacheWithUncacheableResponse.
@Test
public void postInvalidatesCacheWithUncacheableResponse() throws Exception {
// 1. seed the cache
// 2. invalidate it with uncacheable response
// 3. expect a cache miss
server.enqueue(new MockResponse().setBody("A").addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)));
server.enqueue(new MockResponse().setBody("B").setResponseCode(500));
server.enqueue(new MockResponse().setBody("C"));
HttpUrl url = server.url("/");
assertEquals("A", get(url).body().string());
Request request = new Request.Builder().url(url).method("POST", requestBodyOrNull("POST")).build();
Response invalidate = client.newCall(request).execute();
assertEquals("B", invalidate.body().string());
assertEquals("C", get(url).body().string());
}
Aggregations