use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method connectViaHttpsWithSSLFallback.
// TODO(jwilson): tests below this marker need to be migrated to OkHttp's request/response API.
@Test
public void connectViaHttpsWithSSLFallback() throws Exception {
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setSocketPolicy(FAIL_HANDSHAKE));
server.enqueue(new MockResponse().setBody("this response comes via SSL"));
urlFactory.setClient(urlFactory.client().newBuilder().hostnameVerifier(new RecordingHostnameVerifier()).connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS)).sslSocketFactory(suppressTlsFallbackClientSocketFactory(), sslClient.trustManager).build());
connection = urlFactory.open(server.url("/foo").url());
assertContent("this response comes via SSL", connection);
RecordedRequest failHandshakeRequest = server.takeRequest();
assertNull(failHandshakeRequest.getRequestLine());
RecordedRequest fallbackRequest = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", fallbackRequest.getRequestLine());
assertEquals(TlsVersion.TLS_1_0, fallbackRequest.getTlsVersion());
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method singleByteReadIsSigned.
@Test
public void singleByteReadIsSigned() throws IOException {
server.enqueue(new MockResponse().setBody(new Buffer().writeByte(-2).writeByte(-1)));
connection = urlFactory.open(server.url("/").url());
InputStream in = connection.getInputStream();
assertEquals(254, in.read());
assertEquals(255, in.read());
assertEquals(-1, in.read());
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method bodyPermittedOnDelete.
/**
* The RFC is unclear in this regard as it only specifies that this should invalidate the cache
* entry (if any).
*/
@Test
public void bodyPermittedOnDelete() throws Exception {
server.enqueue(new MockResponse());
HttpURLConnection connection = urlFactory.open(server.url("/").url());
connection.setRequestMethod("DELETE");
connection.setDoOutput(true);
connection.getOutputStream().write("BODY".getBytes(UTF_8));
assertEquals(200, connection.getResponseCode());
RecordedRequest request = server.takeRequest();
assertEquals("DELETE", request.getMethod());
assertEquals("BODY", request.getBody().readUtf8());
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method getContentEncodingConnects.
@Test
public void getContentEncodingConnects() throws Exception {
server.enqueue(new MockResponse().addHeader("Content-Encoding: identity").setBody("ABC"));
connection = urlFactory.open(server.url("/").url());
assertEquals("identity", connection.getContentEncoding());
connection.getInputStream().close();
}
use of okhttp3.Connection in project okhttp by square.
the class URLConnectionTest method writeTimeouts.
/** Confirm that an unacknowledged write times out. */
@Test
public void writeTimeouts() throws IOException {
MockWebServer server = new MockWebServer();
// Sockets on some platforms can have large buffers that mean writes do not block when
// required. These socket factories explicitly set the buffer sizes on sockets created.
final int SOCKET_BUFFER_SIZE = 4 * 1024;
server.setServerSocketFactory(new DelegatingServerSocketFactory(ServerSocketFactory.getDefault()) {
@Override
protected ServerSocket configureServerSocket(ServerSocket serverSocket) throws IOException {
serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
return serverSocket;
}
});
urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {
@Override
protected Socket configureSocket(Socket socket) throws IOException {
socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
return socket;
}
}).writeTimeout(500, TimeUnit.MILLISECONDS).build());
server.start();
server.enqueue(new MockResponse().throttleBody(1, 1, // Prevent the server from reading!
TimeUnit.SECONDS));
connection = urlFactory.open(server.url("/").url());
connection.setDoOutput(true);
connection.setChunkedStreamingMode(0);
OutputStream out = connection.getOutputStream();
try {
// 2 MiB.
byte[] data = new byte[2 * 1024 * 1024];
out.write(data);
fail();
} catch (SocketTimeoutException expected) {
}
}
Aggregations