|
| 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