|
| 1 | +package com.google.firebase.internal; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNull; |
| 5 | +import static org.junit.Assert.fail; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | + |
| 9 | +import org.junit.After; |
| 10 | +import org.junit.BeforeClass; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import com.google.api.client.http.HttpRequestFactory; |
| 14 | +import com.google.api.client.http.HttpResponseException; |
| 15 | +import com.google.api.client.json.JsonFactory; |
| 16 | +import com.google.api.client.util.GenericData; |
| 17 | +import com.google.auth.oauth2.GoogleCredentials; |
| 18 | +import com.google.common.collect.ImmutableMap; |
| 19 | +import com.google.firebase.ErrorCode; |
| 20 | +import com.google.firebase.FirebaseApp; |
| 21 | +import com.google.firebase.FirebaseException; |
| 22 | +import com.google.firebase.FirebaseOptions; |
| 23 | +import com.google.firebase.IncomingHttpResponse; |
| 24 | +import com.google.firebase.TestOnlyImplFirebaseTrampolines; |
| 25 | +import com.google.firebase.auth.MockGoogleCredentials; |
| 26 | + |
| 27 | +public class ApacheHttp2TransportIT { |
| 28 | + private static final GoogleCredentials MOCK_CREDENTIALS = new MockGoogleCredentials("test_token"); |
| 29 | + private static final ImmutableMap<String, Object> payload = ImmutableMap.<String, Object>of("foo", "bar"); |
| 30 | + // Sets a 1 second delay before response |
| 31 | + private static final String DELAY_URL = "https://nghttp2.org/httpbin/delay/1"; |
| 32 | + private static final String POST_URL = "https://nghttp2.org/httpbin/post"; |
| 33 | + |
| 34 | + @BeforeClass |
| 35 | + public static void setUpClass() { |
| 36 | + } |
| 37 | + |
| 38 | + @After |
| 39 | + public void cleanup() { |
| 40 | + TestOnlyImplFirebaseTrampolines.clearInstancesForTest(); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testUnauthorizedPostRequest() throws FirebaseException { |
| 45 | + ErrorHandlingHttpClient<FirebaseException> httpClient = getHttpClient(false); |
| 46 | + HttpRequestInfo request = HttpRequestInfo.buildJsonPostRequest(POST_URL, payload); |
| 47 | + GenericData body = httpClient.sendAndParse(request, GenericData.class); |
| 48 | + assertEquals("{\"foo\":\"bar\"}", body.get("data")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testConnectTimeoutAuthorizedGet() throws FirebaseException { |
| 53 | + FirebaseApp timeoutApp = FirebaseApp.initializeApp(FirebaseOptions.builder() |
| 54 | + .setCredentials(MOCK_CREDENTIALS) |
| 55 | + .setConnectTimeout(1) |
| 56 | + .build()); |
| 57 | + ErrorHandlingHttpClient<FirebaseException> httpClient = getHttpClient(true, timeoutApp); |
| 58 | + HttpRequestInfo request = HttpRequestInfo.buildGetRequest(DELAY_URL); |
| 59 | + |
| 60 | + try { |
| 61 | + httpClient.send(request); |
| 62 | + fail("No exception thrown for HTTP error response"); |
| 63 | + } catch (FirebaseException e) { |
| 64 | + assertEquals(ErrorCode.UNKNOWN, e.getErrorCode()); |
| 65 | + assertEquals("IO error: Exception in request", e.getMessage()); |
| 66 | + assertNull(e.getHttpResponse()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testConnectTimeoutAuthorizedPost() throws FirebaseException { |
| 72 | + FirebaseApp timeoutApp = FirebaseApp.initializeApp(FirebaseOptions.builder() |
| 73 | + .setCredentials(MOCK_CREDENTIALS) |
| 74 | + .setConnectTimeout(1) |
| 75 | + .build()); |
| 76 | + ErrorHandlingHttpClient<FirebaseException> httpClient = getHttpClient(true, timeoutApp); |
| 77 | + HttpRequestInfo request = HttpRequestInfo.buildJsonPostRequest(DELAY_URL, payload); |
| 78 | + |
| 79 | + try { |
| 80 | + httpClient.send(request); |
| 81 | + fail("No exception thrown for HTTP error response"); |
| 82 | + } catch (FirebaseException e) { |
| 83 | + assertEquals(ErrorCode.UNKNOWN, e.getErrorCode()); |
| 84 | + assertEquals("IO error: Exception in request", e.getMessage()); |
| 85 | + assertNull(e.getHttpResponse()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testReadTimeoutAuthorizedGet() throws FirebaseException { |
| 91 | + FirebaseApp timeoutApp = FirebaseApp.initializeApp(FirebaseOptions.builder() |
| 92 | + .setCredentials(MOCK_CREDENTIALS) |
| 93 | + .setReadTimeout(1) |
| 94 | + .build()); |
| 95 | + ErrorHandlingHttpClient<FirebaseException> httpClient = getHttpClient(true, timeoutApp); |
| 96 | + HttpRequestInfo request = HttpRequestInfo.buildGetRequest(DELAY_URL); |
| 97 | + |
| 98 | + try { |
| 99 | + httpClient.send(request); |
| 100 | + fail("No exception thrown for HTTP error response"); |
| 101 | + } catch (FirebaseException e) { |
| 102 | + assertEquals(ErrorCode.UNKNOWN, e.getErrorCode()); |
| 103 | + assertEquals("IO error: Exception in request", e.getMessage()); |
| 104 | + assertNull(e.getHttpResponse()); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testReadTimeoutAuthorizedPost() throws FirebaseException { |
| 110 | + FirebaseApp timeoutApp = FirebaseApp.initializeApp(FirebaseOptions.builder() |
| 111 | + .setCredentials(MOCK_CREDENTIALS) |
| 112 | + .setReadTimeout(1) |
| 113 | + .build()); |
| 114 | + ErrorHandlingHttpClient<FirebaseException> httpClient = getHttpClient(true, timeoutApp); |
| 115 | + HttpRequestInfo request = HttpRequestInfo.buildJsonPostRequest(DELAY_URL, payload); |
| 116 | + |
| 117 | + try { |
| 118 | + httpClient.send(request); |
| 119 | + fail("No exception thrown for HTTP error response"); |
| 120 | + } catch (FirebaseException e) { |
| 121 | + assertEquals(ErrorCode.UNKNOWN, e.getErrorCode()); |
| 122 | + assertEquals("IO error: Exception in request", e.getMessage()); |
| 123 | + assertNull(e.getHttpResponse()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testWriteTimeoutAuthorizedGet() throws FirebaseException { |
| 129 | + FirebaseApp timeoutApp = FirebaseApp.initializeApp(FirebaseOptions.builder() |
| 130 | + .setCredentials(MOCK_CREDENTIALS) |
| 131 | + .setWriteTimeout(1) |
| 132 | + .build()); |
| 133 | + ErrorHandlingHttpClient<FirebaseException> httpClient = getHttpClient(true, timeoutApp); |
| 134 | + HttpRequestInfo request = HttpRequestInfo.buildGetRequest(DELAY_URL); |
| 135 | + |
| 136 | + try { |
| 137 | + httpClient.send(request); |
| 138 | + fail("No exception thrown for HTTP error response"); |
| 139 | + } catch (FirebaseException e) { |
| 140 | + assertEquals(ErrorCode.UNKNOWN, e.getErrorCode()); |
| 141 | + assertEquals("IO error: Timed out", e.getMessage()); |
| 142 | + assertNull(e.getHttpResponse()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testWriteTimeoutAuthorizedPost() throws FirebaseException { |
| 148 | + FirebaseApp timeoutApp = FirebaseApp.initializeApp(FirebaseOptions.builder() |
| 149 | + .setCredentials(MOCK_CREDENTIALS) |
| 150 | + .setWriteTimeout(1) |
| 151 | + .build()); |
| 152 | + ErrorHandlingHttpClient<FirebaseException> httpClient = getHttpClient(true, timeoutApp); |
| 153 | + HttpRequestInfo request = HttpRequestInfo.buildJsonPostRequest(DELAY_URL, payload); |
| 154 | + |
| 155 | + try { |
| 156 | + httpClient.send(request); |
| 157 | + fail("No exception thrown for HTTP error response"); |
| 158 | + } catch (FirebaseException e) { |
| 159 | + assertEquals(ErrorCode.UNKNOWN, e.getErrorCode()); |
| 160 | + assertEquals("IO error: Timed out", e.getMessage()); |
| 161 | + assertNull(e.getHttpResponse()); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private static ErrorHandlingHttpClient<FirebaseException> getHttpClient(boolean authorized, FirebaseApp app) { |
| 166 | + HttpRequestFactory requestFactory; |
| 167 | + if (authorized) { |
| 168 | + requestFactory = ApiClientUtils.newAuthorizedRequestFactory(app); |
| 169 | + } else { |
| 170 | + requestFactory = ApiClientUtils.newUnauthorizedRequestFactory(app); |
| 171 | + } |
| 172 | + JsonFactory jsonFactory = ApiClientUtils.getDefaultJsonFactory(); |
| 173 | + TestHttpErrorHandler errorHandler = new TestHttpErrorHandler(); |
| 174 | + return new ErrorHandlingHttpClient<>(requestFactory, jsonFactory, errorHandler); |
| 175 | + } |
| 176 | + |
| 177 | + private static ErrorHandlingHttpClient<FirebaseException> getHttpClient(boolean authorized) { |
| 178 | + return getHttpClient(authorized, FirebaseApp.initializeApp(FirebaseOptions.builder() |
| 179 | + .setCredentials(MOCK_CREDENTIALS) |
| 180 | + .build(), "test-app")); |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + private static class TestHttpErrorHandler implements HttpErrorHandler<FirebaseException> { |
| 185 | + @Override |
| 186 | + public FirebaseException handleIOException(IOException e) { |
| 187 | + return new FirebaseException( |
| 188 | + ErrorCode.UNKNOWN, "IO error: " + e.getMessage(), e); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public FirebaseException handleHttpResponseException( |
| 193 | + HttpResponseException e, IncomingHttpResponse response) { |
| 194 | + return new FirebaseException( |
| 195 | + ErrorCode.INTERNAL, "Example error message: " + e.getContent(), e, response); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public FirebaseException handleParseException(IOException e, IncomingHttpResponse response) { |
| 200 | + return new FirebaseException(ErrorCode.UNKNOWN, "Parse error", e, response); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments