|
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; |
@@ -161,6 +162,66 @@ public void RemoveDefaultNullProperties_ShouldRemoveDefaultNullValuesInJsonArray |
161 | 162 | Assert.Equal("Tim", result[0]?["displayname"]?.ToString()); |
162 | 163 | Assert.False(result[0].ToObject<JObject>().ContainsKey("email")); |
163 | 164 |
|
| 165 | + } |
| 166 | + [Fact] |
| 167 | + public void ReplaceAndRemoveSlashes_Should_Preserve_Json_Property_Values() |
| 168 | + { |
| 169 | + // Arrange |
| 170 | + string inputJson = @"{ |
| 171 | + ""RedirectUris"": [""http://localhost/.auth/login/aad/callback""], |
| 172 | + ""DirectoryPath"": ""/this/is/a/directory/and/should/not/be/removed"" |
| 173 | + }"; |
| 174 | + |
| 175 | + string expectedJson = @"{ |
| 176 | + ""RedirectUris"": [""http://localhost/.auth/login/aad/callback""], |
| 177 | + ""DirectoryPath"": ""/this/is/a/directory/and/should/not/be/removed"" |
| 178 | + }"; |
| 179 | + |
| 180 | + // Act |
| 181 | + string result = inputJson.ReplaceAndRemoveSlashes(); |
| 182 | + |
| 183 | + // Assert |
| 184 | + Assert.Equal(NormalizeJson(expectedJson), NormalizeJson(result)); |
| 185 | + } |
| 186 | + |
| 187 | + [Fact] |
| 188 | + public void ReplaceAndRemoveSlashes_Should_Remove_Backslashes() |
| 189 | + { |
| 190 | + // Arrange |
| 191 | + string input = @"Some \random \slashes that \should be removed."; |
| 192 | + string expected = "Some random slashes that should be removed."; |
| 193 | + |
| 194 | + // Act |
| 195 | + string result = input.ReplaceAndRemoveSlashes(); |
| 196 | + |
| 197 | + // Assert |
| 198 | + Assert.Equal(expected, result); |
| 199 | + } |
| 200 | + |
| 201 | + [Fact] |
| 202 | + public void ReplaceAndRemoveSlashes_Should_Handle_Invalid_Json_Gracefully() |
| 203 | + { |
| 204 | + // Arrange |
| 205 | + string invalidJson = "{Invalid Json \\with /slashes}"; |
| 206 | + |
| 207 | + // Act |
| 208 | + string result = invalidJson.ReplaceAndRemoveSlashes(); |
| 209 | + |
| 210 | + // Assert |
| 211 | + Assert.DoesNotContain("\\", result); |
| 212 | + } |
| 213 | + |
| 214 | + /// <summary> |
| 215 | + /// Normalizes JSON for comparison (removes formatting differences). |
| 216 | + /// </summary> |
| 217 | + private string NormalizeJson(string json) |
| 218 | + { |
| 219 | + using var doc = JsonDocument.Parse(json); |
| 220 | + return JsonSerializer.Serialize(doc.RootElement, new JsonSerializerOptions |
| 221 | + { |
| 222 | + WriteIndented = false, |
| 223 | + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping |
| 224 | + }); |
164 | 225 | } |
165 | 226 |
|
166 | 227 | [Fact] |
|
0 commit comments