16
16
17
17
import static com .google .common .truth .Truth .assertThat ;
18
18
import static com .google .firebase .appdistribution .impl .TestUtils .readTestFile ;
19
+ import static java .nio .charset .StandardCharsets .UTF_8 ;
19
20
import static org .junit .Assert .assertThrows ;
20
21
import static org .mockito .Mockito .verify ;
21
22
import static org .mockito .Mockito .when ;
22
23
23
24
import androidx .test .core .app .ApplicationProvider ;
24
25
import com .google .common .collect .Iterators ;
26
+ import com .google .common .io .ByteStreams ;
25
27
import com .google .firebase .FirebaseApp ;
26
28
import com .google .firebase .FirebaseOptions ;
27
29
import com .google .firebase .appdistribution .FirebaseAppDistributionException ;
28
30
import com .google .firebase .appdistribution .FirebaseAppDistributionException .Status ;
29
31
import java .io .ByteArrayInputStream ;
32
+ import java .io .ByteArrayOutputStream ;
30
33
import java .io .IOException ;
31
34
import java .io .InputStream ;
32
- import java .nio . charset . StandardCharsets ;
35
+ import java .util . zip . GZIPInputStream ;
33
36
import javax .net .ssl .HttpsURLConnection ;
34
37
import org .json .JSONObject ;
35
38
import org .junit .Before ;
@@ -51,6 +54,7 @@ public class TesterApiHttpClientTest {
51
54
private static final String TEST_URL =
52
55
String .format ("https://firebaseapptesters.googleapis.com/%s" , TEST_PATH );
53
56
private static final String TAG = "Test Tag" ;
57
+ private static final String TEST_POST_BODY = "Post body" ;
54
58
55
59
private TesterApiHttpClient testerApiHttpClient ;
56
60
@ Mock private HttpsURLConnection mockHttpsURLConnection ;
@@ -61,8 +65,6 @@ public void setup() throws Exception {
61
65
MockitoAnnotations .initMocks (this );
62
66
FirebaseApp .clearInstancesForTest ();
63
67
64
- when (mockHttpsURLConnectionFactory .openConnection (TEST_URL )).thenReturn (mockHttpsURLConnection );
65
-
66
68
FirebaseApp firebaseApp =
67
69
FirebaseApp .initializeApp (
68
70
ApplicationProvider .getApplicationContext (),
@@ -72,14 +74,15 @@ public void setup() throws Exception {
72
74
.setApiKey (TEST_API_KEY )
73
75
.build ());
74
76
77
+ when (mockHttpsURLConnectionFactory .openConnection (TEST_URL )).thenReturn (mockHttpsURLConnection );
78
+
75
79
testerApiHttpClient = new TesterApiHttpClient (firebaseApp , mockHttpsURLConnectionFactory );
76
80
}
77
81
78
82
@ Test
79
83
public void makeGetRequest_whenResponseSuccessful_returnsJsonResponse () throws Exception {
80
84
String responseJson = readTestFile ("testSimpleResponse.json" );
81
- InputStream responseInputStream =
82
- new ByteArrayInputStream (responseJson .getBytes (StandardCharsets .UTF_8 ));
85
+ InputStream responseInputStream = new ByteArrayInputStream (responseJson .getBytes (UTF_8 ));
83
86
when (mockHttpsURLConnection .getResponseCode ()).thenReturn (200 );
84
87
when (mockHttpsURLConnection .getInputStream ()).thenReturn (responseInputStream );
85
88
@@ -107,8 +110,7 @@ public void makeGetRequest_whenConnectionFails_throwsError() throws Exception {
107
110
108
111
@ Test
109
112
public void makeGetRequest_whenInvalidJson_throwsError () throws Exception {
110
- InputStream response =
111
- new ByteArrayInputStream (INVALID_RESPONSE .getBytes (StandardCharsets .UTF_8 ));
113
+ InputStream response = new ByteArrayInputStream (INVALID_RESPONSE .getBytes (UTF_8 ));
112
114
when (mockHttpsURLConnection .getInputStream ()).thenReturn (response );
113
115
when (mockHttpsURLConnection .getResponseCode ()).thenReturn (200 );
114
116
@@ -186,4 +188,68 @@ public void makeGetRequest_whenResponseFailsWithUnknownCode_throwsError() throws
186
188
assertThat (e .getMessage ()).contains ("409" );
187
189
verify (mockHttpsURLConnection ).disconnect ();
188
190
}
191
+
192
+ @ Test
193
+ public void makePostRequest_zipsRequestBodyAndSetsCorrectHeaders () throws Exception {
194
+ String responseJson = readTestFile ("testSimpleResponse.json" );
195
+ InputStream responseInputStream = new ByteArrayInputStream (responseJson .getBytes (UTF_8 ));
196
+ when (mockHttpsURLConnection .getResponseCode ()).thenReturn (200 );
197
+ when (mockHttpsURLConnection .getInputStream ()).thenReturn (responseInputStream );
198
+ ByteArrayOutputStream requestBodyOutputStream = new ByteArrayOutputStream ();
199
+ when (mockHttpsURLConnection .getOutputStream ()).thenReturn (requestBodyOutputStream );
200
+
201
+ testerApiHttpClient .makePostRequest (TAG , TEST_PATH , TEST_AUTH_TOKEN , TEST_POST_BODY );
202
+
203
+ byte [] unzippedPostBody =
204
+ ByteStreams .toByteArray (
205
+ new GZIPInputStream (new ByteArrayInputStream (requestBodyOutputStream .toByteArray ())));
206
+ assertThat (new String (unzippedPostBody , UTF_8 )).isEqualTo (TEST_POST_BODY );
207
+ verify (mockHttpsURLConnection ).setDoOutput (true );
208
+ verify (mockHttpsURLConnection ).setRequestMethod ("POST" );
209
+ verify (mockHttpsURLConnection ).addRequestProperty ("Content-Type" , "application/json" );
210
+ verify (mockHttpsURLConnection ).addRequestProperty ("Content-Encoding" , "gzip" );
211
+ verify (mockHttpsURLConnection ).disconnect ();
212
+ }
213
+
214
+ @ Test
215
+ public void makePostRequest_whenConnectionFails_throwsError () throws Exception {
216
+ IOException caughtException = new IOException ("error" );
217
+ when (mockHttpsURLConnectionFactory .openConnection (TEST_URL )).thenThrow (caughtException );
218
+
219
+ FirebaseAppDistributionException e =
220
+ assertThrows (
221
+ FirebaseAppDistributionException .class ,
222
+ () ->
223
+ testerApiHttpClient .makePostRequest (
224
+ TAG , TEST_PATH , TEST_AUTH_TOKEN , TEST_POST_BODY ));
225
+
226
+ assertThat (e .getErrorCode ()).isEqualTo (Status .NETWORK_FAILURE );
227
+ assertThat (e .getMessage ()).contains (TAG );
228
+ assertThat (e .getMessage ()).contains (ErrorMessages .NETWORK_ERROR );
229
+ }
230
+
231
+ @ Test
232
+ public void makeUploadRequest_zipsRequestBodyAndSetsCorrectHeaders () throws Exception {
233
+ String responseJson = readTestFile ("testSimpleResponse.json" );
234
+ InputStream responseInputStream = new ByteArrayInputStream (responseJson .getBytes (UTF_8 ));
235
+ when (mockHttpsURLConnection .getResponseCode ()).thenReturn (200 );
236
+ when (mockHttpsURLConnection .getInputStream ()).thenReturn (responseInputStream );
237
+ ByteArrayOutputStream requestBodyOutputStream = new ByteArrayOutputStream ();
238
+ when (mockHttpsURLConnection .getOutputStream ()).thenReturn (requestBodyOutputStream );
239
+
240
+ testerApiHttpClient .makeUploadRequest (
241
+ TAG , TEST_PATH , TEST_AUTH_TOKEN , TEST_POST_BODY .getBytes (UTF_8 ));
242
+
243
+ byte [] unzippedPostBody =
244
+ ByteStreams .toByteArray (
245
+ new GZIPInputStream (new ByteArrayInputStream (requestBodyOutputStream .toByteArray ())));
246
+ assertThat (new String (unzippedPostBody , UTF_8 )).isEqualTo (TEST_POST_BODY );
247
+ verify (mockHttpsURLConnection ).setDoOutput (true );
248
+ verify (mockHttpsURLConnection ).setRequestMethod ("POST" );
249
+ verify (mockHttpsURLConnection ).addRequestProperty ("Content-Type" , "application/json" );
250
+ verify (mockHttpsURLConnection ).addRequestProperty ("Content-Encoding" , "gzip" );
251
+ verify (mockHttpsURLConnection ).addRequestProperty ("X-Goog-Upload-Protocol" , "raw" );
252
+ verify (mockHttpsURLConnection ).addRequestProperty ("X-Goog-Upload-File-Name" , "screenshot.png" );
253
+ verify (mockHttpsURLConnection ).disconnect ();
254
+ }
189
255
}
0 commit comments