本文整理汇总了Java中okhttp3.mockwebserver.MockWebServer.setServerSocketFactory方法的典型用法代码示例。如果您正苦于以下问题:Java MockWebServer.setServerSocketFactory方法的具体用法?Java MockWebServer.setServerSocketFactory怎么用?Java MockWebServer.setServerSocketFactory使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类okhttp3.mockwebserver.MockWebServer
的用法示例。
在下文中一共展示了MockWebServer.setServerSocketFactory方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setUp
import okhttp3.mockwebserver.MockWebServer; //导入方法依赖的package包/类
@Before public void setUp() throws Exception {
// 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.
server = new MockWebServer();
server.setServerSocketFactory(
new DelegatingServerSocketFactory(ServerSocketFactory.getDefault()) {
@Override protected ServerSocket configureServerSocket(
ServerSocket serverSocket) throws IOException {
serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
return serverSocket;
}
});
client = defaultClient().newBuilder()
.socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {
@Override protected Socket configureSocket(Socket socket) throws IOException {
socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
return socket;
}
})
.build();
}
示例2: setUp
import okhttp3.mockwebserver.MockWebServer; //导入方法依赖的package包/类
@Before public void setUp() throws Exception {
// 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.
server = new MockWebServer();
server.setServerSocketFactory(
new DelegatingServerSocketFactory(ServerSocketFactory.getDefault()) {
@Override
protected ServerSocket configureServerSocket(ServerSocket serverSocket)
throws IOException {
serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
return serverSocket;
}
});
client = defaultClient().newBuilder()
.socketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {
@Override
protected Socket configureSocket(Socket socket) throws IOException {
socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
return socket;
}
})
.build();
}
示例3: writeTimeouts
import okhttp3.mockwebserver.MockWebServer; //导入方法依赖的package包/类
/** 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, TimeUnit.SECONDS)); // Prevent the server from reading!
connection = urlFactory.open(server.url("/").url());
connection.setDoOutput(true);
connection.setChunkedStreamingMode(0);
OutputStream out = connection.getOutputStream();
try {
byte[] data = new byte[2 * 1024 * 1024]; // 2 MiB.
out.write(data);
fail();
} catch (SocketTimeoutException expected) {
}
}