Skip to content

Commit f2f1509

Browse files
authored
Merge pull request #728 from microsoftgraph/bugfix/ODSP-path-encoding
bugfix/ODSP path encoding
2 parents 8428170 + 70e12a9 commit f2f1509

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/main/java/com/microsoft/graph/requests/DriveItemRequestBuilder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,14 @@ public DriveItemRequest buildRequest(@Nonnull final java.util.List<? extends com
9393
*/
9494
@Nonnull
9595
public DriveItemRequestBuilder itemWithPath(@Nonnull final String path) {
96-
return new DriveItemRequestBuilder(getRequestUrl() + ":/" + path + ":", getClient(), null);
96+
String value = path;
97+
try {
98+
value = java.net.URLEncoder.encode(path, java.nio.charset.StandardCharsets.UTF_8.toString()).replace("+", "%20");
99+
//ODSP doesn't respect application/x-www-form-urlencoded MIME format and expects spaces with %20
100+
} catch (java.io.UnsupportedEncodingException ex) {
101+
throw new ClientException("unsupported encoding", ex);
102+
}
103+
return new DriveItemRequestBuilder(getRequestUrl() + ":/" + value + ":", getClient(), null);
97104
}
98105

99106

src/test/java/com/microsoft/graph/functional/OneDriveTests.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.microsoft.graph.functional;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
34
import static org.junit.jupiter.api.Assertions.assertFalse;
45
import static org.junit.jupiter.api.Assertions.assertNotNull;
56
import static org.junit.jupiter.api.Assertions.assertTrue;
67

78
import java.io.ByteArrayInputStream;
89
import java.io.IOException;
910
import java.io.InputStream;
11+
import java.net.URL;
1012
import java.nio.charset.StandardCharsets;
1113

1214
import org.junit.jupiter.api.BeforeEach;
@@ -24,13 +26,13 @@
2426
import com.microsoft.graph.models.UploadSession;
2527
import com.microsoft.graph.models.DriveItemCreateUploadSessionParameterSet;
2628

27-
@Disabled
2829
public class OneDriveTests {
2930
private TestBase testBase;
3031

31-
@BeforeEach
3232
public void setUp() {
33-
testBase = new TestBase();
33+
if(testBase == null) {
34+
testBase = new TestBase();
35+
}
3436
}
3537

3638
final IProgressCallback callback = new IProgressCallback () {
@@ -39,6 +41,17 @@ public void progress(final long current, final long max) {
3941
//Check progress
4042
}
4143
};
44+
/**
45+
* Tests that paths are properly encoded acording to ODSP's expectations
46+
* - everything is encoded to the application/x-www-form-urlencoded MIME format
47+
* - spaces are encoded with %20
48+
*/
49+
@Test
50+
public void itemWithPathEncodesSpecialCharacters() {
51+
final URL requestURL = new TestBase(false).graphClient.me().drive().root().itemWithPath("some folder/some name with a + and a #777.docx").buildRequest().getRequestUrl();
52+
assertEquals("/me/drive/root:/some%20folder%2Fsome%20name%20with%20a%20%2B%20and%20a%20%23777.docx:", requestURL.getPath().replace("/v1.0", "").replace("/beta", ""));
53+
// version replacement so the test is version agnostic
54+
}
4255
/**
4356
* Test large file upload.
4457
* https://github.com/OneDrive/onedrive-sdk-csharp/blob/master/docs/chunked-uploads.md
@@ -47,8 +60,10 @@ public void progress(final long current, final long max) {
4760
* @throws InterruptedException if the chunked upload fails
4861
*/
4962
@Test
63+
@Disabled
5064
public void testLargeFileUpload() throws IOException, InterruptedException {
51-
//Get resource file from file system
65+
setUp();
66+
//Get resource file from file system
5267
InputStream uploadFile = OneDriveTests.class.getClassLoader().getResourceAsStream("largefile10M.blob");
5368
long fileSize = (long) uploadFile.available();
5469

@@ -72,14 +87,18 @@ public void testLargeFileUpload() throws IOException, InterruptedException {
7287
assertNotNull(result);
7388
}
7489
@Test
90+
@Disabled
7591
public void testDownloadWithCustomRequest() throws IOException {
92+
setUp();
7693
final String testDownloadFileId = "01RWFXFJG3UYRHE75RZVFYWKNUEBB53H7A";
7794
try (final InputStream stream = testBase.graphClient.customRequest("/me/drive/items/"+testDownloadFileId+"/content", InputStream.class).buildRequest().get()) {
7895
assertFalse(stream.read() == -1, "stream should not be empty");
7996
}
8097
}
8198
@Test
99+
@Disabled
82100
public void downloadJsonFileFromOneDrive() throws Exception {
101+
setUp();
83102
final DriveItemUploadableProperties item = new DriveItemUploadableProperties();
84103
item.name = "test.json";
85104
item.additionalDataManager().put("@microsoft.graph.conflictBehavior", new JsonPrimitive("replace"));

0 commit comments

Comments
 (0)