3838import com .google .firebase .IncomingHttpResponse ;
3939import com .google .firebase .auth .MockGoogleCredentials ;
4040import java .io .IOException ;
41+ import java .net .ServerSocket ;
42+ import java .net .Socket ;
4143import java .util .concurrent .atomic .AtomicBoolean ;
4244import org .apache .hc .client5 .http .impl .async .CloseableHttpAsyncClient ;
4345import org .apache .hc .client5 .http .impl .async .HttpAsyncClients ;
4951import org .apache .hc .core5 .http .protocol .HttpContext ;
5052
5153import org .junit .After ;
54+ import org .junit .AfterClass ;
5255import org .junit .BeforeClass ;
5356import org .junit .Test ;
5457
@@ -58,17 +61,37 @@ public class ApacheHttp2TransportIT {
5861 private static final ImmutableMap <String , Object > payload =
5962 ImmutableMap .<String , Object >of ("foo" , "bar" );
6063
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.
6266 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" ;
6567 private static final String GET_URL = "https://nghttp2.org/httpbin/get" ;
6668 private static final String POST_URL = "https://nghttp2.org/httpbin/post" ;
6769
70+ private static ServerSocket serverSocket ;
71+ private static Socket fillerSocket ;
72+ private static int port ;
73+
6874 @ 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+ }
7092 }
7193
94+
7295 @ After
7396 public void cleanup () {
7497 if (app != null ) {
@@ -96,7 +119,7 @@ public void testUnauthorizedPostRequest() throws FirebaseException {
96119 public void testConnectTimeoutGet () throws IOException {
97120 HttpTransport transport = new ApacheHttp2Transport ();
98121 try {
99- transport .createRequestFactory ().buildGetRequest (new GenericUrl (NO_CONNECT_URL ))
122+ transport .createRequestFactory ().buildGetRequest (new GenericUrl ("https://localhost:" + port ))
100123 .setConnectTimeout (100 ).execute ();
101124 fail ("No exception thrown for HTTP error response" );
102125 } catch (IOException e ) {
@@ -109,7 +132,7 @@ public void testConnectTimeoutGet() throws IOException {
109132 @ Test (timeout = 10_000L )
110133 public void testConnectTimeoutPost () throws IOException {
111134 ApacheHttp2Transport transport = new ApacheHttp2Transport ();
112- ApacheHttp2Request request = transport .buildRequest ("POST" , NO_CONNECT_URL );
135+ ApacheHttp2Request request = transport .buildRequest ("POST" , "https://localhost:" + port );
113136 request .setTimeout (100 , 0 );
114137 try {
115138 request .execute ();
@@ -128,7 +151,7 @@ public void testConnectTimeoutAuthorizedGet() throws FirebaseException {
128151 .setConnectTimeout (100 )
129152 .build (), "test-app" );
130153 ErrorHandlingHttpClient <FirebaseException > httpClient = getHttpClient (true , app );
131- HttpRequestInfo request = HttpRequestInfo .buildGetRequest (NO_CONNECT_URL );
154+ HttpRequestInfo request = HttpRequestInfo .buildGetRequest ("https://localhost:" + port );
132155
133156 try {
134157 httpClient .send (request );
@@ -147,7 +170,7 @@ public void testConnectTimeoutAuthorizedPost() throws FirebaseException {
147170 .setConnectTimeout (100 )
148171 .build (), "test-app" );
149172 ErrorHandlingHttpClient <FirebaseException > httpClient = getHttpClient (true , app );
150- HttpRequestInfo request = HttpRequestInfo .buildJsonPostRequest (NO_CONNECT_URL , payload );
173+ HttpRequestInfo request = HttpRequestInfo .buildJsonPostRequest ("https://localhost:" + port , payload );
151174
152175 try {
153176 httpClient .send (request );
@@ -273,7 +296,7 @@ public void process(
273296 request .execute ();
274297 fail ("should not actually make the request" );
275298 } catch (IOException exception ) {
276- assertEquals ("Exception in request" , exception .getMessage ());
299+ assertEquals ("Unknown exception in request" , exception .getMessage ());
277300 }
278301 assertTrue ("Expected to have called our test interceptor" , interceptorCalled .get ());
279302 }
0 commit comments