-
Notifications
You must be signed in to change notification settings - Fork 626
Fix the way of gzipping and base64. This is now consistent with server #3414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
de90983
f60cdae
94da184
6dbf227
8d30036
64f6e7d
c1f6d75
b6a8d3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
package com.google.firebase.heartbeatinfo; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
|
@@ -40,9 +41,7 @@ | |
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.zip.GZIPOutputStream; | ||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
@@ -115,13 +114,9 @@ public void generateHeartBeat_oneHeartBeat() | |
heartBeatController | ||
.getHeartBeatsHeader() | ||
.addOnCompleteListener(executor, getOnCompleteListener); | ||
String expected = | ||
Base64.getUrlEncoder() | ||
.withoutPadding() | ||
.encodeToString( | ||
compress( | ||
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"date\":\"[2015-02-03]\"}],\"version\":\"2\"}") | ||
.getBytes()); | ||
String str = | ||
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"dates\":[\"2015-02-03\"]}],\"version\":\"2\"}"; | ||
String expected = compress(str); | ||
assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected); | ||
} | ||
|
||
|
@@ -162,10 +157,7 @@ public void firstOldThenNew_synchronizedCorrectly() | |
DefaultHeartBeatController controller = | ||
new DefaultHeartBeatController( | ||
() -> heartBeatInfoStorage, logSources, executor, () -> publisher, context); | ||
String emptyString = | ||
Base64.getUrlEncoder() | ||
.withoutPadding() | ||
.encodeToString(compress("{\"heartbeats\":[],\"version\":\"2\"}").getBytes()); | ||
String emptyString = compress("{\"heartbeats\":[],\"version\":\"2\"}"); | ||
controller.registerHeartBeat().addOnCompleteListener(executor, storeOnCompleteListener); | ||
storeOnCompleteListener.await(); | ||
int heartBeatCode = controller.getHeartBeatCode("test").getCode(); | ||
|
@@ -202,27 +194,32 @@ public void generateHeartBeat_twoHeartBeatsSameUserAgent() | |
heartBeatController | ||
.getHeartBeatsHeader() | ||
.addOnCompleteListener(executor, getOnCompleteListener); | ||
JSONObject output = new JSONObject(); | ||
JSONArray array = new JSONArray(); | ||
JSONObject obj = new JSONObject(); | ||
obj.put("agent", "test-agent"); | ||
obj.put("date", dateList); | ||
array.put(obj); | ||
output.put("heartbeats", array); | ||
output.put("version", "2"); | ||
String expected = | ||
Base64.getUrlEncoder() | ||
.withoutPadding() | ||
.encodeToString(compress(output.toString()).getBytes()); | ||
String str = | ||
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"dates\":[\"2015-03-02\",\"2015-03-01\"]}],\"version\":\"2\"}"; | ||
String expected = compress(str); | ||
assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected); | ||
} | ||
|
||
private static String base64Encode(byte[] input) { | ||
return Base64.getUrlEncoder().withoutPadding().encodeToString(input); | ||
} | ||
|
||
private static byte[] gzip(String input) { | ||
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); | ||
try { | ||
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteOutputStream)) { | ||
gzipOutputStream.write(input.getBytes(UTF_8)); | ||
} | ||
byte[] gzipped = byteOutputStream.toByteArray(); | ||
byteOutputStream.close(); | ||
Comment on lines
+208
to
+214
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you can use try with resources here as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it complains saying byte[] is not a proper type for try with resources. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK not closing it is OK. https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayOutputStream.html#close()
|
||
return gzipped; | ||
} catch (IOException e) { | ||
return null; | ||
} | ||
} | ||
|
||
private String compress(String str) throws IOException { | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
GZIPOutputStream gzip = new GZIPOutputStream(out); | ||
gzip.write(str.toString().getBytes()); | ||
gzip.close(); | ||
return out.toString("UTF-8"); | ||
return base64Encode(gzip(str)); | ||
} | ||
|
||
@Config(sdk = 29) | ||
|
@@ -250,13 +247,9 @@ public void generateHeartBeat_twoHeartBeatstwoUserAgents() | |
heartBeatController | ||
.getHeartBeatsHeader() | ||
.addOnCompleteListener(executor, getOnCompleteListener); | ||
String expected = | ||
Base64.getUrlEncoder() | ||
.withoutPadding() | ||
.encodeToString( | ||
compress( | ||
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"date\":\"[2015-03-02]\"},{\"agent\":\"test-agent-1\",\"date\":\"[2015-03-03]\"}],\"version\":\"2\"}") | ||
.getBytes()); | ||
String str = | ||
"{\"heartbeats\":[{\"agent\":\"test-agent\",\"dates\":[\"2015-03-02\"]},{\"agent\":\"test-agent-1\",\"dates\":[\"2015-03-03\"]}],\"version\":\"2\"}"; | ||
String expected = compress(str); | ||
assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.