use of okhttp3.OkUrlFactory in project okhttp by square.
the class CacheAdapterTest method put_httpsGet.
@Test
public void put_httpsGet() throws Exception {
final URL serverUrl = configureHttpsServer(new MockResponse());
assertEquals("https", serverUrl.getProtocol());
ResponseCache responseCache = new AbstractResponseCache() {
@Override
public CacheRequest put(URI uri, URLConnection connection) throws IOException {
try {
assertTrue(connection instanceof HttpsURLConnection);
assertEquals(toUri(serverUrl), uri);
assertEquals(serverUrl, connection.getURL());
HttpsURLConnection cacheHttpsUrlConnection = (HttpsURLConnection) connection;
HttpsURLConnection realHttpsUrlConnection = (HttpsURLConnection) CacheAdapterTest.this.connection;
assertEquals(realHttpsUrlConnection.getCipherSuite(), cacheHttpsUrlConnection.getCipherSuite());
assertEquals(realHttpsUrlConnection.getPeerPrincipal(), cacheHttpsUrlConnection.getPeerPrincipal());
assertArrayEquals(realHttpsUrlConnection.getLocalCertificates(), cacheHttpsUrlConnection.getLocalCertificates());
assertArrayEquals(realHttpsUrlConnection.getServerCertificates(), cacheHttpsUrlConnection.getServerCertificates());
assertEquals(realHttpsUrlConnection.getLocalPrincipal(), cacheHttpsUrlConnection.getLocalPrincipal());
return null;
} catch (Throwable t) {
throw new IOException("unexpected cache failure", t);
}
}
};
setInternalCache(new CacheAdapter(responseCache));
client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
connection = new OkUrlFactory(client).open(serverUrl);
executeGet(connection);
}
use of okhttp3.OkUrlFactory in project okhttp by square.
the class CacheAdapterTest method get_httpGet.
@Test
public void get_httpGet() throws Exception {
final URL serverUrl = configureServer(new MockResponse());
assertEquals("http", serverUrl.getProtocol());
ResponseCache responseCache = new AbstractResponseCache() {
@Override
public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) throws IOException {
try {
assertEquals(toUri(serverUrl), uri);
assertEquals("GET", method);
assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent"));
assertEquals(Collections.singletonList("value1"), headers.get("key1"));
return null;
} catch (Throwable t) {
throw new IOException("unexpected cache failure", t);
}
}
};
setInternalCache(new CacheAdapter(responseCache));
connection = new OkUrlFactory(client).open(serverUrl);
connection.setRequestProperty("key1", "value1");
executeGet(connection);
}
use of okhttp3.OkUrlFactory in project okhttp by square.
the class CacheAdapterTest method get_httpsGet.
@Test
public void get_httpsGet() throws Exception {
final URL serverUrl = configureHttpsServer(new MockResponse());
assertEquals("https", serverUrl.getProtocol());
ResponseCache responseCache = new AbstractResponseCache() {
@Override
public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) throws IOException {
try {
assertEquals("https", uri.getScheme());
assertEquals(toUri(serverUrl), uri);
assertEquals("GET", method);
assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent"));
assertEquals(Collections.singletonList("value1"), headers.get("key1"));
return null;
} catch (Throwable t) {
throw new IOException("unexpected cache failure", t);
}
}
};
setInternalCache(new CacheAdapter(responseCache));
client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
connection = new OkUrlFactory(client).open(serverUrl);
connection.setRequestProperty("key1", "value1");
executeGet(connection);
}
use of okhttp3.OkUrlFactory in project okhttp by square.
the class DisconnectTest method interruptWritingRequestBody.
@Test
public void interruptWritingRequestBody() throws Exception {
// 2 MiB
int requestBodySize = 2 * 1024 * 1024;
server.enqueue(new MockResponse().throttleBody(64 * 1024, 125, // 500 Kbps
TimeUnit.MILLISECONDS));
server.start();
HttpURLConnection connection = new OkUrlFactory(client).open(server.url("/").url());
disconnectLater(connection, 500);
connection.setDoOutput(true);
connection.setFixedLengthStreamingMode(requestBodySize);
OutputStream requestBody = connection.getOutputStream();
byte[] buffer = new byte[1024];
try {
for (int i = 0; i < requestBodySize; i += buffer.length) {
requestBody.write(buffer);
requestBody.flush();
}
fail("Expected connection to be closed");
} catch (IOException expected) {
}
connection.disconnect();
}
use of okhttp3.OkUrlFactory in project okhttp by square.
the class URLConnectionTest method streamedBodyIsNotRetried.
@Test
public void streamedBodyIsNotRetried() throws Exception {
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AFTER_REQUEST));
urlFactory = new OkUrlFactory(defaultClient().newBuilder().dns(new DoubleInetAddressDns()).build());
HttpURLConnection connection = urlFactory.open(server.url("/").url());
connection.setDoOutput(true);
connection.setChunkedStreamingMode(100);
OutputStream os = connection.getOutputStream();
os.write("OutputStream is no fun.".getBytes("UTF-8"));
os.close();
try {
connection.getResponseCode();
fail();
} catch (IOException expected) {
}
assertEquals(1, server.getRequestCount());
}
Aggregations