Repository navigation
Expand file tree
/
Copy pathFirebaseInstanceIdTest.java
More file actions
281 lines (247 loc) · 10.5 KB
/
Copy pathFirebaseInstanceIdTest.java
File metadata and controls
281 lines (247 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.firebase.iid;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.api.client.http.HttpMethods;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.firebase.ErrorCode;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.IncomingHttpResponse;
import com.google.firebase.OutgoingHttpRequest;
import com.google.firebase.TestOnlyImplFirebaseTrampolines;
import com.google.firebase.auth.MockGoogleCredentials;
import com.google.firebase.testing.GenericFunction;
import com.google.firebase.testing.TestResponseInterceptor;
import com.google.firebase.testing.TestUtils;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.junit.After;
import org.junit.Test;
public class FirebaseInstanceIdTest {
private static final FirebaseOptions APP_OPTIONS = FirebaseOptions.builder()
.setCredentials(new MockGoogleCredentials("test-token"))
.setProjectId("test-project")
.build();
private static final Map<Integer, String> ERROR_MESSAGES = ImmutableMap.of(
404, "Instance ID \"test-iid\": Failed to find the instance ID.",
409, "Instance ID \"test-iid\": Already deleted.",
429, "Instance ID \"test-iid\": Request throttled out by the backend server.",
500, "Instance ID \"test-iid\": Internal server error.",
501, "Unexpected HTTP response with status: 501\ntest error"
);
private static final Map<Integer, ErrorCode> ERROR_CODES = ImmutableMap.of(
404, ErrorCode.NOT_FOUND,
409, ErrorCode.CONFLICT,
429, ErrorCode.RESOURCE_EXHAUSTED,
500, ErrorCode.INTERNAL,
501, ErrorCode.UNKNOWN
);
private static final String TEST_URL =
"https://console.firebase.google.com/v1/project/test-project/instanceId/test-iid";
@After
public void tearDown() {
TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
}
@Test
public void testNoProjectId() {
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(new MockGoogleCredentials("test-token"))
.build();
FirebaseApp.initializeApp(options);
try {
FirebaseInstanceId.getInstance();
fail("No error thrown for missing project ID");
} catch (IllegalArgumentException expected) {
// expected
}
}
@Test
public void testInvokeAfterAppDelete() {
FirebaseApp app = FirebaseApp.initializeApp(APP_OPTIONS, "testInvokeAfterAppDelete");
FirebaseInstanceId instanceId = FirebaseInstanceId.getInstance(app);
assertNotNull(instanceId);
app.delete();
try {
FirebaseInstanceId.getInstance(app);
fail("No error thrown when invoking instanceId after deleting app");
} catch (IllegalStateException ex) {
String message = "FirebaseApp 'testInvokeAfterAppDelete' was deleted";
assertEquals(message, ex.getMessage());
}
}
@Test
public void testInvalidInstanceId() {
FirebaseApp.initializeApp(APP_OPTIONS);
FirebaseInstanceId instanceId = FirebaseInstanceId.getInstance();
TestResponseInterceptor interceptor = new TestResponseInterceptor();
instanceId.setInterceptor(interceptor);
try {
instanceId.deleteInstanceIdAsync(null);
fail("No error thrown for null instance ID");
} catch (IllegalArgumentException expected) {
// expected
}
try {
instanceId.deleteInstanceIdAsync("");
fail("No error thrown for empty instance ID");
} catch (IllegalArgumentException expected) {
// expected
}
assertNull(interceptor.getResponse());
}
@Test
public void testDeleteInstanceId() throws Exception {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse().setContent("{}");
MockHttpTransport transport = new MockHttpTransport.Builder()
.setLowLevelHttpResponse(response)
.build();
FirebaseOptions options = APP_OPTIONS.toBuilder()
.setHttpTransport(transport)
.build();
FirebaseApp app = FirebaseApp.initializeApp(options);
final FirebaseInstanceId instanceId = FirebaseInstanceId.getInstance();
assertSame(instanceId, FirebaseInstanceId.getInstance(app));
List<GenericFunction<Void>> functions = ImmutableList.of(
new GenericFunction<Void>() {
@Override
public Void call(Object... args) throws Exception {
instanceId.deleteInstanceIdAsync("test-iid").get();
return null;
}
},
new GenericFunction<Void>() {
@Override
public Void call(Object... args) throws Exception {
instanceId.deleteInstanceId("test-iid");
return null;
}
}
);
for (GenericFunction<Void> fn : functions) {
TestResponseInterceptor interceptor = new TestResponseInterceptor();
instanceId.setInterceptor(interceptor);
fn.call();
assertNotNull(interceptor.getResponse());
HttpRequest request = interceptor.getResponse().getRequest();
assertEquals(HttpMethods.DELETE, request.getRequestMethod());
assertEquals(TEST_URL, request.getUrl().toString());
assertEquals("Bearer test-token", request.getHeaders().getAuthorization());
}
}
@Test
public void testDeleteInstanceIdError() throws Exception {
final MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
MockHttpTransport transport = new MockHttpTransport.Builder()
.setLowLevelHttpResponse(response)
.build();
FirebaseOptions options = APP_OPTIONS.toBuilder()
.setHttpTransport(transport)
.build();
FirebaseApp app = FirebaseApp.initializeApp(options);
// Disable retries by passing a regular HttpRequestFactory.
FirebaseInstanceId instanceId = new FirebaseInstanceId(app, transport.createRequestFactory());
TestResponseInterceptor interceptor = new TestResponseInterceptor();
instanceId.setInterceptor(interceptor);
try {
for (int statusCode : ERROR_CODES.keySet()) {
response.setStatusCode(statusCode).setContent("test error");
try {
instanceId.deleteInstanceIdAsync("test-iid").get();
fail("No error thrown for HTTP error");
} catch (ExecutionException e) {
assertTrue(e.getCause() instanceof FirebaseInstanceIdException);
checkFirebaseInstanceIdException((FirebaseInstanceIdException) e.getCause(), statusCode);
}
assertNotNull(interceptor.getResponse());
HttpRequest request = interceptor.getResponse().getRequest();
assertEquals(HttpMethods.DELETE, request.getRequestMethod());
assertEquals(TEST_URL, request.getUrl().toString());
}
} finally {
app.delete();
}
}
@Test
public void testDeleteInstanceIdTransportError() throws Exception {
HttpTransport transport = TestUtils.createFaultyHttpTransport();
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(new MockGoogleCredentials("test-token"))
.setProjectId("test-project")
.setHttpTransport(transport)
.build();
FirebaseApp app = FirebaseApp.initializeApp(options);
// Disable retries by passing a regular HttpRequestFactory.
FirebaseInstanceId instanceId = new FirebaseInstanceId(app, transport.createRequestFactory());
try {
instanceId.deleteInstanceIdAsync("test-iid").get();
fail("No error thrown for HTTP error");
} catch (ExecutionException e) {
assertTrue(e.getCause() instanceof FirebaseInstanceIdException);
FirebaseInstanceIdException error = (FirebaseInstanceIdException) e.getCause();
assertEquals(ErrorCode.UNKNOWN, error.getErrorCode());
assertEquals(
"Unknown error while making a remote service call: transport error",
error.getMessage());
assertTrue(error.getCause() instanceof IOException);
assertNull(error.getHttpResponse());
}
}
@Test
public void testDeleteInstanceIdInvalidJsonIgnored() throws Exception {
final MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
MockHttpTransport transport = new MockHttpTransport.Builder()
.setLowLevelHttpResponse(response)
.build();
FirebaseOptions options = FirebaseOptions.builder()
.setCredentials(new MockGoogleCredentials("test-token"))
.setProjectId("test-project")
.setHttpTransport(transport)
.build();
FirebaseApp app = FirebaseApp.initializeApp(options);
// Disable retries by passing a regular HttpRequestFactory.
FirebaseInstanceId instanceId = new FirebaseInstanceId(app, transport.createRequestFactory());
TestResponseInterceptor interceptor = new TestResponseInterceptor();
instanceId.setInterceptor(interceptor);
response.setContent("not json");
instanceId.deleteInstanceIdAsync("test-iid").get();
assertNotNull(interceptor.getResponse());
}
private void checkFirebaseInstanceIdException(FirebaseInstanceIdException error, int statusCode) {
assertEquals(ERROR_CODES.get(statusCode), error.getErrorCode());
assertEquals(ERROR_MESSAGES.get(statusCode), error.getMessage());
assertTrue(error.getCause() instanceof HttpResponseException);
IncomingHttpResponse httpResponse = error.getHttpResponse();
assertNotNull(httpResponse);
assertEquals(statusCode, httpResponse.getStatusCode());
OutgoingHttpRequest request = httpResponse.getRequest();
assertEquals(HttpMethods.DELETE, request.getMethod());
assertEquals(TEST_URL, request.getUrl());
}
}