Skip to content

Commit ba5a320

Browse files
committed
Moved method to hepers and added unit test for the helper method as per review suggestion
1 parent 2bbc232 commit ba5a320

File tree

3 files changed

+61
-25
lines changed

3 files changed

+61
-25
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+

2+
using Xunit;
3+
using System;
4+
using Microsoft.Graph.PowerShell.Authentication.Helpers;
5+
6+
namespace Microsoft.Graph.Authentication.Test.Helpers
7+
{
8+
public class EncodeUriPathTests
9+
{
10+
private string url = "https://graph.microsoft.com/beta/users/first.last_foo.com#EXT#@contoso.onmicrosoft.com";
11+
private string encodedUrl = "https://graph.microsoft.com/beta/users/first.last_foo.com%23EXT%23%40contoso.onmicrosoft.com";
12+
13+
[Fact]
14+
public void ShouldReturnAnEncodedUriGivenAUrlWithSpecialCharactersInPathSegments()
15+
{
16+
//arrange
17+
Uri uri = new Uri(url);
18+
19+
//act
20+
Uri result = uri.EscapeDataStrings();
21+
22+
//assert
23+
Assert.Equal(encodedUrl, result.ToString());
24+
}
25+
}
26+
}

src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -402,31 +402,7 @@ private Uri PrepareUri(HttpClient httpClient, Uri uri)
402402
// set body to null to prevent later FillRequestStream
403403
Body = null;
404404
}
405-
return EscapeDataStrings(uriBuilder.Uri);
406-
}
407-
408-
/// <summary>
409-
/// Escape data string/url encode Uris that have paths containing special characters like #.
410-
/// For a path like /beta/users/first.last_foo.com#EXT#@contoso.onmicrosoft.com, the last segment contains special characters that need to be escaped
411-
/// </summary>
412-
/// <param name="uri"></param>
413-
/// <returns></returns>
414-
private Uri EscapeDataStrings(Uri uri)
415-
{
416-
int counter = 0;
417-
var pathSegments = uri.OriginalString.Split('/');
418-
StringBuilder sb = new StringBuilder();
419-
foreach (var segment in pathSegments)
420-
{
421-
//Skips the left part of the uri i.e https://graph.microsoft.com
422-
if (counter > 2)
423-
{
424-
sb.Append("/" + Uri.EscapeDataString(segment));
425-
}
426-
counter++;
427-
}
428-
Uri escapedUri = new Uri(uri.GetLeftPart(UriPartial.Authority) + sb.ToString());
429-
return escapedUri;
405+
return uriBuilder.Uri.EscapeDataStrings();
430406
}
431407

432408
private void ThrowIfError(ErrorRecord error)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
// ------------------------------------------------------------------------------
4+
5+
using System;
6+
using System.Text;
7+
8+
namespace Microsoft.Graph.PowerShell.Authentication.Helpers
9+
{
10+
/// <summary>
11+
/// Escape data string/url encode Uris that have paths containing special characters like #.
12+
/// For a path like /beta/users/first.last_foo.com#EXT#@contoso.onmicrosoft.com, the last segment contains special characters that need to be escaped
13+
/// </summary>
14+
public static class EncodeUriPaths
15+
{
16+
public static Uri EscapeDataStrings(this Uri uri)
17+
{
18+
int counter = 0;
19+
var pathSegments = uri.OriginalString.Split('/');
20+
StringBuilder sb = new StringBuilder();
21+
foreach (var segment in pathSegments)
22+
{
23+
//Skips the left part of the uri i.e https://graph.microsoft.com
24+
if (counter > 2)
25+
{
26+
sb.Append("/" + Uri.EscapeDataString(segment));
27+
}
28+
counter++;
29+
}
30+
Uri escapedUri = new Uri(uri.GetLeftPart(UriPartial.Authority) + sb.ToString());
31+
return escapedUri;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)