use the name of the header when throwing exception for invalid chars (#66250)
authorBadre BSAILA <54767641+pedrobsaila@users.noreply.github.com>
Tue, 8 Mar 2022 16:15:18 +0000 (17:15 +0100)
committerGitHub <noreply@github.com>
Tue, 8 Mar 2022 16:15:18 +0000 (17:15 +0100)
* 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

src/libraries/Common/src/System/Net/HttpValidationHelpers.cs
src/libraries/System.Net.Http/src/Resources/Strings.resx
src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpHeaders.cs
src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs
src/libraries/System.Net.WebHeaderCollection/src/Resources/Strings.resx
src/libraries/System.Net.WebHeaderCollection/tests/WebHeaderCollectionTest.cs

index 9790382..4ea758a 100644 (file)
@@ -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;
         }
index d830ac9..b57a936 100644 (file)
     <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>
index db68c44..da053ca 100644 (file)
@@ -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)
index 79196e7..4d4d259 100644 (file)
@@ -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<FormatException>(() => headers.Remove("\u0080"), "\u0080");
+        }
+
         public static IEnumerable<object[]> NumberOfHeadersUpToArrayThreshold_AddNonValidated_EnumerateNonValidated()
         {
             for (int i = 0; i <= HttpHeaders.ArrayThreshold; i++)
index 86b2906..9003b0b 100644 (file)
@@ -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>
index 6adb462..f4ad906 100644 (file)
@@ -151,7 +151,8 @@ namespace System.Net.Tests
         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 = {
@@ -234,7 +235,8 @@ namespace System.Net.Tests
         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]
@@ -481,7 +483,12 @@ namespace System.Net.Tests
         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]