diff --git a/src/Microsoft.Extensions.WebEncoders/Testing/HtmlTestEncoder.cs b/src/Microsoft.Extensions.WebEncoders/Testing/HtmlTestEncoder.cs index 7768d65bfa..162ce4f6c1 100644 --- a/src/Microsoft.Extensions.WebEncoders/Testing/HtmlTestEncoder.cs +++ b/src/Microsoft.Extensions.WebEncoders/Testing/HtmlTestEncoder.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.IO; using System.Text.Encodings.Web; @@ -18,11 +19,36 @@ namespace Microsoft.Extensions.WebEncoders.Testing public override string Encode(string value) { + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (value.Length == 0) + { + return string.Empty; + } + return $"HtmlEncode[[{value}]]"; } public override void Encode(TextWriter output, char[] value, int startIndex, int characterCount) { + if (output == null) + { + throw new ArgumentNullException(nameof(output)); + } + + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (characterCount == 0) + { + return; + } + output.Write("HtmlEncode[["); output.Write(value, startIndex, characterCount); output.Write("]]"); @@ -30,6 +56,21 @@ namespace Microsoft.Extensions.WebEncoders.Testing public override void Encode(TextWriter output, string value, int startIndex, int characterCount) { + if (output == null) + { + throw new ArgumentNullException(nameof(output)); + } + + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (characterCount == 0) + { + return; + } + output.Write("HtmlEncode[["); output.Write(value.Substring(startIndex, characterCount)); output.Write("]]"); @@ -45,8 +86,17 @@ namespace Microsoft.Extensions.WebEncoders.Testing return -1; } - public override unsafe bool TryEncodeUnicodeScalar(int unicodeScalar, char* buffer, int bufferLength, out int numberOfCharactersWritten) + public override unsafe bool TryEncodeUnicodeScalar( + int unicodeScalar, + char* buffer, + int bufferLength, + out int numberOfCharactersWritten) { + if (buffer == null) + { + throw new ArgumentNullException(nameof(buffer)); + } + numberOfCharactersWritten = 0; return false; } diff --git a/src/Microsoft.Extensions.WebEncoders/Testing/JavaScriptTestEncoder.cs b/src/Microsoft.Extensions.WebEncoders/Testing/JavaScriptTestEncoder.cs index 4207e8a43f..bef4461676 100644 --- a/src/Microsoft.Extensions.WebEncoders/Testing/JavaScriptTestEncoder.cs +++ b/src/Microsoft.Extensions.WebEncoders/Testing/JavaScriptTestEncoder.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.IO; using System.Text.Encodings.Web; @@ -18,11 +19,36 @@ namespace Microsoft.Extensions.WebEncoders.Testing public override string Encode(string value) { + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (value.Length == 0) + { + return string.Empty; + } + return $"JavaScriptEncode[[{value}]]"; } public override void Encode(TextWriter output, char[] value, int startIndex, int characterCount) { + if (output == null) + { + throw new ArgumentNullException(nameof(output)); + } + + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (characterCount == 0) + { + return; + } + output.Write("JavaScriptEncode[["); output.Write(value, startIndex, characterCount); output.Write("]]"); @@ -30,6 +56,21 @@ namespace Microsoft.Extensions.WebEncoders.Testing public override void Encode(TextWriter output, string value, int startIndex, int characterCount) { + if (output == null) + { + throw new ArgumentNullException(nameof(output)); + } + + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (characterCount == 0) + { + return; + } + output.Write("JavaScriptEncode[["); output.Write(value.Substring(startIndex, characterCount)); output.Write("]]"); @@ -45,8 +86,17 @@ namespace Microsoft.Extensions.WebEncoders.Testing return -1; } - public override unsafe bool TryEncodeUnicodeScalar(int unicodeScalar, char* buffer, int bufferLength, out int numberOfCharactersWritten) + public override unsafe bool TryEncodeUnicodeScalar( + int unicodeScalar, + char* buffer, + int bufferLength, + out int numberOfCharactersWritten) { + if (buffer == null) + { + throw new ArgumentNullException(nameof(buffer)); + } + numberOfCharactersWritten = 0; return false; } diff --git a/src/Microsoft.Extensions.WebEncoders/Testing/UrlTestEncoder.cs b/src/Microsoft.Extensions.WebEncoders/Testing/UrlTestEncoder.cs index d10ee75594..295bda63e8 100644 --- a/src/Microsoft.Extensions.WebEncoders/Testing/UrlTestEncoder.cs +++ b/src/Microsoft.Extensions.WebEncoders/Testing/UrlTestEncoder.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.IO; using System.Text.Encodings.Web; @@ -18,11 +19,36 @@ namespace Microsoft.Extensions.WebEncoders.Testing public override string Encode(string value) { + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (value.Length == 0) + { + return string.Empty; + } + return $"UrlEncode[[{value}]]"; } public override void Encode(TextWriter output, char[] value, int startIndex, int characterCount) { + if (output == null) + { + throw new ArgumentNullException(nameof(output)); + } + + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (characterCount == 0) + { + return; + } + output.Write("UrlEncode[["); output.Write(value, startIndex, characterCount); output.Write("]]"); @@ -30,6 +56,21 @@ namespace Microsoft.Extensions.WebEncoders.Testing public override void Encode(TextWriter output, string value, int startIndex, int characterCount) { + if (output == null) + { + throw new ArgumentNullException(nameof(output)); + } + + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (characterCount == 0) + { + return; + } + output.Write("UrlEncode[["); output.Write(value.Substring(startIndex, characterCount)); output.Write("]]"); @@ -45,8 +86,17 @@ namespace Microsoft.Extensions.WebEncoders.Testing return -1; } - public override unsafe bool TryEncodeUnicodeScalar(int unicodeScalar, char* buffer, int bufferLength, out int numberOfCharactersWritten) + public override unsafe bool TryEncodeUnicodeScalar( + int unicodeScalar, + char* buffer, + int bufferLength, + out int numberOfCharactersWritten) { + if (buffer == null) + { + throw new ArgumentNullException(nameof(buffer)); + } + numberOfCharactersWritten = 0; return false; } diff --git a/test/Microsoft.Extensions.WebEncoders.Tests/HtmlTestEncoderTest.cs b/test/Microsoft.Extensions.WebEncoders.Tests/HtmlTestEncoderTest.cs new file mode 100644 index 0000000000..baafedc4de --- /dev/null +++ b/test/Microsoft.Extensions.WebEncoders.Tests/HtmlTestEncoderTest.cs @@ -0,0 +1,26 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Xunit; + +namespace Microsoft.Extensions.WebEncoders.Testing +{ + public class HtmlTestEncoderTest + { + [Theory] + [InlineData("", "")] + [InlineData("abcd", "HtmlEncode[[abcd]]")] + [InlineData("<<''\"\">>", "HtmlEncode[[<<''\"\">>]]")] + public void StringEncode_EncodesAsExpected(string input, string expectedOutput) + { + // Arrange + var encoder = new HtmlTestEncoder(); + + // Act + var output = encoder.Encode(input); + + // Assert + Assert.Equal(expectedOutput, output); + } + } +}