|
23 | 23 | import static org.junit.Assert.assertTrue;
|
24 | 24 | import static org.junit.Assert.fail;
|
25 | 25 |
|
| 26 | +import com.google.api.client.http.HttpMethods; |
26 | 27 | import com.google.api.client.http.HttpRequest;
|
27 | 28 | import com.google.api.client.http.HttpResponseException;
|
| 29 | +import com.google.api.client.http.HttpTransport; |
28 | 30 | import com.google.api.client.testing.http.MockHttpTransport;
|
29 | 31 | import com.google.api.client.testing.http.MockLowLevelHttpResponse;
|
30 | 32 | import com.google.common.collect.ImmutableList;
|
31 | 33 | import com.google.common.collect.ImmutableMap;
|
| 34 | +import com.google.firebase.ErrorCode; |
32 | 35 | import com.google.firebase.FirebaseApp;
|
33 | 36 | import com.google.firebase.FirebaseOptions;
|
| 37 | +import com.google.firebase.IncomingHttpResponse; |
| 38 | +import com.google.firebase.OutgoingHttpRequest; |
34 | 39 | import com.google.firebase.TestOnlyImplFirebaseTrampolines;
|
35 | 40 | import com.google.firebase.auth.MockGoogleCredentials;
|
36 | 41 | import com.google.firebase.testing.GenericFunction;
|
37 | 42 | import com.google.firebase.testing.TestResponseInterceptor;
|
| 43 | +import com.google.firebase.testing.TestUtils; |
| 44 | +import java.io.IOException; |
38 | 45 | import java.util.List;
|
39 | 46 | import java.util.Map;
|
40 | 47 | import java.util.concurrent.ExecutionException;
|
|
43 | 50 |
|
44 | 51 | public class FirebaseInstanceIdTest {
|
45 | 52 |
|
| 53 | + private static final Map<Integer, String> ERROR_MESSAGES = ImmutableMap.of( |
| 54 | + 404, "Instance ID \"test-iid\": Failed to find the instance ID.", |
| 55 | + 409, "Instance ID \"test-iid\": Already deleted.", |
| 56 | + 429, "Instance ID \"test-iid\": Request throttled out by the backend server.", |
| 57 | + 500, "Instance ID \"test-iid\": Internal server error.", |
| 58 | + 501, "Unexpected HTTP response with status: 501\ntest error" |
| 59 | + ); |
| 60 | + |
| 61 | + private static final Map<Integer, ErrorCode> ERROR_CODES = ImmutableMap.of( |
| 62 | + 404, ErrorCode.NOT_FOUND, |
| 63 | + 409, ErrorCode.CONFLICT, |
| 64 | + 429, ErrorCode.RESOURCE_EXHAUSTED, |
| 65 | + 500, ErrorCode.INTERNAL, |
| 66 | + 501, ErrorCode.UNKNOWN |
| 67 | + ); |
| 68 | + |
| 69 | + private static final String TEST_URL = |
| 70 | + "https://console.firebase.google.com/v1/project/test-project/instanceId/test-iid"; |
| 71 | + |
46 | 72 | @After
|
47 | 73 | public void tearDown() {
|
48 | 74 | TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
|
@@ -123,62 +149,120 @@ public Void call(Object... args) throws Exception {
|
123 | 149 | }
|
124 | 150 | );
|
125 | 151 |
|
126 |
| - String url = "https://console.firebase.google.com/v1/project/test-project/instanceId/test-iid"; |
127 | 152 | for (GenericFunction<Void> fn : functions) {
|
128 | 153 | TestResponseInterceptor interceptor = new TestResponseInterceptor();
|
129 | 154 | instanceId.setInterceptor(interceptor);
|
130 | 155 | fn.call();
|
131 | 156 |
|
132 | 157 | assertNotNull(interceptor.getResponse());
|
133 | 158 | HttpRequest request = interceptor.getResponse().getRequest();
|
134 |
| - assertEquals("DELETE", request.getRequestMethod()); |
135 |
| - assertEquals(url, request.getUrl().toString()); |
| 159 | + assertEquals(HttpMethods.DELETE, request.getRequestMethod()); |
| 160 | + assertEquals(TEST_URL, request.getUrl().toString()); |
136 | 161 | assertEquals("Bearer test-token", request.getHeaders().getAuthorization());
|
137 | 162 | }
|
138 | 163 | }
|
139 | 164 |
|
140 | 165 | @Test
|
141 | 166 | public void testDeleteInstanceIdError() throws Exception {
|
142 |
| - Map<Integer, String> errors = ImmutableMap.of( |
143 |
| - 404, "Instance ID \"test-iid\": Failed to find the instance ID.", |
144 |
| - 429, "Instance ID \"test-iid\": Request throttled out by the backend server.", |
145 |
| - 500, "Instance ID \"test-iid\": Internal server error.", |
146 |
| - 501, "Error while invoking instance ID service." |
147 |
| - ); |
| 167 | + final MockLowLevelHttpResponse response = new MockLowLevelHttpResponse(); |
| 168 | + MockHttpTransport transport = new MockHttpTransport.Builder() |
| 169 | + .setLowLevelHttpResponse(response) |
| 170 | + .build(); |
| 171 | + FirebaseOptions options = new FirebaseOptions.Builder() |
| 172 | + .setCredentials(new MockGoogleCredentials("test-token")) |
| 173 | + .setProjectId("test-project") |
| 174 | + .setHttpTransport(transport) |
| 175 | + .build(); |
| 176 | + FirebaseApp app = FirebaseApp.initializeApp(options); |
148 | 177 |
|
149 |
| - String url = "https://console.firebase.google.com/v1/project/test-project/instanceId/test-iid"; |
150 |
| - for (Map.Entry<Integer, String> entry : errors.entrySet()) { |
151 |
| - MockLowLevelHttpResponse response = new MockLowLevelHttpResponse() |
152 |
| - .setStatusCode(entry.getKey()) |
153 |
| - .setContent("test error"); |
154 |
| - MockHttpTransport transport = new MockHttpTransport.Builder() |
155 |
| - .setLowLevelHttpResponse(response) |
156 |
| - .build(); |
157 |
| - FirebaseOptions options = new FirebaseOptions.Builder() |
158 |
| - .setCredentials(new MockGoogleCredentials("test-token")) |
159 |
| - .setProjectId("test-project") |
160 |
| - .setHttpTransport(transport) |
161 |
| - .build(); |
162 |
| - final FirebaseApp app = FirebaseApp.initializeApp(options); |
163 |
| - |
164 |
| - FirebaseInstanceId instanceId = FirebaseInstanceId.getInstance(); |
165 |
| - TestResponseInterceptor interceptor = new TestResponseInterceptor(); |
166 |
| - instanceId.setInterceptor(interceptor); |
167 |
| - try { |
168 |
| - instanceId.deleteInstanceIdAsync("test-iid").get(); |
169 |
| - fail("No error thrown for HTTP error"); |
170 |
| - } catch (ExecutionException e) { |
171 |
| - assertTrue(e.getCause() instanceof FirebaseInstanceIdException); |
172 |
| - assertEquals(entry.getValue(), e.getCause().getMessage()); |
173 |
| - assertTrue(e.getCause().getCause() instanceof HttpResponseException); |
174 |
| - } |
| 178 | + // Disable retries by passing a regular HttpRequestFactory. |
| 179 | + FirebaseInstanceId instanceId = new FirebaseInstanceId(app, transport.createRequestFactory()); |
| 180 | + TestResponseInterceptor interceptor = new TestResponseInterceptor(); |
| 181 | + instanceId.setInterceptor(interceptor); |
175 | 182 |
|
176 |
| - assertNotNull(interceptor.getResponse()); |
177 |
| - HttpRequest request = interceptor.getResponse().getRequest(); |
178 |
| - assertEquals("DELETE", request.getRequestMethod()); |
179 |
| - assertEquals(url, request.getUrl().toString()); |
180 |
| - assertEquals("Bearer test-token", request.getHeaders().getAuthorization()); |
| 183 | + try { |
| 184 | + for (int statusCode : ERROR_CODES.keySet()) { |
| 185 | + response.setStatusCode(statusCode).setContent("test error"); |
| 186 | + |
| 187 | + try { |
| 188 | + instanceId.deleteInstanceIdAsync("test-iid").get(); |
| 189 | + fail("No error thrown for HTTP error"); |
| 190 | + } catch (ExecutionException e) { |
| 191 | + assertTrue(e.getCause() instanceof FirebaseInstanceIdException); |
| 192 | + checkFirebaseInstanceIdException((FirebaseInstanceIdException) e.getCause(), statusCode); |
| 193 | + } |
| 194 | + |
| 195 | + assertNotNull(interceptor.getResponse()); |
| 196 | + HttpRequest request = interceptor.getResponse().getRequest(); |
| 197 | + assertEquals(HttpMethods.DELETE, request.getRequestMethod()); |
| 198 | + assertEquals(TEST_URL, request.getUrl().toString()); |
| 199 | + } |
| 200 | + } finally { |
181 | 201 | app.delete();
|
182 | 202 | }
|
183 | 203 | }
|
| 204 | + |
| 205 | + @Test |
| 206 | + public void testDeleteInstanceIdTransportError() throws Exception { |
| 207 | + HttpTransport transport = TestUtils.createFaultyHttpTransport(); |
| 208 | + FirebaseOptions options = new FirebaseOptions.Builder() |
| 209 | + .setCredentials(new MockGoogleCredentials("test-token")) |
| 210 | + .setProjectId("test-project") |
| 211 | + .setHttpTransport(transport) |
| 212 | + .build(); |
| 213 | + FirebaseApp app = FirebaseApp.initializeApp(options); |
| 214 | + // Disable retries by passing a regular HttpRequestFactory. |
| 215 | + FirebaseInstanceId instanceId = new FirebaseInstanceId(app, transport.createRequestFactory()); |
| 216 | + |
| 217 | + try { |
| 218 | + instanceId.deleteInstanceIdAsync("test-iid").get(); |
| 219 | + fail("No error thrown for HTTP error"); |
| 220 | + } catch (ExecutionException e) { |
| 221 | + assertTrue(e.getCause() instanceof FirebaseInstanceIdException); |
| 222 | + FirebaseInstanceIdException error = (FirebaseInstanceIdException) e.getCause(); |
| 223 | + assertEquals(ErrorCode.UNKNOWN, error.getErrorCodeNew()); |
| 224 | + assertEquals( |
| 225 | + "Unknown error while making a remote service call: transport error", |
| 226 | + error.getMessage()); |
| 227 | + assertTrue(error.getCause() instanceof IOException); |
| 228 | + assertNull(error.getHttpResponse()); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + @Test |
| 233 | + public void testDeleteInstanceIdInvalidJsonIgnored() throws Exception { |
| 234 | + final MockLowLevelHttpResponse response = new MockLowLevelHttpResponse(); |
| 235 | + MockHttpTransport transport = new MockHttpTransport.Builder() |
| 236 | + .setLowLevelHttpResponse(response) |
| 237 | + .build(); |
| 238 | + FirebaseOptions options = new FirebaseOptions.Builder() |
| 239 | + .setCredentials(new MockGoogleCredentials("test-token")) |
| 240 | + .setProjectId("test-project") |
| 241 | + .setHttpTransport(transport) |
| 242 | + .build(); |
| 243 | + FirebaseApp app = FirebaseApp.initializeApp(options); |
| 244 | + |
| 245 | + // Disable retries by passing a regular HttpRequestFactory. |
| 246 | + FirebaseInstanceId instanceId = new FirebaseInstanceId(app, transport.createRequestFactory()); |
| 247 | + TestResponseInterceptor interceptor = new TestResponseInterceptor(); |
| 248 | + instanceId.setInterceptor(interceptor); |
| 249 | + response.setContent("not json"); |
| 250 | + |
| 251 | + instanceId.deleteInstanceIdAsync("test-iid").get(); |
| 252 | + |
| 253 | + assertNotNull(interceptor.getResponse()); |
| 254 | + } |
| 255 | + |
| 256 | + private void checkFirebaseInstanceIdException(FirebaseInstanceIdException error, int statusCode) { |
| 257 | + assertEquals(ERROR_CODES.get(statusCode), error.getErrorCodeNew()); |
| 258 | + assertEquals(ERROR_MESSAGES.get(statusCode), error.getMessage()); |
| 259 | + assertTrue(error.getCause() instanceof HttpResponseException); |
| 260 | + |
| 261 | + IncomingHttpResponse httpResponse = error.getHttpResponse(); |
| 262 | + assertNotNull(httpResponse); |
| 263 | + assertEquals(statusCode, httpResponse.getStatusCode()); |
| 264 | + OutgoingHttpRequest request = httpResponse.getRequest(); |
| 265 | + assertEquals(HttpMethods.DELETE, request.getMethod()); |
| 266 | + assertEquals(TEST_URL, request.getUrl()); |
| 267 | + } |
184 | 268 | }
|
0 commit comments