|
5 | 5 | import static com.box.sdk.http.ContentType.APPLICATION_JSON; |
6 | 6 | import static com.box.sdk.http.ContentType.APPLICATION_JSON_PATCH; |
7 | 7 | import static com.box.sdk.http.ContentType.APPLICATION_OCTET_STREAM; |
| 8 | +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; |
| 9 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 10 | +import static com.github.tomakehurst.wiremock.client.WireMock.verify; |
8 | 11 | import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; |
9 | 12 | import static java.lang.String.format; |
10 | 13 | import static java.nio.charset.StandardCharsets.UTF_8; |
11 | 14 | import static org.hamcrest.MatcherAssert.assertThat; |
12 | 15 | import static org.hamcrest.Matchers.containsString; |
13 | 16 | import static org.hamcrest.Matchers.equalTo; |
14 | 17 | import static org.hamcrest.Matchers.is; |
| 18 | +import static org.junit.Assert.assertArrayEquals; |
15 | 19 | import static org.junit.Assert.assertEquals; |
16 | 20 | import static org.junit.Assert.assertNull; |
17 | 21 | import static org.junit.Assert.assertTrue; |
@@ -728,6 +732,103 @@ public void createEditableSharedLinkSucceeds() { |
728 | 732 | assertTrue(sharedLink.getPermissions().getCanEdit()); |
729 | 733 | } |
730 | 734 |
|
| 735 | + @Test |
| 736 | + public void testDownloadFromSharedLinkWithPassword() { |
| 737 | + final String sharedItemsURL = "/2.0/shared_items"; |
| 738 | + final String fileContentURL = "/2.0/files/12345/content"; |
| 739 | + final String sharedLink = "https://app.box.com/s/abcdef123456"; |
| 740 | + final String password = "password"; |
| 741 | + final byte[] fileContent = "This is a test file content".getBytes(); |
| 742 | + final String expectedSharedLinkHeaderValue = "shared_link=" + sharedLink + "&shared_link_password=" + password; |
| 743 | + final String expectedDownloadPath = "/shared/static/rh935iit6ewrmw0unyul.jpeg"; |
| 744 | + final String expectedDownloadUrl = format("https://localhost:%d%s", wireMockRule.httpsPort(), expectedDownloadPath); |
| 745 | + |
| 746 | + String sharedItemsResponse = "{ \"type\": \"file\", \"id\": \"12345\" }"; |
| 747 | + |
| 748 | + wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(sharedItemsURL)) |
| 749 | + .willReturn(WireMock.aResponse() |
| 750 | + .withHeader("Content-Type", APPLICATION_JSON) |
| 751 | + .withBody(sharedItemsResponse))); |
| 752 | + |
| 753 | + wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(fileContentURL)) |
| 754 | + .withHeader("boxapi", WireMock.equalTo(expectedSharedLinkHeaderValue)) |
| 755 | + .willReturn(WireMock.aResponse() |
| 756 | + .withStatus(302) |
| 757 | + .withHeader("Location", expectedDownloadUrl))); |
| 758 | + |
| 759 | + wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(expectedDownloadPath)) |
| 760 | + .willReturn(WireMock.aResponse() |
| 761 | + .withHeader("Content-Type", "application/octet-stream") |
| 762 | + .withBody(fileContent))); |
| 763 | + |
| 764 | + |
| 765 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 766 | + BoxFile.downloadFromSharedLink(api, output, sharedLink, password); |
| 767 | + |
| 768 | + verify(1, getRequestedFor( |
| 769 | + urlEqualTo("/2.0/shared_items?fields=id")). |
| 770 | + withHeader("BoxApi", WireMock.equalTo(expectedSharedLinkHeaderValue))); |
| 771 | + |
| 772 | + verify(1, getRequestedFor(urlEqualTo(fileContentURL)). |
| 773 | + withHeader("boxapi", WireMock.equalTo(expectedSharedLinkHeaderValue))); |
| 774 | + |
| 775 | + verify(1, getRequestedFor(urlEqualTo(expectedDownloadPath))); |
| 776 | + |
| 777 | + assertArrayEquals(fileContent, output.toByteArray()); |
| 778 | + } |
| 779 | + |
| 780 | + @Test |
| 781 | + public void testDownloadFromSharedLinkWithProgressListener() { |
| 782 | + final String sharedItemsURL = "/2.0/shared_items"; |
| 783 | + final String fileContentURL = "/2.0/files/12345/content"; |
| 784 | + final String sharedLink = "https://app.box.com/s/abcdef123456"; |
| 785 | + final byte[] fileContent = "This is a test file content".getBytes(); |
| 786 | + final String expectedSharedLinkHeaderValue = "shared_link=" + sharedLink; |
| 787 | + final String expectedDownloadPath = "/shared/static/rh935iit6ewrmw0unyul.jpeg"; |
| 788 | + final String expectedDownloadUrl = format( |
| 789 | + "https://localhost:%d%s", wireMockRule.httpsPort(), expectedDownloadPath |
| 790 | + ); |
| 791 | + |
| 792 | + String sharedItemsResponse = format( |
| 793 | + "{ \"download_url\": \"%s\", \"type\": \"file\", \"id\": \"12345\" }", |
| 794 | + expectedDownloadUrl |
| 795 | + ); |
| 796 | + |
| 797 | + wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(sharedItemsURL)) |
| 798 | + .willReturn(WireMock.aResponse() |
| 799 | + .withHeader("Content-Type", APPLICATION_JSON) |
| 800 | + .withBody(sharedItemsResponse))); |
| 801 | + |
| 802 | + wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(fileContentURL)) |
| 803 | + .withHeader("boxapi", WireMock.equalTo(expectedSharedLinkHeaderValue)) |
| 804 | + .willReturn(WireMock.aResponse() |
| 805 | + .withStatus(302) |
| 806 | + .withHeader("Location", expectedDownloadUrl))); |
| 807 | + |
| 808 | + wireMockRule.stubFor(WireMock.get(WireMock.urlPathEqualTo(expectedDownloadPath)) |
| 809 | + .willReturn(WireMock.aResponse() |
| 810 | + .withHeader("Content-Type", "application/octet-stream") |
| 811 | + .withBody(fileContent))); |
| 812 | + |
| 813 | + |
| 814 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 815 | + ProgressListener listener = (numBytes, totalBytes) -> { |
| 816 | + // Implement progress listener logic if needed |
| 817 | + }; |
| 818 | + BoxFile.downloadFromSharedLink(api, output, sharedLink, listener); |
| 819 | + |
| 820 | + verify(1, getRequestedFor( |
| 821 | + urlEqualTo("/2.0/shared_items?fields=id")). |
| 822 | + withHeader("BoxApi", WireMock.equalTo(expectedSharedLinkHeaderValue))); |
| 823 | + |
| 824 | + verify(1, getRequestedFor(urlEqualTo(fileContentURL)). |
| 825 | + withHeader("boxapi", WireMock.equalTo(expectedSharedLinkHeaderValue))); |
| 826 | + |
| 827 | + verify(1, getRequestedFor(urlEqualTo(expectedDownloadPath))); |
| 828 | + |
| 829 | + assertArrayEquals(fileContent, output.toByteArray()); |
| 830 | + } |
| 831 | + |
731 | 832 | @Test |
732 | 833 | public void testAddClassification() { |
733 | 834 | final String fileID = "12345"; |
|
0 commit comments