Skip to content

Commit ffae834

Browse files
authored
Merge branch 'dev' into WeeklyExamplesUpdate/202311240306
2 parents 7f249e9 + 0fd3465 commit ffae834

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ private async Task ReportResponseStatusASync(HttpMessageFormatter responseMessag
294294
/// <returns></returns>
295295
private HttpRequestMessage GetRequest(HttpClient httpClient, Uri uri)
296296
{
297+
297298
var requestUri = PrepareUri(httpClient, uri);
298299
var httpMethod = GetHttpMethod(Method);
299300
// create the base WebRequest object
@@ -346,7 +347,6 @@ private HttpRequestMessage GetRequest(HttpClient httpClient, Uri uri)
346347
request.Headers.Add(HttpKnownHeaderNames.UserAgent, GraphRequestSession.UserAgent);
347348
}
348349
}
349-
350350
return request;
351351
}
352352

@@ -359,6 +359,7 @@ private HttpRequestMessage GetRequest(HttpClient httpClient, Uri uri)
359359
private Uri PrepareUri(HttpClient httpClient, Uri uri)
360360
{
361361
UriBuilder uriBuilder;
362+
362363
// For AbsoluteUri such as /beta/groups?$count=true, Get the scheme and host from httpClient
363364
// Then use them to compose a new Url with the URL fragment.
364365
if (uri.IsAbsoluteUri)
@@ -401,8 +402,7 @@ private Uri PrepareUri(HttpClient httpClient, Uri uri)
401402
// set body to null to prevent later FillRequestStream
402403
Body = null;
403404
}
404-
405-
return uriBuilder.Uri;
405+
return uriBuilder.Uri.EscapeDataStrings();
406406
}
407407

408408
private void ThrowIfError(ErrorRecord error)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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('/');
27+
sb.Append(Uri.EscapeDataString(segment));
28+
}
29+
counter++;
30+
}
31+
Uri escapedUri = new Uri(uri.GetLeftPart(UriPartial.Authority) + sb.ToString());
32+
return escapedUri;
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)