Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 037196d

Browse files
committed
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)`
1 parent c809462 commit 037196d

File tree

4 files changed

+179
-3
lines changed

4 files changed

+179
-3
lines changed

src/Microsoft.Extensions.WebEncoders/Testing/HtmlTestEncoder.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.IO;
56
using System.Text.Encodings.Web;
67

@@ -18,18 +19,58 @@ public override int MaxOutputCharactersPerInputCharacter
1819

1920
public override string Encode(string value)
2021
{
22+
if (value == null)
23+
{
24+
throw new ArgumentNullException(nameof(value));
25+
}
26+
27+
if (value.Length == 0)
28+
{
29+
return string.Empty;
30+
}
31+
2132
return $"HtmlEncode[[{value}]]";
2233
}
2334

2435
public override void Encode(TextWriter output, char[] value, int startIndex, int characterCount)
2536
{
37+
if (output == null)
38+
{
39+
throw new ArgumentNullException(nameof(output));
40+
}
41+
42+
if (value == null)
43+
{
44+
throw new ArgumentNullException(nameof(value));
45+
}
46+
47+
if (characterCount == 0)
48+
{
49+
return;
50+
}
51+
2652
output.Write("HtmlEncode[[");
2753
output.Write(value, startIndex, characterCount);
2854
output.Write("]]");
2955
}
3056

3157
public override void Encode(TextWriter output, string value, int startIndex, int characterCount)
3258
{
59+
if (output == null)
60+
{
61+
throw new ArgumentNullException(nameof(output));
62+
}
63+
64+
if (value == null)
65+
{
66+
throw new ArgumentNullException(nameof(value));
67+
}
68+
69+
if (characterCount == 0)
70+
{
71+
return;
72+
}
73+
3374
output.Write("HtmlEncode[[");
3475
output.Write(value.Substring(startIndex, characterCount));
3576
output.Write("]]");
@@ -45,8 +86,17 @@ public override unsafe int FindFirstCharacterToEncode(char* text, int textLength
4586
return -1;
4687
}
4788

48-
public override unsafe bool TryEncodeUnicodeScalar(int unicodeScalar, char* buffer, int bufferLength, out int numberOfCharactersWritten)
89+
public override unsafe bool TryEncodeUnicodeScalar(
90+
int unicodeScalar,
91+
char* buffer,
92+
int bufferLength,
93+
out int numberOfCharactersWritten)
4994
{
95+
if (buffer == null)
96+
{
97+
throw new ArgumentNullException(nameof(buffer));
98+
}
99+
50100
numberOfCharactersWritten = 0;
51101
return false;
52102
}

src/Microsoft.Extensions.WebEncoders/Testing/JavaScriptTestEncoder.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.IO;
56
using System.Text.Encodings.Web;
67

@@ -18,18 +19,58 @@ public override int MaxOutputCharactersPerInputCharacter
1819

1920
public override string Encode(string value)
2021
{
22+
if (value == null)
23+
{
24+
throw new ArgumentNullException(nameof(value));
25+
}
26+
27+
if (value.Length == 0)
28+
{
29+
return string.Empty;
30+
}
31+
2132
return $"JavaScriptEncode[[{value}]]";
2233
}
2334

2435
public override void Encode(TextWriter output, char[] value, int startIndex, int characterCount)
2536
{
37+
if (output == null)
38+
{
39+
throw new ArgumentNullException(nameof(output));
40+
}
41+
42+
if (value == null)
43+
{
44+
throw new ArgumentNullException(nameof(value));
45+
}
46+
47+
if (characterCount == 0)
48+
{
49+
return;
50+
}
51+
2652
output.Write("JavaScriptEncode[[");
2753
output.Write(value, startIndex, characterCount);
2854
output.Write("]]");
2955
}
3056

3157
public override void Encode(TextWriter output, string value, int startIndex, int characterCount)
3258
{
59+
if (output == null)
60+
{
61+
throw new ArgumentNullException(nameof(output));
62+
}
63+
64+
if (value == null)
65+
{
66+
throw new ArgumentNullException(nameof(value));
67+
}
68+
69+
if (characterCount == 0)
70+
{
71+
return;
72+
}
73+
3374
output.Write("JavaScriptEncode[[");
3475
output.Write(value.Substring(startIndex, characterCount));
3576
output.Write("]]");
@@ -45,8 +86,17 @@ public override unsafe int FindFirstCharacterToEncode(char* text, int textLength
4586
return -1;
4687
}
4788

48-
public override unsafe bool TryEncodeUnicodeScalar(int unicodeScalar, char* buffer, int bufferLength, out int numberOfCharactersWritten)
89+
public override unsafe bool TryEncodeUnicodeScalar(
90+
int unicodeScalar,
91+
char* buffer,
92+
int bufferLength,
93+
out int numberOfCharactersWritten)
4994
{
95+
if (buffer == null)
96+
{
97+
throw new ArgumentNullException(nameof(buffer));
98+
}
99+
50100
numberOfCharactersWritten = 0;
51101
return false;
52102
}

src/Microsoft.Extensions.WebEncoders/Testing/UrlTestEncoder.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.IO;
56
using System.Text.Encodings.Web;
67

@@ -18,18 +19,58 @@ public override int MaxOutputCharactersPerInputCharacter
1819

1920
public override string Encode(string value)
2021
{
22+
if (value == null)
23+
{
24+
throw new ArgumentNullException(nameof(value));
25+
}
26+
27+
if (value.Length == 0)
28+
{
29+
return string.Empty;
30+
}
31+
2132
return $"UrlEncode[[{value}]]";
2233
}
2334

2435
public override void Encode(TextWriter output, char[] value, int startIndex, int characterCount)
2536
{
37+
if (output == null)
38+
{
39+
throw new ArgumentNullException(nameof(output));
40+
}
41+
42+
if (value == null)
43+
{
44+
throw new ArgumentNullException(nameof(value));
45+
}
46+
47+
if (characterCount == 0)
48+
{
49+
return;
50+
}
51+
2652
output.Write("UrlEncode[[");
2753
output.Write(value, startIndex, characterCount);
2854
output.Write("]]");
2955
}
3056

3157
public override void Encode(TextWriter output, string value, int startIndex, int characterCount)
3258
{
59+
if (output == null)
60+
{
61+
throw new ArgumentNullException(nameof(output));
62+
}
63+
64+
if (value == null)
65+
{
66+
throw new ArgumentNullException(nameof(value));
67+
}
68+
69+
if (characterCount == 0)
70+
{
71+
return;
72+
}
73+
3374
output.Write("UrlEncode[[");
3475
output.Write(value.Substring(startIndex, characterCount));
3576
output.Write("]]");
@@ -45,8 +86,17 @@ public override unsafe int FindFirstCharacterToEncode(char* text, int textLength
4586
return -1;
4687
}
4788

48-
public override unsafe bool TryEncodeUnicodeScalar(int unicodeScalar, char* buffer, int bufferLength, out int numberOfCharactersWritten)
89+
public override unsafe bool TryEncodeUnicodeScalar(
90+
int unicodeScalar,
91+
char* buffer,
92+
int bufferLength,
93+
out int numberOfCharactersWritten)
4994
{
95+
if (buffer == null)
96+
{
97+
throw new ArgumentNullException(nameof(buffer));
98+
}
99+
50100
numberOfCharactersWritten = 0;
51101
return false;
52102
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Xunit;
5+
6+
namespace Microsoft.Extensions.WebEncoders.Testing
7+
{
8+
public class HtmlTestEncoderTest
9+
{
10+
[Theory]
11+
[InlineData("", "")]
12+
[InlineData("abcd", "HtmlEncode[[abcd]]")]
13+
[InlineData("<<''\"\">>", "HtmlEncode[[<<''\"\">>]]")]
14+
public void StringEncode_EncodesAsExpected(string input, string expectedOutput)
15+
{
16+
// Arrange
17+
var encoder = new HtmlTestEncoder();
18+
19+
// Act
20+
var output = encoder.Encode(input);
21+
22+
// Assert
23+
Assert.Equal(expectedOutput, output);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)