diff --git a/src/libraries/Common/src/System/Net/WebSockets/WebSocketValidate.cs b/src/libraries/Common/src/System/Net/WebSockets/WebSocketValidate.cs index dafe325c8c07b8..7c97c082d26f89 100644 --- a/src/libraries/Common/src/System/Net/WebSockets/WebSocketValidate.cs +++ b/src/libraries/Common/src/System/Net/WebSockets/WebSocketValidate.cs @@ -58,10 +58,7 @@ internal static void ThrowIfInvalidState(WebSocketState currentState, bool isDis internal static void ValidateSubprotocol(string subProtocol) { - if (string.IsNullOrWhiteSpace(subProtocol)) - { - throw new ArgumentException(SR.net_WebSockets_InvalidEmptySubProtocol, nameof(subProtocol)); - } + ArgumentException.ThrowIfNullOrWhiteSpace(subProtocol); int indexOfInvalidChar = subProtocol.AsSpan().IndexOfAnyExcept(s_validSubprotocolChars); if (indexOfInvalidChar >= 0) diff --git a/src/libraries/System.ComponentModel.Annotations/src/Resources/Strings.resx b/src/libraries/System.ComponentModel.Annotations/src/Resources/Strings.resx index 0cac133cb27bf4..71307a5f361cf7 100644 --- a/src/libraries/System.ComponentModel.Annotations/src/Resources/Strings.resx +++ b/src/libraries/System.ComponentModel.Annotations/src/Resources/Strings.resx @@ -60,9 +60,6 @@ The {0} field does not equal any of the values specified in AllowedValuesAttribute. - - The argument '{0}' cannot be null, empty or contain only whitespace. - The associated metadata type for type '{0}' contains the following unknown properties or fields: {1}. Please make sure that the names of these members match the names of the properties on the main type. diff --git a/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/ColumnAttribute.cs b/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/ColumnAttribute.cs index 66a112ef8b45d7..c71b1c13431f75 100644 --- a/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/ColumnAttribute.cs +++ b/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/ColumnAttribute.cs @@ -28,11 +28,7 @@ public ColumnAttribute() /// The name of the column the property is mapped to. public ColumnAttribute(string name) { - if (string.IsNullOrWhiteSpace(name)) - { - throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(name)), nameof(name)); - } - + ArgumentException.ThrowIfNullOrWhiteSpace(name); Name = name; } @@ -64,11 +60,7 @@ public string? TypeName get => _typeName; set { - if (string.IsNullOrWhiteSpace(value)) - { - throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(value)), nameof(value)); - } - + ArgumentException.ThrowIfNullOrWhiteSpace(value); _typeName = value; } } diff --git a/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/ForeignKeyAttribute.cs b/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/ForeignKeyAttribute.cs index a93bc6e328ef84..f640f5f597bfb8 100644 --- a/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/ForeignKeyAttribute.cs +++ b/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/ForeignKeyAttribute.cs @@ -23,11 +23,7 @@ public class ForeignKeyAttribute : Attribute /// public ForeignKeyAttribute(string name) { - if (string.IsNullOrWhiteSpace(name)) - { - throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(name)), nameof(name)); - } - + ArgumentException.ThrowIfNullOrWhiteSpace(name); Name = name; } diff --git a/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/InversePropertyAttribute.cs b/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/InversePropertyAttribute.cs index c6d78bdd71365d..4f2b3ceec6f078 100644 --- a/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/InversePropertyAttribute.cs +++ b/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/InversePropertyAttribute.cs @@ -17,11 +17,7 @@ public class InversePropertyAttribute : Attribute /// The navigation property representing the other end of the same relationship. public InversePropertyAttribute(string property) { - if (string.IsNullOrWhiteSpace(property)) - { - throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(property)), nameof(property)); - } - + ArgumentException.ThrowIfNullOrWhiteSpace(property); Property = property; } diff --git a/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/TableAttribute.cs b/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/TableAttribute.cs index 489bf85830c827..19f00eb5fc1a6e 100644 --- a/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/TableAttribute.cs +++ b/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Schema/TableAttribute.cs @@ -20,11 +20,7 @@ public class TableAttribute : Attribute /// The name of the table the class is mapped to. public TableAttribute(string name) { - if (string.IsNullOrWhiteSpace(name)) - { - throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(name)), nameof(name)); - } - + ArgumentException.ThrowIfNullOrWhiteSpace(name); Name = name; } @@ -42,11 +38,7 @@ public string? Schema get => _schema; set { - if (string.IsNullOrWhiteSpace(value)) - { - throw new ArgumentException(SR.Format(SR.ArgumentIsNullOrWhitespace, nameof(value)), nameof(value)); - } - + ArgumentException.ThrowIfNullOrWhiteSpace(value); _schema = value; } } diff --git a/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/ColumnAttributeTests.cs b/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/ColumnAttributeTests.cs index feef0f8a09ab6c..e8283ed52c3d2c 100644 --- a/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/ColumnAttributeTests.cs +++ b/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/ColumnAttributeTests.cs @@ -28,9 +28,15 @@ public static void Ctor_String(string name) [Theory] [InlineData(null)] + public static void Ctor_String_NullName_ThrowsArgumentException(string name) + { + AssertExtensions.Throws("name", null, () => new ColumnAttribute(name)); + } + + [Theory] [InlineData("")] [InlineData(" \t\r\n")] - public static void Ctor_String_NullOrWhitespaceName_ThrowsArgumentException(string name) + public static void Ctor_String_WhitespaceName_ThrowsArgumentException(string name) { AssertExtensions.Throws("name", null, () => new ColumnAttribute(name)); } @@ -61,9 +67,16 @@ public static void TypeName_Set_ReturnsExpected(string value) [Theory] [InlineData(null)] + public static void TypeName_Set_NullValue_ThrowsArgumentException(string value) + { + ColumnAttribute attribute = new ColumnAttribute(); + AssertExtensions.Throws("value", null, () => attribute.TypeName = value); + } + + [Theory] [InlineData("")] [InlineData(" \t\r\n")] - public static void TypeName_Set_NullOrWhitespaceValue_ThrowsArgumentException(string value) + public static void TypeName_Set_WhitespaceValue_ThrowsArgumentException(string value) { ColumnAttribute attribute = new ColumnAttribute(); AssertExtensions.Throws("value", null, () => attribute.TypeName = value); diff --git a/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/ForeignKeyAttributeTests.cs b/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/ForeignKeyAttributeTests.cs index fee82dac2c3f34..45c732d212595c 100644 --- a/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/ForeignKeyAttributeTests.cs +++ b/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/ForeignKeyAttributeTests.cs @@ -17,9 +17,15 @@ public static void Ctor_String(string name) [Theory] [InlineData(null)] + public static void Ctor_String_NullName_ThrowsArgumentException(string name) + { + AssertExtensions.Throws("name", null, () => new ForeignKeyAttribute(name)); + } + + [Theory] [InlineData("")] [InlineData(" \t\r\n")] - public static void Ctor_String_NullOrWhitespaceName_ThrowsArgumentException(string name) + public static void Ctor_String_WhitespaceName_ThrowsArgumentException(string name) { AssertExtensions.Throws("name", null, () => new ForeignKeyAttribute(name)); } diff --git a/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/InversePropertyAttributeTests.cs b/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/InversePropertyAttributeTests.cs index 9d17043325fce0..cb1362867781d6 100644 --- a/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/InversePropertyAttributeTests.cs +++ b/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/InversePropertyAttributeTests.cs @@ -17,9 +17,15 @@ public static void Ctor_String(string property) [Theory] [InlineData(null)] + public static void Ctor_String_NullProperty_ThrowsArgumentException(string property) + { + AssertExtensions.Throws("property", null, () => new InversePropertyAttribute(property)); + } + + [Theory] [InlineData("")] [InlineData(" \t\r\n")] - public static void Ctor_String_NullOrWhitespaceProperty_ThrowsArgumentException(string property) + public static void Ctor_String_WhitespaceProperty_ThrowsArgumentException(string property) { AssertExtensions.Throws("property", null, () => new InversePropertyAttribute(property)); } diff --git a/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/TableAttributeTests.cs b/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/TableAttributeTests.cs index d34b762e3a43c6..3e5aab421542d5 100644 --- a/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/TableAttributeTests.cs +++ b/src/libraries/System.ComponentModel.Annotations/tests/System/ComponentModel/DataAnnotations/Schema/TableAttributeTests.cs @@ -18,9 +18,15 @@ public static void Ctor_String(string name) [Theory] [InlineData(null)] + public static void Ctor_String_NullName_ThrowsArgumentException(string name) + { + AssertExtensions.Throws("name", null, () => new TableAttribute(name)); + } + + [Theory] [InlineData("")] [InlineData(" \t\r\n")] - public static void Ctor_String_NullOrWhitespaceName_ThrowsArgumentException(string name) + public static void Ctor_String_WhitespaceName_ThrowsArgumentException(string name) { AssertExtensions.Throws("name", null, () => new TableAttribute(name)); } @@ -35,9 +41,16 @@ public static void Schema_Set_ReturnsExpected(string value) [Theory] [InlineData(null)] + public static void Schema_Set_NullValue_ThrowsArgumentException(string value) + { + TableAttribute attribute = new TableAttribute("Perspicacia Tick"); + AssertExtensions.Throws("value", null, () => attribute.Schema = value); + } + + [Theory] [InlineData("")] [InlineData(" \t\r\n")] - public static void Schema_Set_NullOrWhitespaceValue_ThrowsArgumentException(string value) + public static void Schema_Set_WhitespaceValue_ThrowsArgumentException(string value) { TableAttribute attribute = new TableAttribute("Perspicacia Tick"); AssertExtensions.Throws("value", null, () => attribute.Schema = value); diff --git a/src/libraries/System.Net.Http/src/Resources/Strings.resx b/src/libraries/System.Net.Http/src/Resources/Strings.resx index 8cd19984d7216b..b89c5a6c1c9b78 100644 --- a/src/libraries/System.Net.Http/src/Resources/Strings.resx +++ b/src/libraries/System.Net.Http/src/Resources/Strings.resx @@ -180,9 +180,6 @@ The content's stream has already been retrieved via async ReadAsStreamAsync and cannot be subsequently accessed synchronously. - - The value cannot be null or empty. - The request message was already sent. Cannot send the same request message multiple times. diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/AuthenticationHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/AuthenticationHeaderValue.cs index fedc31077e9e75..930cb8f8408587 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/AuthenticationHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/AuthenticationHeaderValue.cs @@ -37,7 +37,7 @@ public AuthenticationHeaderValue(string scheme) public AuthenticationHeaderValue(string scheme, string? parameter) { - HeaderUtilities.CheckValidToken(scheme, nameof(scheme)); + HeaderUtilities.CheckValidToken(scheme); HttpHeaders.CheckContainsNewLine(parameter); _scheme = scheme; _parameter = parameter; diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/CacheControlHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/CacheControlHeaderValue.cs index 3067cc68689b46..c72e1965e14386 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/CacheControlHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/CacheControlHeaderValue.cs @@ -594,7 +594,7 @@ object ICloneable.Clone() private sealed class TokenObjectCollection : ObjectCollection { - public override void Validate(string item) => HeaderUtilities.CheckValidToken(item, nameof(item)); + public override void Validate(string item) => HeaderUtilities.CheckValidToken(item); public int GetHashCode(StringComparer comparer) { diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ContentDispositionHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ContentDispositionHeaderValue.cs index 02cc4b3289caf5..05f731954469e3 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ContentDispositionHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ContentDispositionHeaderValue.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; +using System.Runtime.CompilerServices; using System.Text; namespace System.Net.Http.Headers @@ -34,7 +35,7 @@ public string DispositionType get { return _dispositionType; } set { - CheckDispositionTypeFormat(value, nameof(value)); + CheckDispositionTypeFormat(value); _dispositionType = value; } } @@ -139,7 +140,7 @@ protected ContentDispositionHeaderValue(ContentDispositionHeaderValue source) public ContentDispositionHeaderValue(string dispositionType) { - CheckDispositionTypeFormat(dispositionType, nameof(dispositionType)); + CheckDispositionTypeFormat(dispositionType); _dispositionType = dispositionType; } @@ -270,12 +271,9 @@ private static int GetDispositionTypeExpressionLength(string input, int startInd return typeLength; } - private static void CheckDispositionTypeFormat(string dispositionType, string parameterName) + private static void CheckDispositionTypeFormat(string dispositionType, [CallerArgumentExpression(nameof(dispositionType))] string? parameterName = null) { - if (string.IsNullOrEmpty(dispositionType)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, parameterName); - } + ArgumentException.ThrowIfNullOrWhiteSpace(dispositionType, parameterName); // When adding values using strongly typed objects, no leading/trailing LWS (whitespace) are allowed. int dispositionTypeLength = GetDispositionTypeExpressionLength(dispositionType, 0, out string? tempDispositionType); diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ContentRangeHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ContentRangeHeaderValue.cs index aa08f5e507e035..5c463972d60dd4 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ContentRangeHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ContentRangeHeaderValue.cs @@ -19,7 +19,7 @@ public string Unit get { return _unit; } set { - HeaderUtilities.CheckValidToken(value, nameof(value)); + HeaderUtilities.CheckValidToken(value); _unit = value; } } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/EntityTagHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/EntityTagHeaderValue.cs index fb1e2614686484..d6177a2555a567 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/EntityTagHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/EntityTagHeaderValue.cs @@ -35,10 +35,8 @@ public EntityTagHeaderValue(string tag) public EntityTagHeaderValue(string tag, bool isWeak) { - if (string.IsNullOrEmpty(tag)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, nameof(tag)); - } + ArgumentException.ThrowIfNullOrWhiteSpace(tag); + int length; if ((HttpRuleParser.GetQuotedStringLength(tag, 0, out length) != HttpParseResult.Parsed) || (length != tag.Length)) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderUtilities.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderUtilities.cs index 20730990ce9a42..1a40f471418c41 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderUtilities.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderUtilities.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; +using System.Runtime.CompilerServices; using System.Text; namespace System.Net.Http.Headers @@ -139,12 +140,9 @@ private static void AddHexEscaped(byte c, ref ValueStringBuilder destination) return null; } - internal static void CheckValidToken(string value, string parameterName) + internal static void CheckValidToken(string value, [CallerArgumentExpression(nameof(value))] string? parameterName = null) { - if (string.IsNullOrEmpty(value)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, parameterName); - } + ArgumentException.ThrowIfNullOrWhiteSpace(value, parameterName); if (HttpRuleParser.GetTokenLength(value, 0) != value.Length) { @@ -152,12 +150,9 @@ internal static void CheckValidToken(string value, string parameterName) } } - internal static void CheckValidComment(string value, string parameterName) + internal static void CheckValidComment(string value, [CallerArgumentExpression(nameof(value))] string? parameterName = null) { - if (string.IsNullOrEmpty(value)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, parameterName); - } + ArgumentException.ThrowIfNullOrWhiteSpace(value, parameterName); int length; if ((HttpRuleParser.GetCommentLength(value, 0, out length) != HttpParseResult.Parsed) || @@ -167,12 +162,9 @@ internal static void CheckValidComment(string value, string parameterName) } } - internal static void CheckValidQuotedString(string value, string parameterName) + internal static void CheckValidQuotedString(string value, [CallerArgumentExpression(nameof(value))] string? parameterName = null) { - if (string.IsNullOrEmpty(value)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, parameterName); - } + ArgumentException.ThrowIfNullOrWhiteSpace(value, parameterName); int length; if ((HttpRuleParser.GetQuotedStringLength(value, 0, out length) != HttpParseResult.Parsed) || 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 aa984ff86e41be..090f195fae23e2 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 @@ -1027,10 +1027,7 @@ private static void ParseAndAddValue(HeaderDescriptor descriptor, HeaderStoreIte private HeaderDescriptor GetHeaderDescriptor(string name) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, nameof(name)); - } + ArgumentException.ThrowIfNullOrWhiteSpace(name); if (!HeaderDescriptor.TryGet(name, out HeaderDescriptor descriptor)) { diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/MediaTypeHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/MediaTypeHeaderValue.cs index d87029e3b93365..130c8bdd198927 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/MediaTypeHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/MediaTypeHeaderValue.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; using System.Text; using static System.HexConverter; @@ -68,7 +69,7 @@ public string? MediaType get { return _mediaType; } set { - CheckMediaTypeFormat(value, nameof(value)); + CheckMediaTypeFormat(value); _mediaType = value; } } @@ -100,7 +101,7 @@ public MediaTypeHeaderValue(string mediaType) /// The value to use for the character set. public MediaTypeHeaderValue(string mediaType, string? charSet) { - CheckMediaTypeFormat(mediaType, nameof(mediaType)); + CheckMediaTypeFormat(mediaType); _mediaType = mediaType; if (!string.IsNullOrEmpty(charSet)) @@ -272,12 +273,9 @@ private static int GetMediaTypeExpressionLength(string input, int startIndex, ou return mediaTypeLength; } - private static void CheckMediaTypeFormat(string mediaType, string parameterName) + private static void CheckMediaTypeFormat(string mediaType, [CallerArgumentExpression(nameof(mediaType))] string? parameterName = null) { - if (string.IsNullOrEmpty(mediaType)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, parameterName); - } + ArgumentException.ThrowIfNullOrWhiteSpace(mediaType, parameterName); // When adding values using strongly typed objects, no leading/trailing LWS (whitespace) are allowed. // Also no LWS between type and subtype are allowed. diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/NameValueHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/NameValueHeaderValue.cs index 56c9fb1ba4eb86..6920a881828fcc 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/NameValueHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/NameValueHeaderValue.cs @@ -345,7 +345,7 @@ internal static int GetValueLength(string input, int startIndex) private static void CheckNameValueFormat(string name, string? value) { - HeaderUtilities.CheckValidToken(name, nameof(name)); + HeaderUtilities.CheckValidToken(name); CheckValueFormat(value); } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ProductHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ProductHeaderValue.cs index efdf5f7d39d56a..846f435a099b97 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ProductHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ProductHeaderValue.cs @@ -29,11 +29,11 @@ public ProductHeaderValue(string name) public ProductHeaderValue(string name, string? version) { - HeaderUtilities.CheckValidToken(name, nameof(name)); + HeaderUtilities.CheckValidToken(name); if (!string.IsNullOrEmpty(version)) { - HeaderUtilities.CheckValidToken(version, nameof(version)); + HeaderUtilities.CheckValidToken(version); _version = version; } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ProductInfoHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ProductInfoHeaderValue.cs index 6ed9717836ad6a..1d877bb053a176 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ProductInfoHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ProductInfoHeaderValue.cs @@ -29,7 +29,7 @@ public ProductInfoHeaderValue(ProductHeaderValue product) public ProductInfoHeaderValue(string comment) { - HeaderUtilities.CheckValidComment(comment, nameof(comment)); + HeaderUtilities.CheckValidComment(comment); _comment = comment; } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/RangeHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/RangeHeaderValue.cs index cc7b3259120cf0..85bfdd0ec5c769 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/RangeHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/RangeHeaderValue.cs @@ -19,7 +19,7 @@ public string Unit get { return _unit; } set { - HeaderUtilities.CheckValidToken(value, nameof(value)); + HeaderUtilities.CheckValidToken(value); _unit = value; } } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/StringWithQualityHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/StringWithQualityHeaderValue.cs index d7da89b5cb0bb8..bf7dc4f2dd3d5b 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/StringWithQualityHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/StringWithQualityHeaderValue.cs @@ -20,7 +20,7 @@ public class StringWithQualityHeaderValue : ICloneable public StringWithQualityHeaderValue(string value) { - HeaderUtilities.CheckValidToken(value, nameof(value)); + HeaderUtilities.CheckValidToken(value); _value = value; _quality = NotSetSentinel; @@ -28,7 +28,7 @@ public StringWithQualityHeaderValue(string value) public StringWithQualityHeaderValue(string value, double quality) { - HeaderUtilities.CheckValidToken(value, nameof(value)); + HeaderUtilities.CheckValidToken(value); ArgumentOutOfRangeException.ThrowIfNegative(quality); ArgumentOutOfRangeException.ThrowIfGreaterThan(quality, 1.0); diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/TransferCodingHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/TransferCodingHeaderValue.cs index 67b2b30fd601d0..db30cd036f56fd 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/TransferCodingHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/TransferCodingHeaderValue.cs @@ -33,7 +33,7 @@ protected TransferCodingHeaderValue(TransferCodingHeaderValue source) public TransferCodingHeaderValue(string value) { - HeaderUtilities.CheckValidToken(value, nameof(value)); + HeaderUtilities.CheckValidToken(value); _value = value; } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ViaHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ViaHeaderValue.cs index 65fd928a001bb5..fc9a64e7bc7345 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ViaHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/ViaHeaderValue.cs @@ -47,18 +47,18 @@ public ViaHeaderValue(string protocolVersion, string receivedBy, string? protoco public ViaHeaderValue(string protocolVersion, string receivedBy, string? protocolName, string? comment) { - HeaderUtilities.CheckValidToken(protocolVersion, nameof(protocolVersion)); + HeaderUtilities.CheckValidToken(protocolVersion); CheckReceivedBy(receivedBy); if (!string.IsNullOrEmpty(protocolName)) { - HeaderUtilities.CheckValidToken(protocolName, nameof(protocolName)); + HeaderUtilities.CheckValidToken(protocolName); _protocolName = protocolName; } if (!string.IsNullOrEmpty(comment)) { - HeaderUtilities.CheckValidComment(comment, nameof(comment)); + HeaderUtilities.CheckValidComment(comment); _comment = comment; } @@ -275,10 +275,7 @@ object ICloneable.Clone() private static void CheckReceivedBy(string receivedBy) { - if (string.IsNullOrEmpty(receivedBy)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, nameof(receivedBy)); - } + ArgumentException.ThrowIfNullOrWhiteSpace(receivedBy); // 'receivedBy' can either be a host or a token. Since a token is a valid host, we only verify if the value // is a valid host.; diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/WarningHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/WarningHeaderValue.cs index 586c0b68fc549b..9501eac5f5f5df 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/WarningHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/WarningHeaderValue.cs @@ -28,7 +28,7 @@ public WarningHeaderValue(int code, string agent, string text) { CheckCode(code); CheckAgent(agent); - HeaderUtilities.CheckValidQuotedString(text, nameof(text)); + HeaderUtilities.CheckValidQuotedString(text); _code = code; _agent = agent; @@ -39,7 +39,7 @@ public WarningHeaderValue(int code, string agent, string text, DateTimeOffset da { CheckCode(code); CheckAgent(agent); - HeaderUtilities.CheckValidQuotedString(text, nameof(text)); + HeaderUtilities.CheckValidQuotedString(text); _code = code; _agent = agent; @@ -282,10 +282,7 @@ private static void CheckCode(int code) private static void CheckAgent(string agent) { - if (string.IsNullOrEmpty(agent)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, nameof(agent)); - } + ArgumentException.ThrowIfNullOrWhiteSpace(agent); // 'receivedBy' can either be a host or a token. Since a token is a valid host, we only verify if the value // is a valid host. diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpMethod.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpMethod.cs index cda7a9f228a028..045d85fb97cd8c 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpMethod.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpMethod.cs @@ -78,10 +78,7 @@ public string Method public HttpMethod(string method) { - if (string.IsNullOrEmpty(method)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, nameof(method)); - } + ArgumentException.ThrowIfNullOrWhiteSpace(method); if (!HttpRuleParser.IsToken(method)) { throw new FormatException(SR.net_http_httpmethod_format_error); diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs b/src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs index 3f249987f08a96..961de333e1d6fa 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs @@ -43,10 +43,7 @@ public MultipartContent(string subtype) public MultipartContent(string subtype, string boundary) { - if (string.IsNullOrWhiteSpace(subtype)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, nameof(subtype)); - } + ArgumentException.ThrowIfNullOrWhiteSpace(subtype); ValidateBoundary(boundary); _boundary = boundary; @@ -68,10 +65,7 @@ private static void ValidateBoundary(string boundary) { // NameValueHeaderValue is too restrictive for boundary. // Instead validate it ourselves and then quote it. - if (string.IsNullOrWhiteSpace(boundary)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, nameof(boundary)); - } + ArgumentException.ThrowIfNullOrWhiteSpace(boundary); // RFC 2046 Section 5.1.1 // boundary := 0*69 bcharsnospace diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/MultipartFormDataContent.cs b/src/libraries/System.Net.Http/src/System/Net/Http/MultipartFormDataContent.cs index 7eba9d8aee7660..e09b64e79bb009 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/MultipartFormDataContent.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/MultipartFormDataContent.cs @@ -35,11 +35,7 @@ public override void Add(HttpContent content) public void Add(HttpContent content, string name) { ArgumentNullException.ThrowIfNull(content); - - if (string.IsNullOrWhiteSpace(name)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, nameof(name)); - } + ArgumentException.ThrowIfNullOrWhiteSpace(name); AddInternal(content, name, null); } @@ -47,15 +43,8 @@ public void Add(HttpContent content, string name) public void Add(HttpContent content, string name, string fileName) { ArgumentNullException.ThrowIfNull(content); - - if (string.IsNullOrWhiteSpace(name)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, nameof(name)); - } - if (string.IsNullOrWhiteSpace(fileName)) - { - throw new ArgumentException(SR.net_http_argument_empty_string, nameof(fileName)); - } + ArgumentException.ThrowIfNullOrWhiteSpace(name); + ArgumentException.ThrowIfNullOrWhiteSpace(fileName); AddInternal(content, name, fileName); } diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs index 1961ce9d42ecbd..2a804decd9fb2d 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpMethodTest.cs @@ -52,7 +52,7 @@ public void Ctor_ValidMethodToken_Success() [Fact] public void Ctor_NullMethod_Exception() { - AssertExtensions.Throws("method", () => { new HttpMethod(null); } ); + AssertExtensions.Throws("method", () => { new HttpMethod(null); } ); } [Theory] diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartContentTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartContentTest.cs index c67def2093ed8e..7df54b8ddd9550 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartContentTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartContentTest.cs @@ -16,7 +16,7 @@ public class MultipartContentTest [Fact] public void Ctor_NullOrEmptySubType_ThrowsArgumentException() { - AssertExtensions.Throws("subtype", () => new MultipartContent(null)); + AssertExtensions.Throws("subtype", () => new MultipartContent(null)); AssertExtensions.Throws("subtype", () => new MultipartContent("")); AssertExtensions.Throws("subtype", () => new MultipartContent(" ")); } @@ -24,7 +24,7 @@ public void Ctor_NullOrEmptySubType_ThrowsArgumentException() [Fact] public void Ctor_NullOrEmptyBoundary_ThrowsArgumentException() { - AssertExtensions.Throws("boundary", () => new MultipartContent("Some", null)); + AssertExtensions.Throws("boundary", () => new MultipartContent("Some", null)); AssertExtensions.Throws("boundary", () => new MultipartContent("Some", "")); AssertExtensions.Throws("boundary", () => new MultipartContent("Some", " ")); } diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartFormDataContentTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartFormDataContentTest.cs index 4a02ada4deb5f8..90a75c3b7078b2 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartFormDataContentTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/MultipartFormDataContentTest.cs @@ -22,7 +22,7 @@ public void Ctor_NoParams_CorrectMediaType() [Fact] public void Ctor_NullBoundary_ThrowsArgumentException() { - AssertExtensions.Throws("boundary", () => new MultipartFormDataContent(null)); + AssertExtensions.Throws("boundary", () => new MultipartFormDataContent(null)); } [Fact] @@ -42,7 +42,7 @@ public void Add_NullContent_ThrowsArgumentNullException() public void Add_NullName_ThrowsArgumentException() { var content = new MultipartFormDataContent(); - AssertExtensions.Throws("name", () => content.Add(new StringContent("Hello world"), null)); + AssertExtensions.Throws("name", () => content.Add(new StringContent("Hello world"), null)); } [Fact] @@ -56,7 +56,7 @@ public void Add_EmptyName_ThrowsArgumentException() public void Add_NullFileName_ThrowsArgumentException() { var content = new MultipartFormDataContent(); - AssertExtensions.Throws("fileName", () => content.Add(new StringContent("Hello world"), "name", null)); + AssertExtensions.Throws("fileName", () => content.Add(new StringContent("Hello world"), "name", null)); } [Fact] diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/AuthenticationHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/AuthenticationHeaderValueTest.cs index c5944885917edd..51122ee49838b4 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/AuthenticationHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/AuthenticationHeaderValueTest.cs @@ -16,7 +16,7 @@ public void Ctor_SetBothSchemeAndParameters_MatchExpectation() Assert.Equal("Basic", auth.Scheme); Assert.Equal("realm=\"contoso.com\"", auth.Parameter); - AssertExtensions.Throws("scheme", () => { new AuthenticationHeaderValue(null, "x"); }); + AssertExtensions.Throws("scheme", () => { new AuthenticationHeaderValue(null, "x"); }); AssertExtensions.Throws("scheme", () => { new AuthenticationHeaderValue("", "x"); }); Assert.Throws(() => { new AuthenticationHeaderValue(" x", "x"); }); Assert.Throws(() => { new AuthenticationHeaderValue("x ", "x"); }); diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/CacheControlHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/CacheControlHeaderValueTest.cs index e93c46f4816e2a..595c07a18ad819 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/CacheControlHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/CacheControlHeaderValueTest.cs @@ -51,14 +51,14 @@ public void Properties_SetAndGetAllProperties_SetValueReturnedInGetter() // String collection properties Assert.NotNull(cacheControl.NoCacheHeaders); - AssertExtensions.Throws("item", () => { cacheControl.NoCacheHeaders.Add(null); }); + AssertExtensions.Throws("item", () => { cacheControl.NoCacheHeaders.Add(null); }); Assert.Throws(() => { cacheControl.NoCacheHeaders.Add("invalid token"); }); cacheControl.NoCacheHeaders.Add("token"); Assert.Equal(1, cacheControl.NoCacheHeaders.Count); Assert.Equal("token", cacheControl.NoCacheHeaders.First()); Assert.NotNull(cacheControl.PrivateHeaders); - AssertExtensions.Throws("item", () => { cacheControl.PrivateHeaders.Add(null); }); + AssertExtensions.Throws("item", () => { cacheControl.PrivateHeaders.Add(null); }); Assert.Throws(() => { cacheControl.PrivateHeaders.Add("invalid token"); }); cacheControl.PrivateHeaders.Add("token"); Assert.Equal(1, cacheControl.PrivateHeaders.Count); diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/ContentDispositionHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/ContentDispositionHeaderValueTest.cs index cb44f49b767814..5601ad137d2719 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/ContentDispositionHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/ContentDispositionHeaderValueTest.cs @@ -14,7 +14,7 @@ public class ContentDispositionHeaderValueTest [Fact] public void Ctor_ContentDispositionNull_Throw() { - AssertExtensions.Throws("dispositionType", () => { new ContentDispositionHeaderValue(null); }); + AssertExtensions.Throws("dispositionType", () => { new ContentDispositionHeaderValue(null); }); } [Fact] diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/ContentRangeHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/ContentRangeHeaderValueTest.cs index 69073870fe18f9..aa021f4ea37353 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/ContentRangeHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/ContentRangeHeaderValueTest.cs @@ -79,7 +79,7 @@ public void Unit_GetAndSetValidAndInvalidValues_MatchExpectation() range.Unit = "myunit"; Assert.Equal("myunit", range.Unit); // "Unit (custom value)" - AssertExtensions.Throws("value", () => { range.Unit = null; }); // "" + AssertExtensions.Throws("value", () => { range.Unit = null; }); // "" AssertExtensions.Throws("value", () => { range.Unit = ""; }); // "empty string" Assert.Throws(() => { range.Unit = " x"; }); // "leading space" Assert.Throws(() => { range.Unit = "x "; }); // "trailing space" diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/EntityTagHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/EntityTagHeaderValueTest.cs index 644c6cb1581a28..e43b6ef5c7aa97 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/EntityTagHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/EntityTagHeaderValueTest.cs @@ -12,7 +12,7 @@ public class EntityTagHeaderValueTest [Fact] public void Ctor_ETagNull_Throw() { - AssertExtensions.Throws("tag", () => { new EntityTagHeaderValue(null); }); + AssertExtensions.Throws("tag", () => { new EntityTagHeaderValue(null); }); } [Fact] diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/HeaderUtilitiesTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/HeaderUtilitiesTest.cs index d063895aabbc6e..3dd4d63e94bed2 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/HeaderUtilitiesTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/HeaderUtilitiesTest.cs @@ -82,7 +82,7 @@ public void CheckValidQuotedString_ValidAndInvalidvalues_MatchExpectation() HeaderUtilities.CheckValidQuotedString("\"x\"", "param"); HeaderUtilities.CheckValidQuotedString("\"x y\"", "param"); - AssertExtensions.Throws("param", () => { HeaderUtilities.CheckValidQuotedString(null, "param"); }); + AssertExtensions.Throws("param", () => { HeaderUtilities.CheckValidQuotedString(null, "param"); }); AssertExtensions.Throws("param", () => { HeaderUtilities.CheckValidQuotedString("", "param"); }); Assert.Throws(() => { HeaderUtilities.CheckValidQuotedString("\"x", "param"); }); Assert.Throws(() => { HeaderUtilities.CheckValidQuotedString("\"x\"y", "param"); }); 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 4fbff52d6af72e..9def1da0e76bfd 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs @@ -443,8 +443,17 @@ public void TryAddWithoutValidation_MultipleAddNullValueCollection_Throws() [Theory] [InlineData(null)] + public void Add_SingleUseNullHeaderName_Throw(string headerName) + { + MockHeaders headers = new MockHeaders(); + + AssertExtensions.Throws("name", () => { headers.Add(headerName, "value"); }); + } + + [Theory] [InlineData("")] - public void Add_SingleUseEmptyHeaderName_Throw(string headerName) + [InlineData(" \t\r\n ")] + public void Add_SingleUseWhiteSpaceHeaderName_Throw(string headerName) { MockHeaders headers = new MockHeaders(); @@ -1064,8 +1073,17 @@ public void Clear_AddMultipleHeadersAndThenClear_NoHeadersInCollection() [Theory] [InlineData(null)] + public void Remove_UseNullHeaderName_Throw(string headerName) + { + MockHeaders headers = new MockHeaders(); + + AssertExtensions.Throws("name", () => { headers.Remove(headerName); }); + } + + [Theory] [InlineData("")] - public void Remove_UseEmptyHeaderName_Throw(string headerName) + [InlineData(" \t\r\n ")] + public void Remove_UseWhiteSpaceHeaderName_Throw(string headerName) { MockHeaders headers = new MockHeaders(); @@ -1211,8 +1229,17 @@ public void TryGetValues_GetValuesForExistingHeader_ReturnsTrueAndListOfValues() [Theory] [InlineData(null)] + public void GetValues_UseNullHeaderName_Throw(string headerName) + { + MockHeaders headers = new MockHeaders(); + + AssertExtensions.Throws("name", () => { headers.GetValues(headerName); }); + } + + [Theory] [InlineData("")] - public void GetValues_UseEmptyHeaderName_Throw(string headerName) + [InlineData(" \t\r\n ")] + public void GetValues_UseWhiteSpaceHeaderName_Throw(string headerName) { MockHeaders headers = new MockHeaders(); @@ -1576,7 +1603,16 @@ public void NonValidated_ValidInvalidAndRaw_AllReturned() [Theory] [InlineData(null)] + public void Contains_UseNullHeaderName_Throw(string headerName) + { + MockHeaders headers = new MockHeaders(); + + AssertExtensions.Throws("name", () => { headers.Contains(headerName); }); + } + + [Theory] [InlineData("")] + [InlineData(" \t\r\n ")] public void Contains_UseEmptyHeaderName_Throw(string headerName) { MockHeaders headers = new MockHeaders(); diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/MediaTypeHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/MediaTypeHeaderValueTest.cs index 88d1a58df08211..e51e8efc5e6101 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/MediaTypeHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/MediaTypeHeaderValueTest.cs @@ -13,7 +13,7 @@ public class MediaTypeHeaderValueTest [Fact] public void Ctor_MediaTypeNull_Throw() { - AssertExtensions.Throws("mediaType", () => { new MediaTypeHeaderValue(null); }); + AssertExtensions.Throws("mediaType", () => { new MediaTypeHeaderValue(null); }); } [Fact] diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/NameValueHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/NameValueHeaderValueTest.cs index dc2270e545547b..f9078d3736d1ca 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/NameValueHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/NameValueHeaderValueTest.cs @@ -12,7 +12,7 @@ public class NameValueHeaderValueTest [Fact] public void Ctor_NameNull_Throw() { - AssertExtensions.Throws("name", () => { NameValueHeaderValue nameValue = new NameValueHeaderValue((string)null); }); + AssertExtensions.Throws("name", () => { NameValueHeaderValue nameValue = new NameValueHeaderValue((string)null); }); } [Fact] diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/NameValueWithParametersHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/NameValueWithParametersHeaderValueTest.cs index 5aca687e2882e3..651d22ea96c404 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/NameValueWithParametersHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/NameValueWithParametersHeaderValueTest.cs @@ -13,7 +13,7 @@ public class NameValueWithParametersHeaderValueTest [Fact] public void Ctor_NameNull_Throw() { - AssertExtensions.Throws("name", () => { NameValueWithParametersHeaderValue nameValue = new NameValueWithParametersHeaderValue(null); }); + AssertExtensions.Throws("name", () => { NameValueWithParametersHeaderValue nameValue = new NameValueWithParametersHeaderValue(null); }); } [Fact] diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/ProductHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/ProductHeaderValueTest.cs index e810e572f07cb3..8e75d2fa7b52a3 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/ProductHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/ProductHeaderValueTest.cs @@ -28,7 +28,7 @@ public void Ctor_SetValidHeaderValues_InstanceCreatedCorrectly() [Fact] public void Ctor_UseInvalidValues_Throw() { - AssertExtensions.Throws("name", () => { new ProductHeaderValue(null); }); + AssertExtensions.Throws("name", () => { new ProductHeaderValue(null); }); AssertExtensions.Throws("name", () => { new ProductHeaderValue(string.Empty); }); Assert.Throws(() => { new ProductHeaderValue(" x"); }); Assert.Throws(() => { new ProductHeaderValue("x "); }); diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/ProductInfoHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/ProductInfoHeaderValueTest.cs index 59cacbb496d0dd..72118cce54fd95 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/ProductInfoHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/ProductInfoHeaderValueTest.cs @@ -35,7 +35,7 @@ public void Ctor_CommentOverload_MatchExpectation() Assert.Null(productInfo.Product); Assert.Equal("(this is a comment)", productInfo.Comment); - AssertExtensions.Throws("comment", () => { new ProductInfoHeaderValue((string)null); }); + AssertExtensions.Throws("comment", () => { new ProductInfoHeaderValue((string)null); }); Assert.Throws(() => { new ProductInfoHeaderValue("invalid comment"); }); Assert.Throws(() => { new ProductInfoHeaderValue(" (leading space)"); }); Assert.Throws(() => { new ProductInfoHeaderValue("(trailing space) "); }); diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/RangeConditionHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/RangeConditionHeaderValueTest.cs index edb31cbe1584dd..2648a718c4b368 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/RangeConditionHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/RangeConditionHeaderValueTest.cs @@ -27,7 +27,7 @@ public void Ctor_EntityTagStringOverload_MatchExpectation() Assert.Equal(new EntityTagHeaderValue("\"y\""), rangeCondition.EntityTag); Assert.Null(rangeCondition.Date); - AssertExtensions.Throws("tag", () => { new RangeConditionHeaderValue((string)null); }); + AssertExtensions.Throws("tag", () => { new RangeConditionHeaderValue((string)null); }); } [Fact] diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/RangeHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/RangeHeaderValueTest.cs index 463a6fdac95c99..187d9ce35ac176 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/RangeHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/RangeHeaderValueTest.cs @@ -23,7 +23,7 @@ public void Unit_GetAndSetValidAndInvalidValues_MatchExpectation() range.Unit = "myunit"; Assert.Equal("myunit", range.Unit); - AssertExtensions.Throws("value", () => { range.Unit = null; }); + AssertExtensions.Throws("value", () => { range.Unit = null; }); AssertExtensions.Throws("value", () => { range.Unit = ""; }); Assert.Throws(() => { range.Unit = " x"; }); Assert.Throws(() => { range.Unit = "x "; }); diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/StringWithQualityHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/StringWithQualityHeaderValueTest.cs index 8e3075c10168b3..6da88c1c2accf3 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/StringWithQualityHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/StringWithQualityHeaderValueTest.cs @@ -16,7 +16,7 @@ public void Ctor_StringOnlyOverload_MatchExpectation() Assert.Equal("token", value.Value); Assert.Null(value.Quality); - AssertExtensions.Throws("value", () => { new StringWithQualityHeaderValue(null); }); + AssertExtensions.Throws("value", () => { new StringWithQualityHeaderValue(null); }); AssertExtensions.Throws("value", () => { new StringWithQualityHeaderValue(""); }); Assert.Throws(() => { new StringWithQualityHeaderValue("in valid"); }); } @@ -28,7 +28,7 @@ public void Ctor_StringWithQualityOverload_MatchExpectation() Assert.Equal("token", value.Value); Assert.Equal(0.5, value.Quality); - AssertExtensions.Throws("value", () => { new StringWithQualityHeaderValue(null, 0.1); }); + AssertExtensions.Throws("value", () => { new StringWithQualityHeaderValue(null, 0.1); }); AssertExtensions.Throws("value", () => { new StringWithQualityHeaderValue("", 0.1); }); Assert.Throws(() => { new StringWithQualityHeaderValue("in valid", 0.1); }); diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/TransferCodingHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/TransferCodingHeaderValueTest.cs index e59958ea421037..58be4c458f4472 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/TransferCodingHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/TransferCodingHeaderValueTest.cs @@ -13,7 +13,7 @@ public class TransferCodingHeaderValueTest [Fact] public void Ctor_ValueNull_Throw() { - AssertExtensions.Throws("value", () => { new TransferCodingHeaderValue(null); }); + AssertExtensions.Throws("value", () => { new TransferCodingHeaderValue(null); }); } [Fact] diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/ViaHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/ViaHeaderValueTest.cs index 902a5fd5ac3ecc..a803faa912a61d 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/ViaHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/ViaHeaderValueTest.cs @@ -22,12 +22,12 @@ public void Ctor_ProtocolVersionAndReceivedByOnlyOverload_CallForwardedToOtherCt Assert.Equal("x11", via.ProtocolVersion); Assert.Equal("[::1]:1818", via.ReceivedBy); - AssertExtensions.Throws("protocolVersion", () => { new ViaHeaderValue(null, "host"); }); + AssertExtensions.Throws("protocolVersion", () => { new ViaHeaderValue(null, "host"); }); AssertExtensions.Throws("protocolVersion", () => { new ViaHeaderValue("", "host"); }); Assert.Throws(() => { new ViaHeaderValue("x y", "h"); }); Assert.Throws(() => { new ViaHeaderValue("x ", "h"); }); Assert.Throws(() => { new ViaHeaderValue(" x", "h"); }); - AssertExtensions.Throws("receivedBy", () => { new ViaHeaderValue("1.1", null); }); + AssertExtensions.Throws("receivedBy", () => { new ViaHeaderValue("1.1", null); }); AssertExtensions.Throws("receivedBy", () => { new ViaHeaderValue("1.1", ""); }); Assert.Throws(() => { new ViaHeaderValue("v", "x y"); }); Assert.Throws(() => { new ViaHeaderValue("v", "x "); }); diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/WarningHeaderValueTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/WarningHeaderValueTest.cs index dc867577ea76e7..65de44e7ec0c16 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/WarningHeaderValueTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/WarningHeaderValueTest.cs @@ -27,13 +27,13 @@ public void Ctor_3ParamsOverload_AllFieldsInitializedCorrectly() Assert.Throws(() => { new WarningHeaderValue(-1, "host", "\"\""); }); Assert.Throws(() => { new WarningHeaderValue(1000, "host", "\"\""); }); - AssertExtensions.Throws("agent", () => { new WarningHeaderValue(100, null, "\"\""); }); + AssertExtensions.Throws("agent", () => { new WarningHeaderValue(100, null, "\"\""); }); AssertExtensions.Throws("agent", () => { new WarningHeaderValue(100, "", "\"\""); }); Assert.Throws(() => { new WarningHeaderValue(100, "x y", "\"\""); }); Assert.Throws(() => { new WarningHeaderValue(100, "x ", "\"\""); }); Assert.Throws(() => { new WarningHeaderValue(100, " x", "\"\""); }); - AssertExtensions.Throws("agent", () => { new WarningHeaderValue(100, null, "\"\""); }); + AssertExtensions.Throws("agent", () => { new WarningHeaderValue(100, null, "\"\""); }); AssertExtensions.Throws("agent", () => { new WarningHeaderValue(100, "", "\"\""); }); Assert.Throws(() => { new WarningHeaderValue(100, "h", "x"); }); Assert.Throws(() => { new WarningHeaderValue(100, "h", "\"x"); }); @@ -52,13 +52,13 @@ public void Ctor_4ParamsOverload_AllFieldsInitializedCorrectly() Assert.Throws(() => { new WarningHeaderValue(-1, "host", "\"\""); }); Assert.Throws(() => { new WarningHeaderValue(1000, "host", "\"\""); }); - AssertExtensions.Throws("agent", () => { new WarningHeaderValue(100, null, "\"\""); }); + AssertExtensions.Throws("agent", () => { new WarningHeaderValue(100, null, "\"\""); }); AssertExtensions.Throws("agent", () => { new WarningHeaderValue(100, "", "\"\""); }); Assert.Throws(() => { new WarningHeaderValue(100, "[::1]:80(x)", "\"\""); }); Assert.Throws(() => { new WarningHeaderValue(100, "host::80", "\"\""); }); Assert.Throws(() => { new WarningHeaderValue(100, "192.168.0.1=", "\"\""); }); - AssertExtensions.Throws("agent", () => { new WarningHeaderValue(100, null, "\"\""); }); + AssertExtensions.Throws("agent", () => { new WarningHeaderValue(100, null, "\"\""); }); AssertExtensions.Throws("agent", () => { new WarningHeaderValue(100, "", "\"\""); }); Assert.Throws(() => { new WarningHeaderValue(100, "h", "(x)"); }); Assert.Throws(() => { new WarningHeaderValue(100, "h", "\"x\"y"); }); diff --git a/src/libraries/System.Net.HttpListener/src/Resources/Strings.resx b/src/libraries/System.Net.HttpListener/src/Resources/Strings.resx index 6bd8e2b8aa767d..9e7e8bfa17c853 100644 --- a/src/libraries/System.Net.HttpListener/src/Resources/Strings.resx +++ b/src/libraries/System.Net.HttpListener/src/Resources/Strings.resx @@ -302,9 +302,6 @@ The {0} operation was called on an incoming request with WebSocket version '{1}', expected '{2}'. - - Empty string is not a valid subprotocol value. Please use \"null\" to specify no value. - The WebSocket protocol '{0}' is invalid because it contains the invalid character '{1}'. diff --git a/src/libraries/System.Net.WebSockets.Client/src/Resources/Strings.resx b/src/libraries/System.Net.WebSockets.Client/src/Resources/Strings.resx index e3d5079aafc0bc..81ae9dc7e21e47 100644 --- a/src/libraries/System.Net.WebSockets.Client/src/Resources/Strings.resx +++ b/src/libraries/System.Net.WebSockets.Client/src/Resources/Strings.resx @@ -93,9 +93,6 @@ The WebSocket protocol '{0}' is invalid because it contains the invalid character '{1}'. - - Empty string is not a valid subprotocol value. Please use \"null\" to specify no value. - The close status description '{0}' is invalid. When using close status code '{1}' the description must be null. diff --git a/src/libraries/System.Net.WebSockets/src/Resources/Strings.resx b/src/libraries/System.Net.WebSockets/src/Resources/Strings.resx index 24f8e00b44f217..1b7857f0021605 100644 --- a/src/libraries/System.Net.WebSockets/src/Resources/Strings.resx +++ b/src/libraries/System.Net.WebSockets/src/Resources/Strings.resx @@ -90,9 +90,6 @@ The argument must be a value greater than {0}. - - Empty string is not a valid subprotocol value. Please use \"null\" to specify no value. - The WebSocket protocol '{0}' is invalid because it contains the invalid character '{1}'. diff --git a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx index da4412098aa6fe..9244fede0601e1 100644 --- a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx +++ b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx @@ -3869,6 +3869,9 @@ The value cannot be an empty string. + + The value cannot be an empty string or composed entirely of whitespace. + FrameworkName is invalid. diff --git a/src/libraries/System.Private.CoreLib/src/System/ArgumentException.cs b/src/libraries/System.Private.CoreLib/src/System/ArgumentException.cs index e9b41c5af3e8a7..5265a299b62c9b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ArgumentException.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ArgumentException.cs @@ -118,11 +118,31 @@ public static void ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumen } } + /// Throws an exception if is null, empty, or consists only of white-space characters. + /// The string argument to validate. + /// The name of the parameter with which corresponds. + /// is null. + /// is empty or consists only of white-space characters. + public static void ThrowIfNullOrWhiteSpace([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) + { + if (string.IsNullOrWhiteSpace(argument)) + { + ThrowNullOrWhiteSpaceException(argument, paramName); + } + } + [DoesNotReturn] private static void ThrowNullOrEmptyException(string? argument, string? paramName) { ArgumentNullException.ThrowIfNull(argument, paramName); throw new ArgumentException(SR.Argument_EmptyString, paramName); } + + [DoesNotReturn] + private static void ThrowNullOrWhiteSpaceException(string? argument, string? paramName) + { + ArgumentNullException.ThrowIfNull(argument, paramName); + throw new ArgumentException(SR.Argument_EmptyOrWhiteSpaceString, paramName); + } } } diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index f5a8ae284b7229..5362c12fcb659e 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -292,6 +292,7 @@ public ArgumentException(string? message, string? paramName, System.Exception? i [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } public static void ThrowIfNullOrEmpty([System.Diagnostics.CodeAnalysis.NotNullAttribute] string? argument, [System.Runtime.CompilerServices.CallerArgumentExpression("argument")] string? paramName = null) { throw null; } + public static void ThrowIfNullOrWhiteSpace([System.Diagnostics.CodeAnalysis.NotNullAttribute] string? argument, [System.Runtime.CompilerServices.CallerArgumentExpression("argument")] string? paramName = null) { throw null; } } public partial class ArgumentNullException : System.ArgumentException { diff --git a/src/libraries/System.Runtime/tests/System/ArgumentExceptionTests.cs b/src/libraries/System.Runtime/tests/System/ArgumentExceptionTests.cs index d2d9804912e944..4535eb38c68a46 100644 --- a/src/libraries/System.Runtime/tests/System/ArgumentExceptionTests.cs +++ b/src/libraries/System.Runtime/tests/System/ArgumentExceptionTests.cs @@ -87,5 +87,35 @@ public static void ThrowIfNullOrEmpty_UsesArgumentExpression_ParameterNameMatche someString = "abc"; ArgumentException.ThrowIfNullOrEmpty(someString); } + + [Fact] + public static void ThrowIfNullOrWhiteSpace_ThrowsForInvalidInput() + { + AssertExtensions.Throws(null, () => ArgumentException.ThrowIfNullOrWhiteSpace(null, null)); + AssertExtensions.Throws("something", () => ArgumentException.ThrowIfNullOrWhiteSpace(null, "something")); + + AssertExtensions.Throws(null, () => ArgumentException.ThrowIfNullOrWhiteSpace("", null)); + AssertExtensions.Throws("something", () => ArgumentException.ThrowIfNullOrWhiteSpace("", "something")); + + string allWhitespace = "\u0009\u000A\u000B\u000C\u000D\u0020\u0085\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000"; + AssertExtensions.Throws("something", () => ArgumentException.ThrowIfNullOrWhiteSpace(" ", "something")); + AssertExtensions.Throws("something", () => ArgumentException.ThrowIfNullOrWhiteSpace(allWhitespace, "something")); + ArgumentException.ThrowIfNullOrWhiteSpace("a" + allWhitespace, "something"); + ArgumentException.ThrowIfNullOrWhiteSpace(allWhitespace + "a", "something"); + ArgumentException.ThrowIfNullOrWhiteSpace(allWhitespace.Substring(0, 5) + "a" + allWhitespace.Substring(5), "something"); + } + + [Fact] + public static void ThrowIfNullOrWhiteSpace_UsesArgumentExpression_ParameterNameMatches() + { + string someString = null; + AssertExtensions.Throws(nameof(someString), () => ArgumentException.ThrowIfNullOrWhiteSpace(someString)); + + someString = ""; + AssertExtensions.Throws(nameof(someString), () => ArgumentException.ThrowIfNullOrWhiteSpace(someString)); + + someString = " "; + AssertExtensions.Throws(nameof(someString), () => ArgumentException.ThrowIfNullOrWhiteSpace(someString)); + } } }