Search in sources :

Example 51 with MockWebServer

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");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 52 with MockWebServer

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();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SocketTimeoutException(java.net.SocketTimeoutException) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 53 with MockWebServer

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();
}
Also used : DelegatingSocketFactory(okhttp3.DelegatingSocketFactory) DelegatingServerSocketFactory(okhttp3.DelegatingServerSocketFactory) MockWebServer(okhttp3.mockwebserver.MockWebServer) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Before(org.junit.Before)

Aggregations

MockWebServer (okhttp3.mockwebserver.MockWebServer)48 MockResponse (okhttp3.mockwebserver.MockResponse)31 Test (org.junit.Test)20 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)16 Before (org.junit.Before)14 IOException (java.io.IOException)9 CookieManager (java.net.CookieManager)7 HttpCookie (java.net.HttpCookie)5 Retrofit (retrofit2.Retrofit)5 ServerSocket (java.net.ServerSocket)4 Socket (java.net.Socket)4 Request (okhttp3.Request)4 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)3 Response (com.github.scribejava.core.model.Response)3 InputStream (java.io.InputStream)3 InterruptedIOException (java.io.InterruptedIOException)3 HttpURLConnection (java.net.HttpURLConnection)3 URI (java.net.URI)3 URL (java.net.URL)3 List (java.util.List)3