diff --git a/src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs b/src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs index 4dec45ad515..8b490e49a90 100644 --- a/src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs +++ b/src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs @@ -44,12 +44,14 @@ public static class DriveItemRequestBuilderExtensions /// public static CustomDriveItemItemRequestBuilder ItemWithPath(this Microsoft.Graph.Drives.Item.Root.RootRequestBuilder rootRequestBuilder, string path) { - if (!string.IsNullOrEmpty(path)) + if (path is null) + throw new ArgumentNullException(nameof(path)); + if (string.IsNullOrWhiteSpace(path)) + throw new ArgumentException("path cannot be empty or whitespace.", nameof(path)); + + if (!path.StartsWith("/")) { - if (!path.StartsWith("/")) - { - path = string.Format("/{0}", path); - } + path = string.Format("/{0}", path); } var requestInformation = rootRequestBuilder.ToGetRequestInformation(); @@ -67,12 +69,14 @@ public static CustomDriveItemItemRequestBuilder ItemWithPath(this Microsoft.Grap /// public static CustomDriveItemItemRequestBuilder ItemWithPath(this Microsoft.Graph.Drives.Item.Items.Item.DriveItemItemRequestBuilder rootRequestBuilder, string path) { - if (!string.IsNullOrEmpty(path)) + if (path is null) + throw new ArgumentNullException(nameof(path)); + if (string.IsNullOrWhiteSpace(path)) + throw new ArgumentException("path cannot be empty or whitespace.", nameof(path)); + + if (!path.StartsWith("/")) { - if (!path.StartsWith("/")) - { - path = string.Format("/{0}", path); - } + path = string.Format("/{0}", path); } var requestInformation = rootRequestBuilder.ToGetRequestInformation(); diff --git a/tests/Microsoft.Graph.DotnetCore.Test/Requests/Extensions/DriveItemRequestBuilderExtensionsTests.cs b/tests/Microsoft.Graph.DotnetCore.Test/Requests/Extensions/DriveItemRequestBuilderExtensionsTests.cs index edc08ec2e5f..d8d518415e3 100644 --- a/tests/Microsoft.Graph.DotnetCore.Test/Requests/Extensions/DriveItemRequestBuilderExtensionsTests.cs +++ b/tests/Microsoft.Graph.DotnetCore.Test/Requests/Extensions/DriveItemRequestBuilderExtensionsTests.cs @@ -141,5 +141,37 @@ public void ItemByPath_BuildRequestWithNestedPathSlashAndMoreThanOneParameter() Assert.NotNull(itemRequestInformation); Assert.Equal(expectedRequestUri, itemRequestInformation.URI); } + [Fact] + public void ItemByPath_ThrowsArgumentNullException_WhenPathIsNull_Root() + { + var graphServiceClient = new GraphServiceClient(new MockAuthenticationProvider().Object); + Assert.Throws(() => graphServiceClient.Drives["driveId"].Root.ItemWithPath(null)); + } + + [Theory] + [InlineData("")] + [InlineData(" ")] + public void ItemByPath_ThrowsArgumentException_WhenPathIsEmptyOrWhitespace_Root(string path) + { + var graphServiceClient = new GraphServiceClient(new MockAuthenticationProvider().Object); + Assert.Throws(() => graphServiceClient.Drives["driveId"].Root.ItemWithPath(path)); + } + + [Fact] + public void ItemByPath_ThrowsArgumentNullException_WhenPathIsNull_DriveItem() + { + var graphServiceClient = new GraphServiceClient(new MockAuthenticationProvider().Object); + Assert.Throws(() => graphServiceClient.Drives["driveId"].Items["itemId"].ItemWithPath(null)); + } + + [Theory] + [InlineData("")] + [InlineData(" ")] + public void ItemByPath_ThrowsArgumentException_WhenPathIsEmptyOrWhitespace_DriveItem(string path) + { + var graphServiceClient = new GraphServiceClient(new MockAuthenticationProvider().Object); + Assert.Throws(() => graphServiceClient.Drives["driveId"].Items["itemId"].ItemWithPath(path)); + } + } }