38
38
import com .google .firebase .IncomingHttpResponse ;
39
39
import com .google .firebase .auth .MockGoogleCredentials ;
40
40
import java .io .IOException ;
41
+ import java .net .ServerSocket ;
42
+ import java .net .Socket ;
41
43
import java .util .concurrent .atomic .AtomicBoolean ;
42
44
import org .apache .hc .client5 .http .impl .async .CloseableHttpAsyncClient ;
43
45
import org .apache .hc .client5 .http .impl .async .HttpAsyncClients ;
49
51
import org .apache .hc .core5 .http .protocol .HttpContext ;
50
52
51
53
import org .junit .After ;
54
+ import org .junit .AfterClass ;
52
55
import org .junit .BeforeClass ;
53
56
import org .junit .Test ;
54
57
@@ -58,17 +61,37 @@ public class ApacheHttp2TransportIT {
58
61
private static final ImmutableMap <String , Object > payload =
59
62
ImmutableMap .<String , Object >of ("foo" , "bar" );
60
63
61
- // Sets a 5 second delay before server response to simulate a slow network that results in a read timeout.
64
+ // Sets a 5 second delay before server response to simulate a slow network that
65
+ // results in a read timeout.
62
66
private static final String DELAY_URL = "https://nghttp2.org/httpbin/delay/5" ;
63
- // Points to a unused port that simulates a slow conncetion which results in a conncetion timeout
64
- private static final String NO_CONNECT_URL = "https://google.com:81" ;
65
67
private static final String GET_URL = "https://nghttp2.org/httpbin/get" ;
66
68
private static final String POST_URL = "https://nghttp2.org/httpbin/post" ;
67
69
70
+ private static ServerSocket serverSocket ;
71
+ private static Socket fillerSocket ;
72
+ private static int port ;
73
+
68
74
@ BeforeClass
69
- public static void setUpClass () {
75
+ public static void setUpClass () throws IOException {
76
+ // Start server socket with a backlog queue of 1 and a automatically assigned port
77
+ serverSocket = new ServerSocket (0 , 1 );
78
+ port = serverSocket .getLocalPort ();
79
+ // Fill the backlog queue to force socket to ignore future connections
80
+ fillerSocket = new Socket ();
81
+ fillerSocket .connect (serverSocket .getLocalSocketAddress ());
82
+ }
83
+
84
+ @ AfterClass
85
+ public static void cleanUpClass () throws IOException {
86
+ if (serverSocket != null && !serverSocket .isClosed ()) {
87
+ serverSocket .close ();
88
+ }
89
+ if (fillerSocket != null && !fillerSocket .isClosed ()) {
90
+ fillerSocket .close ();
91
+ }
70
92
}
71
93
94
+
72
95
@ After
73
96
public void cleanup () {
74
97
if (app != null ) {
@@ -96,7 +119,7 @@ public void testUnauthorizedPostRequest() throws FirebaseException {
96
119
public void testConnectTimeoutGet () throws IOException {
97
120
HttpTransport transport = new ApacheHttp2Transport ();
98
121
try {
99
- transport .createRequestFactory ().buildGetRequest (new GenericUrl (NO_CONNECT_URL ))
122
+ transport .createRequestFactory ().buildGetRequest (new GenericUrl ("https://localhost:" + port ))
100
123
.setConnectTimeout (100 ).execute ();
101
124
fail ("No exception thrown for HTTP error response" );
102
125
} catch (IOException e ) {
@@ -109,7 +132,7 @@ public void testConnectTimeoutGet() throws IOException {
109
132
@ Test (timeout = 10_000L )
110
133
public void testConnectTimeoutPost () throws IOException {
111
134
ApacheHttp2Transport transport = new ApacheHttp2Transport ();
112
- ApacheHttp2Request request = transport .buildRequest ("POST" , NO_CONNECT_URL );
135
+ ApacheHttp2Request request = transport .buildRequest ("POST" , "https://localhost:" + port );
113
136
request .setTimeout (100 , 0 );
114
137
try {
115
138
request .execute ();
@@ -128,7 +151,7 @@ public void testConnectTimeoutAuthorizedGet() throws FirebaseException {
128
151
.setConnectTimeout (100 )
129
152
.build (), "test-app" );
130
153
ErrorHandlingHttpClient <FirebaseException > httpClient = getHttpClient (true , app );
131
- HttpRequestInfo request = HttpRequestInfo .buildGetRequest (NO_CONNECT_URL );
154
+ HttpRequestInfo request = HttpRequestInfo .buildGetRequest ("https://localhost:" + port );
132
155
133
156
try {
134
157
httpClient .send (request );
@@ -147,7 +170,7 @@ public void testConnectTimeoutAuthorizedPost() throws FirebaseException {
147
170
.setConnectTimeout (100 )
148
171
.build (), "test-app" );
149
172
ErrorHandlingHttpClient <FirebaseException > httpClient = getHttpClient (true , app );
150
- HttpRequestInfo request = HttpRequestInfo .buildJsonPostRequest (NO_CONNECT_URL , payload );
173
+ HttpRequestInfo request = HttpRequestInfo .buildJsonPostRequest ("https://localhost:" + port , payload );
151
174
152
175
try {
153
176
httpClient .send (request );
@@ -273,7 +296,7 @@ public void process(
273
296
request .execute ();
274
297
fail ("should not actually make the request" );
275
298
} catch (IOException exception ) {
276
- assertEquals ("Exception in request" , exception .getMessage ());
299
+ assertEquals ("Unknown exception in request" , exception .getMessage ());
277
300
}
278
301
assertTrue ("Expected to have called our test interceptor" , interceptorCalled .get ());
279
302
}
0 commit comments