From 32bc20803b8564063d58aa68c94402cb19c6ca2c Mon Sep 17 00:00:00 2001 From: Ciaran Lawlor Date: Tue, 5 May 2020 17:48:44 +0100 Subject: [PATCH] Fix downloads issue (#26) * Use byte arrays for downloads instead of input streams * Bump version --- onfido-java/pom.xml | 2 +- .../main/java/com/onfido/api/FileDownload.java | 6 ++---- .../java/com/onfido/api/ResourceManager.java | 2 +- .../onfido/integration/ApiIntegrationTest.java | 6 ++---- .../onfido/integration/DocumentManagerTest.java | 9 +++++---- .../integration/LivePhotoManagerTest.java | 9 +++++---- .../integration/LiveVideoManagerTest.java | 17 +++++++++-------- 7 files changed, 25 insertions(+), 26 deletions(-) diff --git a/onfido-java/pom.xml b/onfido-java/pom.xml index 6d9fd808..56137781 100644 --- a/onfido-java/pom.xml +++ b/onfido-java/pom.xml @@ -4,7 +4,7 @@ com.onfido onfido-api-java - 1.1.1 + 1.1.2 Onfido API Java Client Official Java API client library for the Onfido API diff --git a/onfido-java/src/main/java/com/onfido/api/FileDownload.java b/onfido-java/src/main/java/com/onfido/api/FileDownload.java index 81ff8613..2d0028ab 100644 --- a/onfido-java/src/main/java/com/onfido/api/FileDownload.java +++ b/onfido-java/src/main/java/com/onfido/api/FileDownload.java @@ -1,14 +1,12 @@ package com.onfido.api; -import java.io.InputStream; - /** * A wrapper class for any file downloaded from the API. Stores an content as an InputStream and * file type as a String. */ public class FileDownload { /** The content of the downloaded file. */ - public final InputStream content; + public final byte[] content; /** The file type of the associated content. */ public final String contentType; @@ -18,7 +16,7 @@ public class FileDownload { * @param content the content of the file * @param contentType the file type of the associated content */ - public FileDownload(InputStream content, String contentType) { + public FileDownload(byte[] content, String contentType) { this.content = content; this.contentType = contentType; } diff --git a/onfido-java/src/main/java/com/onfido/api/ResourceManager.java b/onfido-java/src/main/java/com/onfido/api/ResourceManager.java index ea35da1e..d7e653da 100644 --- a/onfido-java/src/main/java/com/onfido/api/ResourceManager.java +++ b/onfido-java/src/main/java/com/onfido/api/ResourceManager.java @@ -156,7 +156,7 @@ private String performRequest(Request request) throws OnfidoException { private FileDownload performDownload(Request request) throws OnfidoException { try (Response response = client.newCall(request).execute()) { if (response.isSuccessful()) { - return new FileDownload(response.body().byteStream(), response.header("content-type")); + return new FileDownload(response.body().bytes(), response.header("content-type")); } else { throw ApiException.fromResponseBody(response.body().string(), response.code()); } diff --git a/onfido-java/src/test/java/com/onfido/integration/ApiIntegrationTest.java b/onfido-java/src/test/java/com/onfido/integration/ApiIntegrationTest.java index dd748c81..166ddbbd 100644 --- a/onfido-java/src/test/java/com/onfido/integration/ApiIntegrationTest.java +++ b/onfido-java/src/test/java/com/onfido/integration/ApiIntegrationTest.java @@ -30,11 +30,9 @@ protected MockWebServer mockRequestResponse(String response) throws IOException return server; } - protected MockWebServer mockFileRequestResponse() throws IOException { - Buffer buffer = new Buffer(); - buffer.writeInt(5); + protected MockWebServer mockFileRequestResponse(String content, String type) throws IOException { server = new MockWebServer(); - server.enqueue(new MockResponse().setBody(buffer)); + server.enqueue(new MockResponse().setBody(content).addHeader("content-type", type)); server.start(); return server; diff --git a/onfido-java/src/test/java/com/onfido/integration/DocumentManagerTest.java b/onfido-java/src/test/java/com/onfido/integration/DocumentManagerTest.java index 319e1809..680e881e 100644 --- a/onfido-java/src/test/java/com/onfido/integration/DocumentManagerTest.java +++ b/onfido-java/src/test/java/com/onfido/integration/DocumentManagerTest.java @@ -1,10 +1,10 @@ package com.onfido.integration; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import com.onfido.JsonObject; import com.onfido.Onfido; +import com.onfido.api.FileDownload; import com.onfido.exceptions.ApiException; import com.onfido.models.Document; import java.io.ByteArrayInputStream; @@ -58,19 +58,20 @@ public void uploadDocument() throws Exception { @Test public void downloadDocument() throws Exception { - MockWebServer server = mockFileRequestResponse(); + MockWebServer server = mockFileRequestResponse("test", "image/png"); Onfido onfido = Onfido.builder().apiToken("token").unknownApiUrl(server.url("/").toString()).build(); - InputStream inputStream = onfido.document.download("document id").content; + FileDownload download = onfido.document.download("document id"); // Correct path RecordedRequest request = server.takeRequest(); assertEquals("/documents/document%20id/download", request.getPath()); // Correct response body - assertTrue(inputStream != null); + assertEquals("test", new String(download.content)); + assertEquals("image/png", download.contentType); } @Test diff --git a/onfido-java/src/test/java/com/onfido/integration/LivePhotoManagerTest.java b/onfido-java/src/test/java/com/onfido/integration/LivePhotoManagerTest.java index 6169d052..ea9b2fdd 100644 --- a/onfido-java/src/test/java/com/onfido/integration/LivePhotoManagerTest.java +++ b/onfido-java/src/test/java/com/onfido/integration/LivePhotoManagerTest.java @@ -1,10 +1,10 @@ package com.onfido.integration; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import com.onfido.JsonObject; import com.onfido.Onfido; +import com.onfido.api.FileDownload; import com.onfido.exceptions.ApiException; import com.onfido.models.LivePhoto; import java.io.ByteArrayInputStream; @@ -49,19 +49,20 @@ public void uploadLivePhoto() throws Exception { @Test public void downloadLivePhoto() throws Exception { - MockWebServer server = mockFileRequestResponse(); + MockWebServer server = mockFileRequestResponse("test", "image/png"); Onfido onfido = Onfido.builder().apiToken("token").unknownApiUrl(server.url("/").toString()).build(); - InputStream inputStream = onfido.livePhoto.download("live photo id").content; + FileDownload download = onfido.livePhoto.download("live photo id"); // Correct path RecordedRequest request = server.takeRequest(); assertEquals("/live_photos/live%20photo%20id/download", request.getPath()); // Correct response body - assertNotNull(inputStream); + assertEquals("test", new String(download.content)); + assertEquals("image/png", download.contentType); } @Test diff --git a/onfido-java/src/test/java/com/onfido/integration/LiveVideoManagerTest.java b/onfido-java/src/test/java/com/onfido/integration/LiveVideoManagerTest.java index 11822c34..84c9c048 100644 --- a/onfido-java/src/test/java/com/onfido/integration/LiveVideoManagerTest.java +++ b/onfido-java/src/test/java/com/onfido/integration/LiveVideoManagerTest.java @@ -1,13 +1,12 @@ package com.onfido.integration; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import com.onfido.JsonObject; import com.onfido.Onfido; +import com.onfido.api.FileDownload; import com.onfido.exceptions.ApiException; import com.onfido.models.LiveVideo; -import java.io.InputStream; import java.util.Arrays; import java.util.List; import okhttp3.mockwebserver.MockWebServer; @@ -19,36 +18,38 @@ public class LiveVideoManagerTest extends ApiIntegrationTest { @Test public void downloadLiveVideo() throws Exception { - MockWebServer server = mockFileRequestResponse(); + MockWebServer server = mockFileRequestResponse("test", "video/mp4"); Onfido onfido = Onfido.builder().apiToken("token").unknownApiUrl(server.url("/").toString()).build(); - InputStream inputStream = onfido.liveVideo.download("live video id").content; + FileDownload download = onfido.liveVideo.download("live video id"); // Correct path RecordedRequest request = server.takeRequest(); assertEquals("/live_videos/live%20video%20id/download", request.getPath()); // Correct response body - assertNotNull(inputStream); + assertEquals("test", new String(download.content)); + assertEquals("video/mp4", download.contentType); } @Test public void downloadLiveVideoFrame() throws Exception { - MockWebServer server = mockFileRequestResponse(); + MockWebServer server = mockFileRequestResponse("test", "image/png"); Onfido onfido = Onfido.builder().apiToken("token").unknownApiUrl(server.url("/").toString()).build(); - InputStream inputStream = onfido.liveVideo.downloadFrame("live video id").content; + FileDownload download = onfido.liveVideo.downloadFrame("live video id"); // Correct path RecordedRequest request = server.takeRequest(); assertEquals("/live_videos/live%20video%20id/frame", request.getPath()); // Correct response body - assertNotNull(inputStream); + assertEquals("test", new String(download.content)); + assertEquals("image/png", download.contentType); } @Test