From 037196d5c7eff127d6dc7ef29e6ba029b8c129e6 Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Fri, 30 Oct 2015 19:09:08 -0700 Subject: [PATCH] Restore `null` and `string.Empty` handling from `EncoderExtensions` - prior test encoders were never invoked for `null` or empty `string`s e.g. ``` c# public static void HtmlEncode(this IHtmlEncoder htmlEncoder, string value, TextWriter output) { ... if (!String.IsNullOrEmpty(value)) { htmlEncoder.HtmlEncode(value, 0, value.Length, output); } } ``` - add missing `null` checks and handle `string.Empty` in `TextWriter output, string value, ...` overloads - better match for the underlying `TextEncoder` behaviour - `EncoderExtensions` provided an API like `TextEncoder.Encode(TextWriter output, string value)` - that method calls `Encode(TextWriter output, string value, int startIndex, int characterCount)` --- .../Testing/HtmlTestEncoder.cs | 52 ++++++++++++++++++- .../Testing/JavaScriptTestEncoder.cs | 52 ++++++++++++++++++- .../Testing/UrlTestEncoder.cs | 52 ++++++++++++++++++- .../HtmlTestEncoderTest.cs | 26 ++++++++++ 4 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 test/Microsoft.Extensions.WebEncoders.Tests/HtmlTestEncoderTest.cs 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); + } + } +}