Skip to content

Commit 050513f

Browse files
Lp 10190 support gzip (#318)
* accepted encodgin gzip * Leanplum feature flag * fixed feature flag * Update AndroidSDKCore/src/main/java/com/leanplum/internal/Util.java Co-Authored-By: hrishileanplum <[email protected]> * cleanup code * useragent gzip support added * support gzip response parsing * test cases added * test cases * cleanup * comment * trim logic
1 parent b3c0bb2 commit 050513f

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class Constants {
3838
public static int NETWORK_TIMEOUT_SECONDS_FOR_DOWNLOADS = 10;
3939
public static String LEANPLUM_VERSION = BuildConfig.SDK_VERSION + '.' + BuildConfig.BUILD_NUMBER;
4040
public static String CLIENT = "android";
41+
public static String LEANPLUM_SUPPORTED_ENCODING = "gzip";
4142

4243
static final String LEANPLUM_PACKAGE_IDENTIFIER = BuildConfig.LEANPLUM_PACKAGE_IDENTIFIER;
4344
static final String INVALID_MAC_ADDRESS = "02:00:00:00:00:00";

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import java.util.UUID;
8282
import java.util.concurrent.Executor;
8383
import java.util.concurrent.Executors;
84+
import java.util.zip.GZIPInputStream;
8485

8586
import javax.net.ssl.HttpsURLConnection;
8687
import javax.net.ssl.SSLSocketFactory;
@@ -553,10 +554,18 @@ static HttpURLConnection createHttpUrlConnection(
553554
urlConnection.setUseCaches(false);
554555
urlConnection.setInstanceFollowRedirects(true);
555556
Context context = Leanplum.getContext();
557+
558+
/*
559+
Must include `Accept-Encoding: gzip` in the header
560+
Must include the phrase `gzip` in the `User-Agent` header
561+
https://cloud.google.com/appengine/kb/
562+
*/
563+
556564
urlConnection.setRequestProperty("User-Agent",
557565
getApplicationName(context) + "/" + getVersionName() + "/" + RequestOld.appId() + "/" +
558566
Constants.CLIENT + "/" + Constants.LEANPLUM_VERSION + "/" + getSystemName() + "/" +
559-
getSystemVersion() + "/" + Constants.LEANPLUM_PACKAGE_IDENTIFIER);
567+
getSystemVersion() + "/" + Constants.LEANPLUM_SUPPORTED_ENCODING + "/" + Constants.LEANPLUM_PACKAGE_IDENTIFIER);
568+
urlConnection.setRequestProperty("Accept-Encoding", Constants.LEANPLUM_SUPPORTED_ENCODING);
560569
return urlConnection;
561570
}
562571

@@ -660,7 +669,12 @@ public static void saveResponse(URLConnection op, OutputStream outputStream) thr
660669
private static String getResponse(HttpURLConnection op) throws IOException {
661670
InputStream inputStream;
662671
if (op.getResponseCode() < 400) {
663-
inputStream = op.getInputStream();
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+
}
664678
} else {
665679
inputStream = op.getErrorStream();
666680
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.leanplum.internal;
2+
3+
import com.leanplum.__setup.LeanplumTestApp;
4+
import com.leanplum._whitebox.utilities.ResponseHelper;
5+
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.powermock.core.classloader.annotations.PowerMockIgnore;
10+
import org.powermock.core.classloader.annotations.PrepareForTest;
11+
import org.robolectric.RobolectricTestRunner;
12+
import org.robolectric.annotation.Config;
13+
14+
import java.net.HttpURLConnection;
15+
16+
import static junit.framework.Assert.assertNotNull;
17+
import static org.powermock.api.mockito.PowerMockito.mock;
18+
import static org.powermock.api.mockito.PowerMockito.when;
19+
20+
/**
21+
* Tests for {@link Util} class.
22+
*
23+
* @author Hrishi Amravatkar
24+
*/
25+
@RunWith(RobolectricTestRunner.class)
26+
@Config(
27+
sdk = 16,
28+
application = LeanplumTestApp.class
29+
)
30+
@PowerMockIgnore({
31+
"org.mockito.*",
32+
"org.robolectric.*",
33+
"org.json.*",
34+
"org.powermock.*",
35+
"android.*"
36+
})
37+
@PrepareForTest({Util.class})
38+
public class UtilTest {
39+
40+
/**
41+
* Runs before every test case.
42+
*/
43+
@Before
44+
public void setUp() {
45+
}
46+
47+
48+
/**
49+
* Test for {@link Util#getJsonResponse(HttpURLConnection op)} that returns no gzip unmarshalled data.
50+
*/
51+
@Test
52+
public void getNonGzipEncodedResponseWithNoContentEncodingTest() throws Exception {
53+
HttpURLConnection mockHttpUrlConnection = mock(HttpURLConnection.class);
54+
when(mockHttpUrlConnection.getInputStream()).thenReturn(ResponseHelper.class.getResourceAsStream("/responses/simple_start_response.json"));
55+
when(mockHttpUrlConnection.getResponseCode()).thenReturn(200);
56+
assertNotNull(Util.getJsonResponse(mockHttpUrlConnection));
57+
}
58+
59+
/**
60+
* Test for {@link Util#getJsonResponse(HttpURLConnection op)} that returns gzip unmarshalled data.
61+
*/
62+
@Test
63+
public void getGzipEncodedResponseWithContentEndingTest() throws Exception {
64+
HttpURLConnection mockHttpUrlConnection = mock(HttpURLConnection.class);
65+
when(mockHttpUrlConnection.getInputStream()).thenReturn(ResponseHelper.class.getResourceAsStream("/responses/simple_start_response.json.gz"));
66+
when(mockHttpUrlConnection.getResponseCode()).thenReturn(200);
67+
when(mockHttpUrlConnection.getHeaderField("content-encoding")).thenReturn("gzip");
68+
assertNotNull(Util.getJsonResponse(mockHttpUrlConnection));
69+
}
70+
71+
}
Binary file not shown.

0 commit comments

Comments
 (0)