|
1 | 1 | namespace JsonUtilitiesTest;
|
2 | 2 | using System;
|
| 3 | +using System.Text.Json; |
3 | 4 | using Newtonsoft.Json.Linq;
|
4 | 5 | using Xunit;
|
5 | 6 | using NamespacePrefixPlaceholder.PowerShell.JsonUtilities;
|
@@ -144,6 +145,66 @@ public void RemoveDefaultNullProperties_ShouldRemoveDefaultNullValuesInJsonArray
|
144 | 145 | Assert.False(result[0].ToObject<JObject>().ContainsKey("email"));
|
145 | 146 |
|
146 | 147 | }
|
| 148 | + [Fact] |
| 149 | + public void ReplaceAndRemoveSlashes_Should_Preserve_Json_Property_Values() |
| 150 | + { |
| 151 | + // Arrange |
| 152 | + string inputJson = @"{ |
| 153 | + ""RedirectUris"": [""http://localhost/.auth/login/aad/callback""], |
| 154 | + ""DirectoryPath"": ""/this/is/a/directory/and/should/not/be/removed"" |
| 155 | + }"; |
| 156 | + |
| 157 | + string expectedJson = @"{ |
| 158 | + ""RedirectUris"": [""http://localhost/.auth/login/aad/callback""], |
| 159 | + ""DirectoryPath"": ""/this/is/a/directory/and/should/not/be/removed"" |
| 160 | + }"; |
| 161 | + |
| 162 | + // Act |
| 163 | + string result = inputJson.ReplaceAndRemoveSlashes(); |
| 164 | + |
| 165 | + // Assert |
| 166 | + Assert.Equal(NormalizeJson(expectedJson), NormalizeJson(result)); |
| 167 | + } |
| 168 | + |
| 169 | + [Fact] |
| 170 | + public void ReplaceAndRemoveSlashes_Should_Remove_Backslashes() |
| 171 | + { |
| 172 | + // Arrange |
| 173 | + string input = @"Some \random \slashes that \should be removed."; |
| 174 | + string expected = "Some random slashes that should be removed."; |
| 175 | + |
| 176 | + // Act |
| 177 | + string result = input.ReplaceAndRemoveSlashes(); |
| 178 | + |
| 179 | + // Assert |
| 180 | + Assert.Equal(expected, result); |
| 181 | + } |
| 182 | + |
| 183 | + [Fact] |
| 184 | + public void ReplaceAndRemoveSlashes_Should_Handle_Invalid_Json_Gracefully() |
| 185 | + { |
| 186 | + // Arrange |
| 187 | + string invalidJson = "{Invalid Json \\with /slashes}"; |
| 188 | + |
| 189 | + // Act |
| 190 | + string result = invalidJson.ReplaceAndRemoveSlashes(); |
| 191 | + |
| 192 | + // Assert |
| 193 | + Assert.DoesNotContain("\\", result); |
| 194 | + } |
| 195 | + |
| 196 | + /// <summary> |
| 197 | + /// Normalizes JSON for comparison (removes formatting differences). |
| 198 | + /// </summary> |
| 199 | + private string NormalizeJson(string json) |
| 200 | + { |
| 201 | + using var doc = JsonDocument.Parse(json); |
| 202 | + return JsonSerializer.Serialize(doc.RootElement, new JsonSerializerOptions |
| 203 | + { |
| 204 | + WriteIndented = false, |
| 205 | + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping |
| 206 | + }); |
| 207 | + } |
147 | 208 |
|
148 | 209 |
|
149 | 210 | }
|
|
0 commit comments