use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method connectionCloseInRequest.
@Test
public void connectionCloseInRequest() throws IOException, InterruptedException {
// server doesn't honor the connection: close header!
server.enqueue(new MockResponse());
server.enqueue(new MockResponse());
HttpURLConnection a = urlFactory.open(server.url("/").url());
a.setRequestProperty("Connection", "close");
assertEquals(200, a.getResponseCode());
HttpURLConnection b = urlFactory.open(server.url("/").url());
assertEquals(200, b.getResponseCode());
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals("When connection: close is used, each request should get its own connection", 0, server.takeRequest().getSequenceNumber());
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method redirectedPostStripsRequestBodyHeaders.
@Test
public void redirectedPostStripsRequestBodyHeaders() throws Exception {
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /page2"));
server.enqueue(new MockResponse().setBody("Page 2"));
connection = urlFactory.open(server.url("/page1").url());
connection.setDoOutput(true);
connection.addRequestProperty("Content-Length", "4");
connection.addRequestProperty("Content-Type", "text/plain; charset=utf-8");
connection.addRequestProperty("Transfer-Encoding", "identity");
OutputStream outputStream = connection.getOutputStream();
outputStream.write("ABCD".getBytes("UTF-8"));
outputStream.close();
assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
assertEquals("POST /page1 HTTP/1.1", server.takeRequest().getRequestLine());
RecordedRequest page2 = server.takeRequest();
assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine());
assertNull(page2.getHeader("Content-Length"));
assertNull(page2.getHeader("Content-Type"));
assertNull(page2.getHeader("Transfer-Encoding"));
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method connectViaHttpsReusingConnectionsDifferentFactories.
@Test
public void connectViaHttpsReusingConnectionsDifferentFactories() throws Exception {
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
server.enqueue(new MockResponse().setBody("another response via HTTPS"));
// install a custom SSL socket factory so the server can be authorized
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
assertContent("this response comes via HTTPS", connection1);
SSLContext sslContext2 = SSLContext.getInstance("TLS");
sslContext2.init(null, null, null);
SSLSocketFactory sslSocketFactory2 = sslContext2.getSocketFactory();
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
X509TrustManager trustManager = (X509TrustManager) trustManagerFactory.getTrustManagers()[0];
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslSocketFactory2, trustManager).build());
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
try {
readAscii(connection2.getInputStream(), Integer.MAX_VALUE);
fail("without an SSL socket factory, the connection should fail");
} catch (SSLException expected) {
}
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method bug2939.
// Check that if we don't read to the end of a response, the next request on the
// recycled connection doesn't get the unread tail of the first request's response.
// http://code.google.com/p/android/issues/detail?id=2939
@Test
public void bug2939() throws Exception {
MockResponse response = new MockResponse().setChunkedBody("ABCDE\nFGHIJ\nKLMNO\nPQR", 8);
server.enqueue(response);
server.enqueue(response);
HttpURLConnection c1 = urlFactory.open(server.url("/").url());
assertContent("ABCDE", c1, 5);
HttpURLConnection c2 = urlFactory.open(server.url("/").url());
assertContent("ABCDE", c2, 5);
c1.getInputStream().close();
c2.getInputStream().close();
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method doesNotAttemptAuthorization21Times.
@Test
public void doesNotAttemptAuthorization21Times() throws Exception {
for (int i = 0; i < 21; i++) {
server.enqueue(new MockResponse().setResponseCode(401));
}
String credential = Credentials.basic("jesse", "peanutbutter");
urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build());
connection = urlFactory.open(server.url("/").url());
try {
connection.getInputStream();
fail();
} catch (ProtocolException expected) {
assertEquals(401, connection.getResponseCode());
assertEquals("Too many follow-up requests: 21", expected.getMessage());
}
}
Aggregations