Skip to content

Commit

Permalink
use the name of the header when throwing exception for invalid chars (#…
Browse files Browse the repository at this point in the history
…66250)

* use the name of the header when throwing exception for invalid chars

* delete VS code cleanups

* delete VS code cleanups 2

* add suggested changes 1

* delete VS code cleanups 3

* delete VS code cleanups 4

* delete VS code cleanups 5
  • Loading branch information
pedrobsaila authored Mar 8, 2022
1 parent 8fe2d2b commit fa5dcda
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/libraries/Common/src/System/Net/HttpValidationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ internal static string CheckBadHeaderNameChars(string name)
// First, check for absence of separators and spaces.
if (IsInvalidMethodOrHeaderString(name))
{
throw new ArgumentException(SR.net_WebHeaderInvalidHeaderChars, nameof(name));
throw new ArgumentException(string.Format(SR.net_WebHeaderInvalidHeaderChars, name), nameof(name));
}

// Second, check for non CTL ASCII-7 characters (32-126).
if (ContainsNonAsciiChars(name))
{
throw new ArgumentException(SR.net_WebHeaderInvalidHeaderChars, nameof(name));
throw new ArgumentException(string.Format(SR.net_WebHeaderInvalidHeaderChars, name), nameof(name));
}
return name;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Net.Http/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
<value>Cannot add value because header '{0}' does not support multiple values.</value>
</data>
<data name="net_http_headers_invalid_header_name" xml:space="preserve">
<value>The header name format is invalid.</value>
<value>The header name '{0}' has an invalid format.</value>
</data>
<data name="net_http_headers_invalid_value" xml:space="preserve">
<value>The format of value '{0}' is invalid.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ private HeaderDescriptor GetHeaderDescriptor(string name)

if (!HeaderDescriptor.TryGet(name, out HeaderDescriptor descriptor))
{
throw new FormatException(SR.net_http_headers_invalid_header_name);
throw new FormatException(string.Format(SR.net_http_headers_invalid_header_name, name));
}

if ((descriptor.HeaderType & _allowedHeaderTypes) != 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2359,6 +2359,13 @@ public void Add_LargeNumberOfHeaders_OperationsStillSupported(int numberOfHeader
Assert.Equal(new[] { "newValue" }, valuesFor3);
}

[Fact]
public void TryAddInvalidHeader_ShouldThrowFormatException()
{
MockHeaders headers = new MockHeaders();
AssertExtensions.ThrowsContains<FormatException>(() => headers.Remove("\u0080"), "\u0080");
}

public static IEnumerable<object[]> NumberOfHeadersUpToArrayThreshold_AddNonValidated_EnumerateNonValidated()
{
for (int i = 0; i <= HttpHeaders.ArrayThreshold; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<value>Specified value has invalid CRLF characters.</value>
</data>
<data name="net_WebHeaderInvalidHeaderChars" xml:space="preserve">
<value>Specified value has invalid HTTP Header characters.</value>
<value>Specified value '{0}' has invalid HTTP Header characters.</value>
</data>
<data name="net_WebHeaderMissingColon" xml:space="preserve">
<value>Specified value does not have a ':' separator.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public void Setter_NullOrEmptyName_Throws(string name)
public void Setter_InvalidName_Throws(string name)
{
WebHeaderCollection w = new WebHeaderCollection();
AssertExtensions.Throws<ArgumentException>("name", () => w[name] = "test");
ArgumentException exception = AssertExtensions.Throws<ArgumentException>("name", () => w[name] = "test");
Assert.Contains(name, exception.Message);
}

public static object[][] InvalidValues = {
Expand Down Expand Up @@ -234,7 +235,8 @@ public void Remove_NullOrEmptyHeader_ThrowsArgumentNullException(string name)
public void Remove_InvalidHeader_ThrowsArgumentException(string name)
{
var headers = new WebHeaderCollection();
AssertExtensions.Throws<ArgumentException>("name", () => headers.Remove(name));
ArgumentException exception = AssertExtensions.Throws<ArgumentException>("name", () => headers.Remove(name));
Assert.Contains(name, exception.Message);
}

[Fact]
Expand Down Expand Up @@ -481,7 +483,12 @@ public void Add_NullName_ThrowsArgumentNullException()
public void Add_InvalidName_ThrowsArgumentException(string name)
{
var headers = new WebHeaderCollection();
AssertExtensions.Throws<ArgumentException>("name", () => headers.Add(name, "value"));
ArgumentException exception = AssertExtensions.Throws<ArgumentException>("name", () => headers.Add(name, "value"));

if (!string.IsNullOrEmpty(name))
{
Assert.Contains(name, exception.Message);
}
}

[Theory]
Expand Down

0 comments on commit fa5dcda

Please sign in to comment.