Skip to content

Commit b1714c9

Browse files
fix: encoded batch request URI issue
1 parent 267babb commit b1714c9

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/Microsoft.Graph.Core/Requests/Content/BatchRequestContent.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,9 @@ private string GetRelativeUrl(Uri requestUri)
329329
if (requestUri == null)
330330
throw new ArgumentNullException(nameof(requestUri));
331331

332-
return requestUri.PathAndQuery.Substring(5); // `v1.0/` and `beta/` are both 5 characters
332+
var encodedUri = requestUri.PathAndQuery.Substring(5); // `v1.0/` and `beta/` are both 5 characters
333+
334+
return Uri.UnescapeDataString(encodedUri); // removes encoded chars from URI since graph batch encodes for us
333335
}
334336

335337
/// <summary>

tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Content/BatchRequestContentTests.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,8 @@ public async Task BatchRequestContent_AddBatchRequestStepWithBaseRequestToBatchR
589589
[InlineData("https://graph.microsoft.com/beta/me", "/me")]
590590
[InlineData("https://graph.microsoft.com/v1.0/users/abcbeta123@wonderemail.com/events", "/users/abcbeta123@wonderemail.com/events")]
591591
[InlineData("https://graph.microsoft.com/beta/users/abcbeta123@wonderemail.com/events", "/users/abcbeta123@wonderemail.com/events")]
592-
[InlineData("https://graph.microsoft.com/v1.0/users?$filter=identities/any(id:id/issuer%20eq%20'$74707853-18b3-411f-ad57-2ef65f6fdeb0'%20and%20id/issuerAssignedId%20eq%20'**bobbetancourt@fakeemail.com**')", "/users?$filter=identities/any(id:id/issuer%20eq%20'$74707853-18b3-411f-ad57-2ef65f6fdeb0'%20and%20id/issuerAssignedId%20eq%20'**bobbetancourt@fakeemail.com**')")]
593-
[InlineData("https://graph.microsoft.com/beta/users?$filter=identities/any(id:id/issuer%20eq%20'$74707853-18b3-411f-ad57-2ef65f6fdeb0'%20and%20id/issuerAssignedId%20eq%20'**bobbetancourt@fakeemail.com**')&$top=1", "/users?$filter=identities/any(id:id/issuer%20eq%20'$74707853-18b3-411f-ad57-2ef65f6fdeb0'%20and%20id/issuerAssignedId%20eq%20'**bobbetancourt@fakeemail.com**')&$top=1")]
592+
[InlineData("https://graph.microsoft.com/v1.0/users?$filter=identities/any(id:id/issuer%20eq%20'$74707853-18b3-411f-ad57-2ef65f6fdeb0'%20and%20id/issuerAssignedId%20eq%20'**bobbetancourt@fakeemail.com**')", "/users?$filter=identities/any(id:id/issuer eq '$74707853-18b3-411f-ad57-2ef65f6fdeb0' and id/issuerAssignedId eq '**bobbetancourt@fakeemail.com**')")]
593+
[InlineData("https://graph.microsoft.com/beta/users?$filter=identities/any(id:id/issuer%20eq%20'$74707853-18b3-411f-ad57-2ef65f6fdeb0'%20and%20id/issuerAssignedId%20eq%20'**bobbetancourt@fakeemail.com**')&$top=1", "/users?$filter=identities/any(id:id/issuer eq '$74707853-18b3-411f-ad57-2ef65f6fdeb0' and id/issuerAssignedId eq '**bobbetancourt@fakeemail.com**')&$top=1")]
594594
public async Task BatchRequestContent_AddBatchRequestStepWithBaseRequestProperlySetsVersion(string requestUrl, string expectedUrl)
595595
{
596596
// Arrange
@@ -607,12 +607,34 @@ public async Task BatchRequestContent_AddBatchRequestStepWithBaseRequestProperly
607607
requestContent = await reader.ReadToEndAsync();
608608
}
609609

610-
var expectedContent = "{\"requests\":[{\"id\":\"1\",\"url\":\""+ expectedUrl +"\",\"method\":\"GET\"}]}";
610+
var expectedContent = "{\"requests\":[{\"id\":\"1\",\"url\":\"" + expectedUrl + "\",\"method\":\"GET\"}]}";
611611

612-
613-
// Assert we added successfully and contents are as expected
612+
// Assert we added successfully and contents are as expected and URI is not encoded
614613
Assert.Equal(expectedContent, System.Text.RegularExpressions.Regex.Unescape(requestContent));
615614
}
616615

616+
[Theory]
617+
[InlineData("https://graph.microsoft.com/v1.0/drives/b%21ynG/items/74707853-18b3-411f-ad57-2ef65f6fdeb0:/test.txt:/textfilecontentbytes", "/drives/b!ynG/items/74707853-18b3-411f-ad57-2ef65f6fdeb0:/test.txt:/textfilecontentbytes")]
618+
public async Task BatchRequestContent_AddBatchRequestPutStepWithBaseRequestProperlyEncodedURI(string requestUrl, string expectedUrl)
619+
{
620+
// Arrange
621+
BatchRequestStep batchRequestStep = new BatchRequestStep("1", new HttpRequestMessage(HttpMethod.Put, requestUrl));
622+
BatchRequestContent batchRequestContent = new BatchRequestContent(client);
623+
Assert.False(batchRequestContent.BatchRequestSteps.Any());//Its empty
624+
625+
// Act
626+
batchRequestContent.AddBatchRequestStep(batchRequestStep);
627+
var requestContentStream = await batchRequestContent.GetBatchRequestContentAsync();
628+
string requestContent;
629+
using (StreamReader reader = new StreamReader(requestContentStream))
630+
{
631+
requestContent = await reader.ReadToEndAsync();
632+
}
633+
634+
var expectedContent = "{\"requests\":[{\"id\":\"1\",\"url\":\"" + expectedUrl + "\",\"method\":\"PUT\"}]}";
635+
636+
// Assert we added successfully and contents are as expected and URI is not encoded
637+
Assert.Equal(expectedContent, System.Text.RegularExpressions.Regex.Unescape(requestContent));
638+
}
617639
}
618640
}

0 commit comments

Comments
 (0)