Skip to content

Commit da9715e

Browse files
E2/lp 10190 fix compressed content error (#353)
* Uncompress error responses too * test cases added
1 parent a4a8ec1 commit da9715e

File tree

2 files changed

+20
-6
lines changed
  • AndroidSDKCore/src/main/java/com/leanplum/internal
  • AndroidSDKTests/src/test/java/com/leanplum/internal

2 files changed

+20
-6
lines changed

AndroidSDKCore/src/main/java/com/leanplum/internal/Util.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -668,16 +668,17 @@ public static void saveResponse(URLConnection op, OutputStream outputStream) thr
668668

669669
private static String getResponse(HttpURLConnection op) throws IOException {
670670
InputStream inputStream;
671+
String contentHeader = op.getHeaderField("content-encoding");
672+
boolean isCompressed = contentHeader != null && contentHeader.trim().equalsIgnoreCase(Constants.LEANPLUM_SUPPORTED_ENCODING);
671673
if (op.getResponseCode() < 400) {
672-
String contentHeader = op.getHeaderField("content-encoding");
673-
if (contentHeader != null && contentHeader.trim().equalsIgnoreCase(Constants.LEANPLUM_SUPPORTED_ENCODING)) {
674-
inputStream = new GZIPInputStream(op.getInputStream());
675-
} else {
676-
inputStream = op.getInputStream();
677-
}
674+
inputStream = op.getInputStream();
678675
} else {
679676
inputStream = op.getErrorStream();
680677
}
678+
679+
// If we have a gzipped response, de-compress it first
680+
if (isCompressed) inputStream = new GZIPInputStream(inputStream);
681+
681682
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
682683
StringBuilder builder = new StringBuilder();
683684
for (String line; (line = reader.readLine()) != null; ) {

AndroidSDKTests/src/test/java/com/leanplum/internal/UtilTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,17 @@ public void getGzipEncodedResponseWithContentEndingTest() throws Exception {
6868
assertNotNull(Util.getJsonResponse(mockHttpUrlConnection));
6969
}
7070

71+
72+
/**
73+
* Test for {@link Util#getJsonResponse(HttpURLConnection op)} that returns gzip unmarshalled error data.
74+
*/
75+
@Test
76+
public void getGzipEncodedErrorResponseWithContentEndingTest() throws Exception {
77+
HttpURLConnection mockHttpUrlConnection = mock(HttpURLConnection.class);
78+
when(mockHttpUrlConnection.getErrorStream()).thenReturn(ResponseHelper.class.getResourceAsStream("/responses/simple_start_response.json.gz"));
79+
when(mockHttpUrlConnection.getResponseCode()).thenReturn(403);
80+
when(mockHttpUrlConnection.getHeaderField("content-encoding")).thenReturn("gzip");
81+
assertNotNull(Util.getJsonResponse(mockHttpUrlConnection));
82+
}
83+
7184
}

0 commit comments

Comments
 (0)