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)`
This commit is contained in:
parent
c80946260a
commit
037196d5c7
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue