From: Badre BSAILA <54767641+pedrobsaila@users.noreply.github.com>
Date: Tue, 8 Mar 2022 16:15:18 +0000 (+0100)
Subject: use the name of the header when throwing exception for invalid chars (#66250)
X-Git-Tag: accepted/tizen/unified/riscv/20231226.055536~10443
X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fa5dcda23c681031b5d2bd68482702baef80b836;p=platform%2Fupstream%2Fdotnet%2Fruntime.git
use the name of the header when throwing exception for invalid chars (#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
---
diff --git a/src/libraries/Common/src/System/Net/HttpValidationHelpers.cs b/src/libraries/Common/src/System/Net/HttpValidationHelpers.cs
index 9790382..4ea758a 100644
--- a/src/libraries/Common/src/System/Net/HttpValidationHelpers.cs
+++ b/src/libraries/Common/src/System/Net/HttpValidationHelpers.cs
@@ -10,13 +10,13 @@ namespace System.Net
// 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;
}
diff --git a/src/libraries/System.Net.Http/src/Resources/Strings.resx b/src/libraries/System.Net.Http/src/Resources/Strings.resx
index d830ac9..b57a936 100644
--- a/src/libraries/System.Net.Http/src/Resources/Strings.resx
+++ b/src/libraries/System.Net.Http/src/Resources/Strings.resx
@@ -136,7 +136,7 @@
Cannot add value because header '{0}' does not support multiple values.
- The header name format is invalid.
+ The header name '{0}' has an invalid format.
The format of value '{0}' is invalid.
diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeaders.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeaders.cs
index db68c44..da053ca 100644
--- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeaders.cs
+++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeaders.cs
@@ -1046,7 +1046,7 @@ namespace System.Net.Http.Headers
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)
diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs
index 79196e7..4d4d259 100644
--- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs
+++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs
@@ -2359,6 +2359,13 @@ namespace System.Net.Http.Tests
Assert.Equal(new[] { "newValue" }, valuesFor3);
}
+ [Fact]
+ public void TryAddInvalidHeader_ShouldThrowFormatException()
+ {
+ MockHeaders headers = new MockHeaders();
+ AssertExtensions.ThrowsContains(() => headers.Remove("\u0080"), "\u0080");
+ }
+
public static IEnumerable
- Specified value has invalid HTTP Header characters.
+ Specified value '{0}' has invalid HTTP Header characters.
Specified value does not have a ':' separator.
diff --git a/src/libraries/System.Net.WebHeaderCollection/tests/WebHeaderCollectionTest.cs b/src/libraries/System.Net.WebHeaderCollection/tests/WebHeaderCollectionTest.cs
index 6adb462..f4ad906 100644
--- a/src/libraries/System.Net.WebHeaderCollection/tests/WebHeaderCollectionTest.cs
+++ b/src/libraries/System.Net.WebHeaderCollection/tests/WebHeaderCollectionTest.cs
@@ -151,7 +151,8 @@ namespace System.Net.Tests
public void Setter_InvalidName_Throws(string name)
{
WebHeaderCollection w = new WebHeaderCollection();
- AssertExtensions.Throws("name", () => w[name] = "test");
+ ArgumentException exception = AssertExtensions.Throws("name", () => w[name] = "test");
+ Assert.Contains(name, exception.Message);
}
public static object[][] InvalidValues = {
@@ -234,7 +235,8 @@ namespace System.Net.Tests
public void Remove_InvalidHeader_ThrowsArgumentException(string name)
{
var headers = new WebHeaderCollection();
- AssertExtensions.Throws("name", () => headers.Remove(name));
+ ArgumentException exception = AssertExtensions.Throws("name", () => headers.Remove(name));
+ Assert.Contains(name, exception.Message);
}
[Fact]
@@ -481,7 +483,12 @@ namespace System.Net.Tests
public void Add_InvalidName_ThrowsArgumentException(string name)
{
var headers = new WebHeaderCollection();
- AssertExtensions.Throws("name", () => headers.Add(name, "value"));
+ ArgumentException exception = AssertExtensions.Throws("name", () => headers.Add(name, "value"));
+
+ if (!string.IsNullOrEmpty(name))
+ {
+ Assert.Contains(name, exception.Message);
+ }
}
[Theory]