use of okhttp3.Protocol in project okhttp by square.
the class JavaApiConverterTest method createJavaCacheResponse_httpsPost.
@Test
public void createJavaCacheResponse_httpsPost() throws Exception {
Request okRequest = createArbitraryOkRequest().newBuilder().url("https://secure/request").post(createRequestBody("RequestBody")).build();
ResponseBody responseBody = createResponseBody("ResponseBody");
Handshake handshake = Handshake.get(null, CipherSuite.TLS_RSA_WITH_NULL_MD5, Arrays.<Certificate>asList(SERVER_CERT), Arrays.<Certificate>asList(LOCAL_CERT));
Response okResponse = createArbitraryOkResponse(okRequest).newBuilder().protocol(Protocol.HTTP_1_1).code(200).message("Fantastic").addHeader("key1", "value1_1").addHeader("key2", "value2").addHeader("key1", "value1_2").body(responseBody).handshake(handshake).build();
SecureCacheResponse javaCacheResponse = (SecureCacheResponse) JavaApiConverter.createJavaCacheResponse(okResponse);
Map<String, List<String>> javaHeaders = javaCacheResponse.getHeaders();
assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1"));
assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), javaHeaders.get(null));
assertEquals("ResponseBody", readAll(javaCacheResponse.getBody()));
assertEquals(handshake.cipherSuite().javaName(), javaCacheResponse.getCipherSuite());
assertEquals(handshake.localCertificates(), javaCacheResponse.getLocalCertificateChain());
assertEquals(handshake.peerCertificates(), javaCacheResponse.getServerCertificateChain());
assertEquals(handshake.localPrincipal(), javaCacheResponse.getLocalPrincipal());
assertEquals(handshake.peerPrincipal(), javaCacheResponse.getPeerPrincipal());
}
use of okhttp3.Protocol in project okhttp by square.
the class JavaApiConverterTest method createJavaCacheResponse_httpGet.
@Test
public void createJavaCacheResponse_httpGet() throws Exception {
Request okRequest = createArbitraryOkRequest().newBuilder().url("http://insecure/request").get().build();
Response okResponse = createArbitraryOkResponse(okRequest).newBuilder().protocol(Protocol.HTTP_1_1).code(200).message("Fantastic").addHeader("key1", "value1_1").addHeader("key2", "value2").addHeader("key1", "value1_2").body(null).build();
CacheResponse javaCacheResponse = JavaApiConverter.createJavaCacheResponse(okResponse);
assertFalse(javaCacheResponse instanceof SecureCacheResponse);
Map<String, List<String>> javaHeaders = javaCacheResponse.getHeaders();
assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1"));
assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), javaHeaders.get(null));
assertNull(javaCacheResponse.getBody());
}
use of okhttp3.Protocol in project okhttp by square.
the class CacheTest method secureResponseCachingAndProtocolRedirects.
/**
* We've had bugs where caching and cross-protocol redirects yield class cast exceptions internal
* to the cache because we incorrectly assumed that HttpsURLConnection was always HTTPS and
* HttpURLConnection was always HTTP; in practice redirects mean that each can do either.
*
* https://github.com/square/okhttp/issues/214
*/
@Test
public void secureResponseCachingAndProtocolRedirects() throws IOException {
server2.useHttps(sslClient.socketFactory, false);
server2.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).setBody("ABC"));
server2.enqueue(new MockResponse().setBody("DEF"));
server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).setResponseCode(HttpURLConnection.HTTP_MOVED_PERM).addHeader("Location: " + server2.url("/")));
client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(NULL_HOSTNAME_VERIFIER).build();
Response response1 = get(server.url("/"));
assertEquals("ABC", response1.body().string());
// Cached!
Response response2 = get(server.url("/"));
assertEquals("ABC", response2.body().string());
// 2 direct + 2 redirect = 4
assertEquals(4, cache.requestCount());
assertEquals(2, cache.hitCount());
}
use of okhttp3.Protocol in project okhttp by square.
the class OkUrlFactory method open.
HttpURLConnection open(URL url, Proxy proxy) {
String protocol = url.getProtocol();
OkHttpClient copy = client.newBuilder().proxy(proxy).build();
if (protocol.equals("http"))
return new OkHttpURLConnection(url, copy, urlFilter);
if (protocol.equals("https"))
return new OkHttpsURLConnection(url, copy, urlFilter);
throw new IllegalArgumentException("Unexpected protocol: " + protocol);
}
use of okhttp3.Protocol in project okhttp by square.
the class UrlConnectionCacheTest method secureResponseCachingAndProtocolRedirects.
/**
* We've had bugs where caching and cross-protocol redirects yield class cast exceptions internal
* to the cache because we incorrectly assumed that HttpsURLConnection was always HTTPS and
* HttpURLConnection was always HTTP; in practice redirects mean that each can do either.
*
* https://github.com/square/okhttp/issues/214
*/
@Test
public void secureResponseCachingAndProtocolRedirects() throws IOException {
server2.useHttps(sslClient.socketFactory, false);
server2.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).setBody("ABC"));
server2.enqueue(new MockResponse().setBody("DEF"));
server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).setResponseCode(HttpURLConnection.HTTP_MOVED_PERM).addHeader("Location: " + server2.url("/").url()));
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(NULL_HOSTNAME_VERIFIER).build());
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
assertEquals("ABC", readAscii(connection1));
// Cached!
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
assertEquals("ABC", readAscii(connection2));
// 2 direct + 2 redirect = 4
assertEquals(4, cache.requestCount());
assertEquals(2, cache.hitCount());
}
Aggregations