use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method testClientConfiguredGzipContentEncodingAndConnectionReuse.
/**
* Test a bug where gzip input streams weren't exhausting the input stream, which corrupted the
* request that followed or prevented connection reuse.
* http://code.google.com/p/android/issues/detail?id=7059
* http://code.google.com/p/android/issues/detail?id=38817
*/
private void testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind transferKind, boolean tls) throws Exception {
if (tls) {
SSLSocketFactory socketFactory = sslClient.socketFactory;
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
server.useHttps(socketFactory, false);
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
}
MockResponse responseOne = new MockResponse();
responseOne.addHeader("Content-Encoding: gzip");
transferKind.setBody(responseOne, gzip("one (gzipped)"), 5);
server.enqueue(responseOne);
MockResponse responseTwo = new MockResponse();
transferKind.setBody(responseTwo, "two (identity)", 5);
server.enqueue(responseTwo);
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
connection1.addRequestProperty("Accept-Encoding", "gzip");
InputStream gunzippedIn = new GZIPInputStream(connection1.getInputStream());
assertEquals("one (gzipped)", readAscii(gunzippedIn, Integer.MAX_VALUE));
assertEquals(0, server.takeRequest().getSequenceNumber());
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
assertEquals("two (identity)", readAscii(connection2.getInputStream(), Integer.MAX_VALUE));
assertEquals(1, server.takeRequest().getSequenceNumber());
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method clientConfiguredGzipContentEncoding.
@Test
public void clientConfiguredGzipContentEncoding() throws Exception {
Buffer bodyBytes = gzip("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
server.enqueue(new MockResponse().setBody(bodyBytes).addHeader("Content-Encoding: gzip"));
URLConnection connection = urlFactory.open(server.url("/").url());
connection.addRequestProperty("Accept-Encoding", "gzip");
InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream());
assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", readAscii(gunzippedIn, Integer.MAX_VALUE));
assertEquals(bodyBytes.size(), connection.getContentLength());
RecordedRequest request = server.takeRequest();
assertEquals("gzip", request.getHeader("Accept-Encoding"));
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method authenticateWithGetAndTransparentGzip.
/** https://code.google.com/p/android/issues/detail?id=74026 */
@Test
public void authenticateWithGetAndTransparentGzip() throws Exception {
MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate: Basic realm=\"protected area\"").setBody("Please authenticate.");
// fail auth three times...
server.enqueue(pleaseAuthenticate);
server.enqueue(pleaseAuthenticate);
server.enqueue(pleaseAuthenticate);
// ...then succeed the fourth time
MockResponse successfulResponse = new MockResponse().addHeader("Content-Encoding", "gzip").setBody(gzip("Successful auth!"));
server.enqueue(successfulResponse);
Authenticator.setDefault(new RecordingAuthenticator());
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new JavaNetAuthenticator()).build());
connection = urlFactory.open(server.url("/").url());
assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
// no authorization header for the first request...
RecordedRequest request = server.takeRequest();
assertNull(request.getHeader("Authorization"));
// ...but the three requests that follow requests include an authorization header
for (int i = 0; i < 3; i++) {
request = server.takeRequest();
assertEquals("GET / HTTP/1.1", request.getRequestLine());
assertEquals("Basic " + RecordingAuthenticator.BASE_64_CREDENTIALS, request.getHeader("Authorization"));
}
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method testEarlyDisconnectDoesntHarmPooling.
private void testEarlyDisconnectDoesntHarmPooling(TransferKind transferKind) throws Exception {
MockResponse response1 = new MockResponse();
transferKind.setBody(response1, "ABCDEFGHIJK", 1024);
server.enqueue(response1);
MockResponse response2 = new MockResponse();
transferKind.setBody(response2, "LMNOPQRSTUV", 1024);
server.enqueue(response2);
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
InputStream in1 = connection1.getInputStream();
assertEquals("ABCDE", readAscii(in1, 5));
in1.close();
connection1.disconnect();
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
InputStream in2 = connection2.getInputStream();
assertEquals("LMNOP", readAscii(in2, 5));
in2.close();
connection2.disconnect();
assertEquals(0, server.takeRequest().getSequenceNumber());
// Connection is pooled!
assertEquals(1, server.takeRequest().getSequenceNumber());
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method testNoSslFallback.
@Test
public void testNoSslFallback() throws Exception {
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setSocketPolicy(FAIL_HANDSHAKE));
server.enqueue(new MockResponse().setBody("Response that would have needed fallbacks"));
HttpsURLConnection connection = (HttpsURLConnection) server.url("/").url().openConnection();
connection.setSSLSocketFactory(sslClient.socketFactory);
try {
connection.getInputStream();
fail();
} catch (SSLProtocolException expected) {
// RI response to the FAIL_HANDSHAKE
} catch (SSLHandshakeException expected) {
// Android's response to the FAIL_HANDSHAKE
}
}
Aggregations