• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 Google 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 
17 package com.squareup.okhttp.mockwebserver;
18 
19 /**
20  * What should be done with the incoming socket.
21  *
22  * <p>Be careful when using values like {@link #DISCONNECT_AT_END}, {@link #SHUTDOWN_INPUT_AT_END}
23  * and {@link #SHUTDOWN_OUTPUT_AT_END} that close a socket after a response, and where there are
24  * follow-up requests. The client is unblocked and free to continue as soon as it has received the
25  * entire response body. If and when the client makes a subsequent request using a pooled socket the
26  * server may not have had time to close the socket. The socket will be closed at an indeterminate
27  * point before or during the second request. It may be closed after client has started sending the
28  * request body. If a request body is not retryable then the client may fail the request, making
29  * client behavior non-deterministic. In order to prevent that {@link
30  * MockResponse#setSocketShutdownListener} can be used. Otherwise, add delays in the client to
31  * improve the chances that the server has closed the socket before follow up requests are made.
32  */
33 public enum SocketPolicy {
34 
35   /**
36    * Keep the socket open after the response. This is the default HTTP/1.1
37    * behavior.
38    */
39   KEEP_OPEN,
40 
41   /**
42    * Close the socket after the response. This is the default HTTP/1.0
43    * behavior.
44    *
45    * <p>See {@link SocketPolicy} for reasons why this can cause test flakiness and how to avoid it.
46    */
47   DISCONNECT_AT_END,
48 
49   /**
50    * Wrap the socket with SSL at the completion of this request/response pair.
51    * Used for CONNECT messages to tunnel SSL over an HTTP proxy.
52    */
53   UPGRADE_TO_SSL_AT_END,
54 
55   /**
56    * Request immediate close of connection without even reading the request. Use
57    * to simulate buggy SSL servers closing connections in response to
58    * unrecognized TLS extensions.
59    */
60   DISCONNECT_AT_START,
61 
62   /**
63    * Close connection after reading the request but before writing the response.
64    * Use this to simulate late connection pool failures.
65    */
66   DISCONNECT_AFTER_REQUEST,
67 
68   /** Close connection after reading half of the request body (if present). */
69   DISCONNECT_DURING_REQUEST_BODY,
70 
71   /** Close connection after writing half of the response body (if present). */
72   DISCONNECT_DURING_RESPONSE_BODY,
73 
74   /** Don't trust the client during the SSL handshake. */
75   FAIL_HANDSHAKE,
76 
77   /**
78    * Shutdown the socket input after sending the response. For testing bad
79    * behavior.
80    *
81    * <p>See {@link SocketPolicy} for reasons why this can cause test flakiness and how to avoid it.
82    */
83   SHUTDOWN_INPUT_AT_END,
84 
85   /**
86    * Shutdown the socket output after sending the response. For testing bad
87    * behavior.
88    *
89    * <p>See {@link SocketPolicy} for reasons why this can cause test flakiness and how to avoid it.
90    */
91   SHUTDOWN_OUTPUT_AT_END,
92 
93   /**
94    * Don't respond to the request but keep the socket open. For testing
95    * read response header timeout issue.
96    */
97   NO_RESPONSE
98 }
99