Skip to content

Commit dbbda87

Browse files
fenggagladjohn
andauthored
Stop replacing "%20" to "+" since it is obsoleted (#5128)
* Stop replacing "%20" to "+" since this standard is too old and is obsoleted. * Add a test * Update test * Update src/client/Microsoft.Identity.Client/Utils/CoreHelpers.cs Co-authored-by: Gladwin Johnson <[email protected]> * Update tests --------- Co-authored-by: Gladwin Johnson <[email protected]>
1 parent a2565ef commit dbbda87

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/client/Microsoft.Identity.Client/Utils/CoreHelpers.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public static string UrlEncode(string message)
3333
}
3434

3535
message = Uri.EscapeDataString(message);
36-
message = message.Replace("%20", "+");
3736

3837
return message;
3938
}
@@ -44,7 +43,7 @@ public static string UrlDecode(string message)
4443
{
4544
return message;
4645
}
47-
46+
// Replace "+" with "%20" for backward compatibility with older systems that used "+" for spaces.
4847
message = message.Replace("+", "%20");
4948
message = Uri.UnescapeDataString(message);
5049

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Identity.Client;
5+
using Microsoft.Identity.Test.Common.Core.Helpers;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
using Microsoft.Identity.Client.Internal;
8+
using Microsoft.Identity.Client.Utils;
9+
10+
namespace Microsoft.Identity.Test.Unit.CoreTests
11+
{
12+
[TestClass]
13+
public class CoreHelperTests
14+
{
15+
[TestMethod]
16+
public void UrlEncodeDecodeTest()
17+
{
18+
// url without blank can be converted correctly.
19+
Assert.AreEqual(CoreHelpers.UrlEncode("https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize"),
20+
"https%3A%2F%2Flogin.microsoftonline.com%2Forganizations%2Foauth2%2Fv2.0%2Fauthorize");
21+
// url with blank can be converted correctly. " " needs to be replaced by "%20"
22+
Assert.AreEqual(CoreHelpers.UrlEncode("https://management.core.windows.net//.default openid profile offline_access"),
23+
"https%3A%2F%2Fmanagement.core.windows.net%2F%2F.default%20openid%20profile%20offline_access");
24+
25+
// Encoded url should be decoded correctly.
26+
Assert.AreEqual(CoreHelpers.UrlDecode("https%3A%2F%2Flogin.microsoftonline.com%2Forganizations%2Foauth2%2Fv2.0%2Fauthorize"),
27+
"https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize");
28+
// Encoded url with blank should be decoded correctly.
29+
Assert.AreEqual(CoreHelpers.UrlDecode("https%3A%2F%2Fmanagement.core.windows.net%2F%2F.default%20openid%20profile%20offline_access"),
30+
"https://management.core.windows.net//.default openid profile offline_access");
31+
// Encoded url with "+" should be decoded correctly.
32+
Assert.AreEqual(CoreHelpers.UrlDecode("https%3A%2F%2Fmanagement.core.windows.net%2F%2F.default+openid+profile+offline_access"),
33+
"https://management.core.windows.net//.default openid profile offline_access");
34+
35+
// Test special OAuth characters (query string scenarios)
36+
Assert.AreEqual(CoreHelpers.UrlEncode("redirect_uri=https://example.com/callback?code=1234"),
37+
"redirect_uri%3Dhttps%3A%2F%2Fexample.com%2Fcallback%3Fcode%3D1234");
38+
Assert.AreEqual(CoreHelpers.UrlDecode("redirect_uri%3Dhttps%3A%2F%2Fexample.com%2Fcallback%3Fcode%3D1234"),
39+
"redirect_uri=https://example.com/callback?code=1234");
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)