Skip to content

Commit 3d91fd9

Browse files
committed
Address Kai's feedback
1 parent 7b4bba8 commit 3d91fd9

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/impl/TesterApiHttpClient.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
class TesterApiHttpClient {
4040

4141
private static final String APP_TESTERS_HOST = "firebaseapptesters.googleapis.com";
42-
private static final String APP_TESTERS_UPLOAD_HOST = "firebaseapptesters.clients6.google.com";
4342
private static final String REQUEST_METHOD_GET = "GET";
4443
private static final String REQUEST_METHOD_POST = "POST";
4544
private static final String CONTENT_TYPE_HEADER_KEY = "Content-Type";
@@ -56,6 +55,7 @@ class TesterApiHttpClient {
5655
private static final String X_GOOG_UPLOAD_PROTOCOL_RAW = "raw";
5756
private static final String X_GOOG_UPLOAD_FILE_NAME_HEADER = "X-Goog-Upload-File-Name";
5857
private static final String X_GOOG_UPLOAD_FILE_NAME = "screenshot.png";
58+
// StandardCharsets.UTF_8 requires API level 19
5959
private static final String UTF_8 = "UTF-8";
6060

6161
private static final String TAG = "TesterApiClient:";
@@ -85,7 +85,7 @@ JSONObject makeGetRequest(String tag, String path, String token)
8585
throws FirebaseAppDistributionException {
8686
HttpsURLConnection connection = null;
8787
try {
88-
connection = openHttpsUrlConnection(getTesterApiUrl(APP_TESTERS_HOST, path), token);
88+
connection = openHttpsUrlConnection(getTesterApiUrl(path), token);
8989
return readResponse(tag, connection);
9090
} catch (IOException e) {
9191
throw getException(tag, ErrorMessages.NETWORK_ERROR, Status.NETWORK_FAILURE, e);
@@ -110,7 +110,7 @@ JSONObject makePostRequest(String tag, String path, String token, String request
110110
throw new FirebaseAppDistributionException(
111111
"Unsupported encoding: " + UTF_8, Status.UNKNOWN, e);
112112
}
113-
return makePostRequest(tag, APP_TESTERS_HOST, path, token, bytes, new HashMap<>());
113+
return makePostRequest(tag, path, token, bytes, new HashMap<>());
114114
}
115115

116116
/**
@@ -125,20 +125,19 @@ JSONObject makeUploadRequest(String tag, String path, String token, byte[] reque
125125
Map<String, String> extraHeaders = new HashMap<>();
126126
extraHeaders.put(X_GOOG_UPLOAD_PROTOCOL_HEADER, X_GOOG_UPLOAD_PROTOCOL_RAW);
127127
extraHeaders.put(X_GOOG_UPLOAD_FILE_NAME_HEADER, X_GOOG_UPLOAD_FILE_NAME);
128-
return makePostRequest(tag, APP_TESTERS_UPLOAD_HOST, path, token, requestBody, extraHeaders);
128+
return makePostRequest(tag, path, token, requestBody, extraHeaders);
129129
}
130130

131131
private JSONObject makePostRequest(
132132
String tag,
133-
String host,
134133
String path,
135134
String token,
136135
byte[] requestBody,
137136
Map<String, String> extraHeaders)
138137
throws FirebaseAppDistributionException {
139138
HttpsURLConnection connection = null;
140139
try {
141-
connection = openHttpsUrlConnection(getTesterApiUrl(host, path), token);
140+
connection = openHttpsUrlConnection(getTesterApiUrl(path), token);
142141
connection.setDoOutput(true);
143142
connection.setRequestMethod(REQUEST_METHOD_POST);
144143
connection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE);
@@ -164,8 +163,8 @@ private JSONObject makePostRequest(
164163
}
165164
}
166165

167-
private static String getTesterApiUrl(String host, String path) {
168-
return String.format("https://%s/%s", host, path);
166+
private static String getTesterApiUrl(String path) {
167+
return String.format("https://%s/%s", APP_TESTERS_HOST, path);
169168
}
170169

171170
private static JSONObject readResponse(String tag, HttpsURLConnection connection)

firebase-appdistribution/src/test/java/com/google/firebase/appdistribution/impl/TesterApiHttpClientTest.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ public class TesterApiHttpClientTest {
5353
private static final String TEST_PATH = "some/url/path";
5454
private static final String TEST_URL =
5555
String.format("https://firebaseapptesters.googleapis.com/%s", TEST_PATH);
56-
private static final String TEST_UPLOAD_URL =
57-
String.format("https://firebaseapptesters.clients6.google.com/%s", TEST_PATH);
5856
private static final String TAG = "Test Tag";
5957
private static final String TEST_POST_BODY = "Post body";
6058

@@ -76,12 +74,13 @@ public void setup() throws Exception {
7674
.setApiKey(TEST_API_KEY)
7775
.build());
7876

77+
when(mockHttpsURLConnectionFactory.openConnection(TEST_URL)).thenReturn(mockHttpsURLConnection);
78+
7979
testerApiHttpClient = new TesterApiHttpClient(firebaseApp, mockHttpsURLConnectionFactory);
8080
}
8181

8282
@Test
8383
public void makeGetRequest_whenResponseSuccessful_returnsJsonResponse() throws Exception {
84-
when(mockHttpsURLConnectionFactory.openConnection(TEST_URL)).thenReturn(mockHttpsURLConnection);
8584
String responseJson = readTestFile("testSimpleResponse.json");
8685
InputStream responseInputStream =
8786
new ByteArrayInputStream(responseJson.getBytes(UTF_8));
@@ -97,7 +96,6 @@ public void makeGetRequest_whenResponseSuccessful_returnsJsonResponse() throws E
9796

9897
@Test
9998
public void makeGetRequest_whenConnectionFails_throwsError() throws Exception {
100-
when(mockHttpsURLConnectionFactory.openConnection(TEST_URL)).thenReturn(mockHttpsURLConnection);
10199
IOException caughtException = new IOException("error");
102100
when(mockHttpsURLConnectionFactory.openConnection(TEST_URL)).thenThrow(caughtException);
103101

@@ -113,7 +111,6 @@ public void makeGetRequest_whenConnectionFails_throwsError() throws Exception {
113111

114112
@Test
115113
public void makeGetRequest_whenInvalidJson_throwsError() throws Exception {
116-
when(mockHttpsURLConnectionFactory.openConnection(TEST_URL)).thenReturn(mockHttpsURLConnection);
117114
InputStream response =
118115
new ByteArrayInputStream(INVALID_RESPONSE.getBytes(UTF_8));
119116
when(mockHttpsURLConnection.getInputStream()).thenReturn(response);
@@ -132,7 +129,6 @@ public void makeGetRequest_whenInvalidJson_throwsError() throws Exception {
132129

133130
@Test
134131
public void makeGetRequest_whenResponseFailsWith401_throwsError() throws Exception {
135-
when(mockHttpsURLConnectionFactory.openConnection(TEST_URL)).thenReturn(mockHttpsURLConnection);
136132
when(mockHttpsURLConnection.getResponseCode()).thenReturn(401);
137133
when(mockHttpsURLConnection.getInputStream()).thenThrow(new IOException("error"));
138134

@@ -149,7 +145,6 @@ public void makeGetRequest_whenResponseFailsWith401_throwsError() throws Excepti
149145

150146
@Test
151147
public void makeGetRequest_whenResponseFailsWith403_throwsError() throws Exception {
152-
when(mockHttpsURLConnectionFactory.openConnection(TEST_URL)).thenReturn(mockHttpsURLConnection);
153148
when(mockHttpsURLConnection.getResponseCode()).thenReturn(403);
154149
when(mockHttpsURLConnection.getInputStream()).thenThrow(new IOException("error"));
155150

@@ -166,7 +161,6 @@ public void makeGetRequest_whenResponseFailsWith403_throwsError() throws Excepti
166161

167162
@Test
168163
public void makeGetRequest_whenResponseFailsWith404_throwsError() throws Exception {
169-
when(mockHttpsURLConnectionFactory.openConnection(TEST_URL)).thenReturn(mockHttpsURLConnection);
170164
when(mockHttpsURLConnection.getResponseCode()).thenReturn(404);
171165
when(mockHttpsURLConnection.getInputStream()).thenThrow(new IOException("error"));
172166

@@ -183,7 +177,6 @@ public void makeGetRequest_whenResponseFailsWith404_throwsError() throws Excepti
183177

184178
@Test
185179
public void makeGetRequest_whenResponseFailsWithUnknownCode_throwsError() throws Exception {
186-
when(mockHttpsURLConnectionFactory.openConnection(TEST_URL)).thenReturn(mockHttpsURLConnection);
187180
when(mockHttpsURLConnection.getResponseCode()).thenReturn(409);
188181
when(mockHttpsURLConnection.getInputStream()).thenThrow(new IOException("error"));
189182

@@ -200,7 +193,6 @@ public void makeGetRequest_whenResponseFailsWithUnknownCode_throwsError() throws
200193

201194
@Test
202195
public void makePostRequest_zipsRequestBodyAndSetsCorrectHeaders() throws Exception {
203-
when(mockHttpsURLConnectionFactory.openConnection(TEST_URL)).thenReturn(mockHttpsURLConnection);
204196
String responseJson = readTestFile("testSimpleResponse.json");
205197
InputStream responseInputStream =
206198
new ByteArrayInputStream(responseJson.getBytes(UTF_8));
@@ -224,7 +216,6 @@ public void makePostRequest_zipsRequestBodyAndSetsCorrectHeaders() throws Except
224216

225217
@Test
226218
public void makePostRequest_whenConnectionFails_throwsError() throws Exception {
227-
when(mockHttpsURLConnectionFactory.openConnection(TEST_URL)).thenReturn(mockHttpsURLConnection);
228219
IOException caughtException = new IOException("error");
229220
when(mockHttpsURLConnectionFactory.openConnection(TEST_URL)).thenThrow(caughtException);
230221

@@ -240,7 +231,6 @@ public void makePostRequest_whenConnectionFails_throwsError() throws Exception {
240231

241232
@Test
242233
public void makeUploadRequest_zipsRequestBodyAndSetsCorrectHeaders() throws Exception {
243-
when(mockHttpsURLConnectionFactory.openConnection(TEST_UPLOAD_URL)).thenReturn(mockHttpsURLConnection);
244234
String responseJson = readTestFile("testSimpleResponse.json");
245235
InputStream responseInputStream =
246236
new ByteArrayInputStream(responseJson.getBytes(UTF_8));

0 commit comments

Comments
 (0)