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 void configureServerSocket(ServerSocket serverSocket) throws IOException { 59 serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE); 60 } 61 }); 62 client.setSocketFactory(new DelegatingSocketFactory(SocketFactory.getDefault()) { 63 @Override 64 protected void configureSocket(Socket socket) throws IOException { 65 socket.setSendBufferSize(SOCKET_BUFFER_SIZE); 66 socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE); 67 } 68 }); 69 } 70 71 @Test public void interruptWritingRequestBody() throws Exception { 72 int requestBodySize = 10 * 1024 * 1024; // 10 MiB 73 74 server.enqueue(new MockResponse() 75 .throttleBody(64 * 1024, 125, TimeUnit.MILLISECONDS)); // 500 Kbps 76 server.start(); 77 78 HttpURLConnection connection = new OkUrlFactory(client).open(server.getUrl("/")); 79 disconnectLater(connection, 500); 80 81 connection.setDoOutput(true); 82 connection.setFixedLengthStreamingMode(requestBodySize); 83 OutputStream requestBody = connection.getOutputStream(); 84 byte[] buffer = new byte[1024]; 85 try { 86 for (int i = 0; i < requestBodySize; i += buffer.length) { 87 requestBody.write(buffer); 88 requestBody.flush(); 89 } 90 fail("Expected connection to be closed"); 91 } catch (IOException expected) { 92 } 93 94 connection.disconnect(); 95 } 96 97 @Test public void interruptReadingResponseBody() throws Exception { 98 int responseBodySize = 10 * 1024 * 1024; // 10 MiB 99 100 server.enqueue(new MockResponse() 101 .setBody(new Buffer().write(new byte[responseBodySize])) 102 .throttleBody(64 * 1024, 125, TimeUnit.MILLISECONDS)); // 500 Kbps 103 server.start(); 104 105 HttpURLConnection connection = new OkUrlFactory(client).open(server.getUrl("/")); 106 disconnectLater(connection, 500); 107 108 InputStream responseBody = connection.getInputStream(); 109 byte[] buffer = new byte[1024]; 110 try { 111 while (responseBody.read(buffer) != -1) { 112 } 113 fail("Expected connection to be closed"); 114 } catch (IOException expected) { 115 } 116 117 connection.disconnect(); 118 } 119 120 private void disconnectLater(final HttpURLConnection connection, final int delayMillis) { 121 Thread interruptingCow = new Thread() { 122 @Override public void run() { 123 try { 124 sleep(delayMillis); 125 connection.disconnect(); 126 } catch (InterruptedException e) { 127 throw new RuntimeException(e); 128 } 129 } 130 }; 131 interruptingCow.start(); 132 } 133 } 134