Home | History | Annotate | Download | only in http
      1 /*
      2  * Copyright (C) 2014 Square, Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.squareup.okhttp.internal.http;
     17 
     18 import com.squareup.okhttp.DelegatingServerSocketFactory;
     19 import com.squareup.okhttp.DelegatingSocketFactory;
     20 import com.squareup.okhttp.OkHttpClient;
     21 import com.squareup.okhttp.OkUrlFactory;
     22 import com.squareup.okhttp.mockwebserver.MockResponse;
     23 import com.squareup.okhttp.mockwebserver.MockWebServer;
     24 import java.io.IOException;
     25 import java.io.InputStream;
     26 import java.io.OutputStream;
     27 import java.net.HttpURLConnection;
     28 import java.net.ServerSocket;
     29 import java.net.Socket;
     30 import java.util.concurrent.TimeUnit;
     31 
     32 import okio.Buffer;
     33 import org.junit.Before;
     34 import org.junit.Test;
     35 
     36 import javax.net.ServerSocketFactory;
     37 import javax.net.SocketFactory;
     38 
     39 import static org.junit.Assert.fail;
     40 
     41 public final class DisconnectTest {
     42 
     43   // The size of the socket buffers in bytes.
     44   private static final int SOCKET_BUFFER_SIZE = 256 * 1024;
     45 
     46   private MockWebServer server;
     47   private OkHttpClient client;
     48 
     49   @Before public void setUp() throws Exception {
     50     server = new MockWebServer();
     51     client = new OkHttpClient();
     52 
     53     // Sockets on some platforms can have large buffers that mean writes do not block when
     54     // required. These socket factories explicitly set the buffer sizes on sockets created.
     55     server.setServerSocketFactory(
     56         new DelegatingServerSocketFactory(ServerSocketFactory.getDefault()) {
     57           @Override
     58           protected ServerSocket configureServerSocket(ServerSocket serverSocket)
     59               throws IOException {
     60             serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
     61             return serverSocket;
     62           }
     63         });
     64     client.setSocketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) {
     65       @Override
     66       protected Socket configureSocket(Socket socket) throws IOException {
     67         socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
     68         socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
     69         return socket;
     70       }
     71     });
     72   }
     73 
     74   @Test public void interruptWritingRequestBody() throws Exception {
     75     int requestBodySize = 2 * 1024 * 1024; // 2 MiB
     76 
     77     server.enqueue(new MockResponse()
     78         .throttleBody(64 * 1024, 125, TimeUnit.MILLISECONDS)); // 500 Kbps
     79     server.start();
     80 
     81     HttpURLConnection connection = new OkUrlFactory(client).open(server.getUrl("/"));
     82     disconnectLater(connection, 500);
     83 
     84     connection.setDoOutput(true);
     85     connection.setFixedLengthStreamingMode(requestBodySize);
     86     OutputStream requestBody = connection.getOutputStream();
     87     byte[] buffer = new byte[1024];
     88     try {
     89       for (int i = 0; i < requestBodySize; i += buffer.length) {
     90         requestBody.write(buffer);
     91         requestBody.flush();
     92       }
     93       fail("Expected connection to be closed");
     94     } catch (IOException expected) {
     95     }
     96 
     97     connection.disconnect();
     98   }
     99 
    100   @Test public void interruptReadingResponseBody() throws Exception {
    101     int responseBodySize = 2 * 1024 * 1024; // 2 MiB
    102 
    103     server.enqueue(new MockResponse()
    104         .setBody(new Buffer().write(new byte[responseBodySize]))
    105         .throttleBody(64 * 1024, 125, TimeUnit.MILLISECONDS)); // 500 Kbps
    106     server.start();
    107 
    108     HttpURLConnection connection = new OkUrlFactory(client).open(server.getUrl("/"));
    109     disconnectLater(connection, 500);
    110 
    111     InputStream responseBody = connection.getInputStream();
    112     byte[] buffer = new byte[1024];
    113     try {
    114       while (responseBody.read(buffer) != -1) {
    115       }
    116       fail("Expected connection to be closed");
    117     } catch (IOException expected) {
    118     }
    119 
    120     connection.disconnect();
    121   }
    122 
    123   private void disconnectLater(final HttpURLConnection connection, final int delayMillis) {
    124     Thread interruptingCow = new Thread() {
    125       @Override public void run() {
    126         try {
    127           sleep(delayMillis);
    128           connection.disconnect();
    129         } catch (InterruptedException e) {
    130           throw new RuntimeException(e);
    131         }
    132       }
    133     };
    134     interruptingCow.start();
    135   }
    136 }
    137