本文整理匯總了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) {
}
}