use of okhttp3.mockwebserver.MockWebServer in project okhttp by square.
the class CallTest method customDns.
@Test
public void customDns() throws Exception {
// Configure a DNS that returns our local MockWebServer for android.com.
FakeDns dns = new FakeDns();
dns.set("android.com", Dns.SYSTEM.lookup(server.url("/").host()));
client = client.newBuilder().dns(dns).build();
server.enqueue(new MockResponse());
Request request = new Request.Builder().url(server.url("/").newBuilder().host("android.com").build()).build();
executeSynchronously(request).assertCode(200);
dns.assertRequests("android.com");
}
use of okhttp3.mockwebserver.MockWebServer in project okhttp by square.
the class URLConnectionTest method readTimeouts.
@Test
public void readTimeouts() throws IOException {
// This relies on the fact that MockWebServer doesn't close the
// connection after a response has been sent. This causes the client to
// try to read more bytes than are sent, which results in a timeout.
MockResponse timeout = new MockResponse().setBody("ABC").clearHeaders().addHeader("Content-Length: 4");
server.enqueue(timeout);
// to keep the server alive
server.enqueue(new MockResponse().setBody("unused"));
URLConnection connection = urlFactory.open(server.url("/").url());
connection.setReadTimeout(1000);
InputStream in = connection.getInputStream();
assertEquals('A', in.read());
assertEquals('B', in.read());
assertEquals('C', in.read());
try {
// if Content-Length was accurate, this would return -1 immediately
in.read();
fail();
} catch (SocketTimeoutException expected) {
}
in.close();
}
use of okhttp3.mockwebserver.MockWebServer in project okhttp by square.
the class ThreadInterruptTest method setUp.
@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();
}
Aggregations