Repository navigation
Serve push notification test image from MockWebServer #1097
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,11 +38,18 @@ | |
| import org.json.JSONArray; | ||
| import org.json.JSONObject; | ||
|
|
||
| import okhttp3.mockwebserver.Dispatcher; | ||
| import okhttp3.mockwebserver.MockResponse; | ||
| import okhttp3.mockwebserver.MockWebServer; | ||
| import okhttp3.mockwebserver.RecordedRequest; | ||
| import okio.Buffer; | ||
|
|
||
| @RunWith(RobolectricTestRunner.class) | ||
| @Config(sdk = {Build.VERSION_CODES.P}) | ||
| public class IterableNotificationTest { | ||
| Context appContext; | ||
| NotificationManager mNotificationManager; | ||
| private MockWebServer imageServer; | ||
|
|
||
| //EX: itbl_notif = "{\"itbl\":{\"templateId\":28,\"campaignId\":17,\"messageId\":\"85fbb5663ada4f40b5ae0d437e040fa6\",\"isGhostPush\":false},\"body\":\"Pushy\",\"badge\":2,\"sound\":\"me.wav\"}}"; | ||
|
|
||
|
|
@@ -51,7 +58,7 @@ public class IterableNotificationTest { | |
| String itbl_ghost = "{\"templateId\":1,\"campaignId\":1,\"messageId\":\"11111111111111111111111111111111\",\"isGhostPush\":true}"; | ||
| String itbl1 = "{\"templateId\":1,\"campaignId\":1,\"messageId\":\"11111111111111111111111111111111\",\"isGhostPush\":false}"; | ||
| String itbl2 = "{\"templateId\":2,\"campaignId\":2,\"messageId\":\"22222222222222222222222222222222\",\"isGhostPush\":false}}"; | ||
| String itbl_image = "{\"templateId\":1,\"campaignId\":1,\"messageId\":\"11111111111111111111111111111111\",\"isGhostPush\":false,\"attachment-url\":\"https://assets.iterable.com/assets/images/logos/itbl-logo-full-gray-800x300.png\"}"; | ||
| String itbl_image; | ||
|
|
||
| private Context getContext() { | ||
| return getApplicationContext(); | ||
|
|
@@ -80,17 +87,42 @@ private String getResourceString(String fileName) throws IOException { | |
| return stringBuilder.toString(); | ||
| } | ||
|
|
||
| private byte[] getResourceBytes(String fileName) throws IOException { | ||
| InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName); | ||
| Buffer buffer = new Buffer(); | ||
| buffer.readFrom(inputStream); | ||
| inputStream.close(); | ||
| return buffer.readByteArray(); | ||
| } | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| appContext = getContext().getApplicationContext(); | ||
| mNotificationManager = (NotificationManager) | ||
| getContext().getSystemService(Context.NOTIFICATION_SERVICE); | ||
| mNotificationManager.cancelAll(); | ||
|
|
||
| final byte[] imageBytes = getResourceBytes("350x150.png"); | ||
| imageServer = new MockWebServer(); | ||
| // Every notification build() re-downloads the attachment, so serve the image on | ||
| // every request instead of enqueuing a fixed number of responses. | ||
| imageServer.setDispatcher(new Dispatcher() { | ||
| @Override | ||
| public MockResponse dispatch(RecordedRequest request) { | ||
| return new MockResponse() | ||
| .setHeader("Content-Type", "image/png") | ||
| .setBody(new Buffer().write(imageBytes)); | ||
| } | ||
| }); | ||
| imageServer.start(); | ||
|
|
||
| itbl_image = "{\"templateId\":1,\"campaignId\":1,\"messageId\":\"11111111111111111111111111111111\",\"isGhostPush\":false,\"attachment-url\":\"" + imageServer.url("/push-image.png") + "\"}"; | ||
|
Comment on lines
+105
to
+119
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. Also What the code does: The branch serves the push image from a MockWebServer started in every What the spec says: Commit Why it conflicts: Suggested action: Rebase onto |
||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| public void tearDown() throws Exception { | ||
| mNotificationManager.cancelAll(); | ||
| imageServer.shutdown(); | ||
| } | ||
|
|
||
| @Test | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also ~L309–323: the 500 chain can still outlive the test, and a stray request still consumes the next script
What the code does:
testResponseCode500stubs 500, 500, then 200, then returns after the secondtakeRequest. The dispatcherpoll()s a scripted response first and, only when the deque is empty, returns the samelastServedResponseinstance.tearDowndoes not drain the main looper.What the spec says: Commit
080825afsays to end the chain with a success so it cannot outlive the test, and to repeat the last response so a stray request is harmless.Why it conflicts: Attempt 3 is the 200, scheduled with delay 0 (
retryCount > 2is false for the second 500) and never awaited, so it can run afteroverrideURLEndpointPathpoints at the next server. If that test has already calledstubResponse,poll()takes its scripted response. Repeating only happens when the deque is empty. OkHttp MockWebServer 4.9 writes that body withthrottledTransfer, which reads theBufferuntil it is empty, so a second use of the sameMockResponsehas an empty body.Suggested action: On the rebase, wait until the terminal success has been received before the test returns, and have the dispatcher return a new copy of the fallback response on every call so it neither steals a scripted response nor serves a drained body.