diff --git a/Directory.Build.props b/Directory.Build.props index 0135e4cc..b9fa8de1 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -43,10 +43,10 @@ - + - + \ No newline at end of file diff --git a/docs/CodeDoc/Atc/IndexExtended.md b/docs/CodeDoc/Atc/IndexExtended.md index 4f02076a..e439efa4 100644 --- a/docs/CodeDoc/Atc/IndexExtended.md +++ b/docs/CodeDoc/Atc/IndexExtended.md @@ -5193,6 +5193,7 @@ - TryParseDate(this string value, out DateTime dateTime) - TryParseDate(this string value, out DateTime dateTime, CultureInfo cultureInfo, DateTimeStyles dateTimeStyles = None) - TryParseDateFromIso8601(this string value, out DateTime dateTime) + - TryParseToHttpStatusCode(this string value, out HttpStatusCode httpStatusCode) - WordCount(this string value) - XmlDecode(this string xml) - XmlEncode(this string xml) @@ -5389,6 +5390,7 @@ - IsServerError(this HttpStatusCode httpStatusCode) - IsSuccessful(this HttpStatusCode httpStatusCode) - ToNormalizedString(this HttpStatusCode httpStatusCode) + - ToStatusCodesConstant(this HttpStatusCode httpStatusCode) ## [System.Reflection](System.Reflection.md) diff --git a/docs/CodeDoc/Atc/System.Net.md b/docs/CodeDoc/Atc/System.Net.md index b13e0b5d..9b9ff6d9 100644 --- a/docs/CodeDoc/Atc/System.Net.md +++ b/docs/CodeDoc/Atc/System.Net.md @@ -43,4 +43,8 @@ >```csharp >string ToNormalizedString(this HttpStatusCode httpStatusCode) >``` +#### ToStatusCodesConstant +>```csharp +>string ToStatusCodesConstant(this HttpStatusCode httpStatusCode) +>```
Generated by MarkdownCodeDoc version 1.2
diff --git a/docs/CodeDoc/Atc/System.md b/docs/CodeDoc/Atc/System.md index 4d04d9d7..eb31f87f 100644 --- a/docs/CodeDoc/Atc/System.md +++ b/docs/CodeDoc/Atc/System.md @@ -2393,6 +2393,10 @@ Extensions for the string class. >Parameters:
>     `value`  -  The value.
>     `dateTime`  -  The date time.
+#### TryParseToHttpStatusCode +>```csharp +>bool TryParseToHttpStatusCode(this string value, out HttpStatusCode httpStatusCode) +>``` #### WordCount >```csharp >int WordCount(this string value) diff --git a/sample/Demo.Atc.Console.Spectre.Cli/Demo.Atc.Console.Spectre.Cli.csproj b/sample/Demo.Atc.Console.Spectre.Cli/Demo.Atc.Console.Spectre.Cli.csproj index 7cd9df50..9fe9c1fb 100644 --- a/sample/Demo.Atc.Console.Spectre.Cli/Demo.Atc.Console.Spectre.Cli.csproj +++ b/sample/Demo.Atc.Console.Spectre.Cli/Demo.Atc.Console.Spectre.Cli.csproj @@ -11,8 +11,8 @@ - - + + diff --git a/sample/Demo.Atc.Dotnet.Cli/Demo.Atc.Dotnet.Cli.csproj b/sample/Demo.Atc.Dotnet.Cli/Demo.Atc.Dotnet.Cli.csproj index 41621ca2..e9048650 100644 --- a/sample/Demo.Atc.Dotnet.Cli/Demo.Atc.Dotnet.Cli.csproj +++ b/sample/Demo.Atc.Dotnet.Cli/Demo.Atc.Dotnet.Cli.csproj @@ -11,8 +11,8 @@ - - + + diff --git a/src/Atc.Console.Spectre/Atc.Console.Spectre.csproj b/src/Atc.Console.Spectre/Atc.Console.Spectre.csproj index 6ce3ac0d..47d2d590 100644 --- a/src/Atc.Console.Spectre/Atc.Console.Spectre.csproj +++ b/src/Atc.Console.Spectre/Atc.Console.Spectre.csproj @@ -9,8 +9,8 @@ - - + + diff --git a/src/Atc.Rest.Extended/Atc.Rest.Extended.csproj b/src/Atc.Rest.Extended/Atc.Rest.Extended.csproj index 7f4f79cf..98e9386a 100644 --- a/src/Atc.Rest.Extended/Atc.Rest.Extended.csproj +++ b/src/Atc.Rest.Extended/Atc.Rest.Extended.csproj @@ -11,8 +11,8 @@ - - + + diff --git a/src/Atc.Rest.HealthChecks/Atc.Rest.HealthChecks.csproj b/src/Atc.Rest.HealthChecks/Atc.Rest.HealthChecks.csproj index 6ab545dc..58147c95 100644 --- a/src/Atc.Rest.HealthChecks/Atc.Rest.HealthChecks.csproj +++ b/src/Atc.Rest.HealthChecks/Atc.Rest.HealthChecks.csproj @@ -9,7 +9,7 @@ - + diff --git a/src/Atc.XUnit/Atc.XUnit.csproj b/src/Atc.XUnit/Atc.XUnit.csproj index a4a1758b..585103ea 100644 --- a/src/Atc.XUnit/Atc.XUnit.csproj +++ b/src/Atc.XUnit/Atc.XUnit.csproj @@ -12,12 +12,12 @@ - + NU1701 - + diff --git a/src/Atc/Extensions/HttpStatusCodeExtensions.cs b/src/Atc/Extensions/HttpStatusCodeExtensions.cs index 4535cf8e..0b110eb4 100644 --- a/src/Atc/Extensions/HttpStatusCodeExtensions.cs +++ b/src/Atc/Extensions/HttpStatusCodeExtensions.cs @@ -3,15 +3,25 @@ namespace System.Net; public static class HttpStatusCodeExtensions { - public static string ToNormalizedString(this HttpStatusCode httpStatusCode) - { - return httpStatusCode switch + public static string ToNormalizedString( + this HttpStatusCode httpStatusCode) + => httpStatusCode switch { HttpStatusCode.OK => "Ok", HttpStatusCode.IMUsed => "ImUsed", _ => httpStatusCode.ToString(), }; - } + + public static string ToStatusCodesConstant( + this HttpStatusCode httpStatusCode) + => httpStatusCode switch + { + HttpStatusCode.NonAuthoritativeInformation => "Status203NonAuthoritative", + HttpStatusCode.Unused => "Status306SwitchProxy", + HttpStatusCode.RedirectKeepVerb => "Status307TemporaryRedirect", + HttpStatusCode.HttpVersionNotSupported => "Status505HttpVersionNotsupported", + _ => $"Status{(int)httpStatusCode}{httpStatusCode}", + }; public static bool IsInformational(this HttpStatusCode httpStatusCode) => (int)httpStatusCode >= 100 && (int)httpStatusCode < 200; diff --git a/src/Atc/Extensions/StringExtensions.cs b/src/Atc/Extensions/StringExtensions.cs index 3a641b96..faa839ce 100644 --- a/src/Atc/Extensions/StringExtensions.cs +++ b/src/Atc/Extensions/StringExtensions.cs @@ -1774,6 +1774,12 @@ public static Stream ToStream(this string value) return stream; } + public static bool TryParseToHttpStatusCode( + this string value, + out HttpStatusCode httpStatusCode) + => Enum.TryParse(value, false, out httpStatusCode) && + !NumberHelper.IsInt(httpStatusCode.ToString()); + /// /// Gets the value between less and greater than chars if exist. /// diff --git a/test/Atc.CodeAnalysis.CSharp.Tests/Atc.CodeAnalysis.CSharp.Tests.csproj b/test/Atc.CodeAnalysis.CSharp.Tests/Atc.CodeAnalysis.CSharp.Tests.csproj index 9ef4cd1d..1dbc8fd1 100644 --- a/test/Atc.CodeAnalysis.CSharp.Tests/Atc.CodeAnalysis.CSharp.Tests.csproj +++ b/test/Atc.CodeAnalysis.CSharp.Tests/Atc.CodeAnalysis.CSharp.Tests.csproj @@ -6,9 +6,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/Atc.CodeDocumentation.Tests/Atc.CodeDocumentation.Tests.csproj b/test/Atc.CodeDocumentation.Tests/Atc.CodeDocumentation.Tests.csproj index b5f55710..433f3022 100644 --- a/test/Atc.CodeDocumentation.Tests/Atc.CodeDocumentation.Tests.csproj +++ b/test/Atc.CodeDocumentation.Tests/Atc.CodeDocumentation.Tests.csproj @@ -6,9 +6,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/Atc.Console.Spectre.Tests/Atc.Console.Spectre.Tests.csproj b/test/Atc.Console.Spectre.Tests/Atc.Console.Spectre.Tests.csproj index 1dcd3e14..6187122d 100644 --- a/test/Atc.Console.Spectre.Tests/Atc.Console.Spectre.Tests.csproj +++ b/test/Atc.Console.Spectre.Tests/Atc.Console.Spectre.Tests.csproj @@ -6,9 +6,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/Atc.DotNet.Tests/Atc.DotNet.Tests.csproj b/test/Atc.DotNet.Tests/Atc.DotNet.Tests.csproj index bf24dcf3..d944b8a9 100644 --- a/test/Atc.DotNet.Tests/Atc.DotNet.Tests.csproj +++ b/test/Atc.DotNet.Tests/Atc.DotNet.Tests.csproj @@ -12,9 +12,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/Atc.OpenApi.Tests/Atc.OpenApi.Tests.csproj b/test/Atc.OpenApi.Tests/Atc.OpenApi.Tests.csproj index 19417f60..0c18d298 100644 --- a/test/Atc.OpenApi.Tests/Atc.OpenApi.Tests.csproj +++ b/test/Atc.OpenApi.Tests/Atc.OpenApi.Tests.csproj @@ -6,9 +6,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/Atc.Rest.Extended.Tests/Atc.Rest.Extended.Tests.csproj b/test/Atc.Rest.Extended.Tests/Atc.Rest.Extended.Tests.csproj index 9de58a23..f95b5c15 100644 --- a/test/Atc.Rest.Extended.Tests/Atc.Rest.Extended.Tests.csproj +++ b/test/Atc.Rest.Extended.Tests/Atc.Rest.Extended.Tests.csproj @@ -7,9 +7,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/Atc.Rest.FluentAssertions.Tests/Atc.Rest.FluentAssertions.Tests.csproj b/test/Atc.Rest.FluentAssertions.Tests/Atc.Rest.FluentAssertions.Tests.csproj index ccf042bf..8a561cbc 100644 --- a/test/Atc.Rest.FluentAssertions.Tests/Atc.Rest.FluentAssertions.Tests.csproj +++ b/test/Atc.Rest.FluentAssertions.Tests/Atc.Rest.FluentAssertions.Tests.csproj @@ -6,9 +6,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/Atc.Rest.HealthChecks.Tests/Atc.Rest.HealthChecks.Tests.csproj b/test/Atc.Rest.HealthChecks.Tests/Atc.Rest.HealthChecks.Tests.csproj index 12fdce08..619b5706 100644 --- a/test/Atc.Rest.HealthChecks.Tests/Atc.Rest.HealthChecks.Tests.csproj +++ b/test/Atc.Rest.HealthChecks.Tests/Atc.Rest.HealthChecks.Tests.csproj @@ -6,9 +6,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/Atc.Rest.Tests/Atc.Rest.Tests.csproj b/test/Atc.Rest.Tests/Atc.Rest.Tests.csproj index 8cecd56a..ee83badd 100644 --- a/test/Atc.Rest.Tests/Atc.Rest.Tests.csproj +++ b/test/Atc.Rest.Tests/Atc.Rest.Tests.csproj @@ -7,10 +7,10 @@ - + - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/Atc.Tests/Atc.Tests.csproj b/test/Atc.Tests/Atc.Tests.csproj index 22995aad..c92fb9dd 100644 --- a/test/Atc.Tests/Atc.Tests.csproj +++ b/test/Atc.Tests/Atc.Tests.csproj @@ -7,9 +7,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/Atc.Tests/Extensions/HttpStatusCodeExtensionsTests.cs b/test/Atc.Tests/Extensions/HttpStatusCodeExtensionsTests.cs index de3b979f..d42bbac0 100644 --- a/test/Atc.Tests/Extensions/HttpStatusCodeExtensionsTests.cs +++ b/test/Atc.Tests/Extensions/HttpStatusCodeExtensionsTests.cs @@ -9,6 +9,70 @@ public class HttpStatusCodeExtensionsTests public void ToNormalizedString(string expected, HttpStatusCode httpStatusCode) => Assert.Equal(expected, httpStatusCode.ToNormalizedString()); + [Theory] + [InlineData(HttpStatusCode.Continue, "Status100Continue")] + [InlineData(HttpStatusCode.SwitchingProtocols, "Status101SwitchingProtocols")] + [InlineData(HttpStatusCode.Processing, "Status102Processing")] + [InlineData(HttpStatusCode.OK, "Status200OK")] + [InlineData(HttpStatusCode.Created, "Status201Created")] + [InlineData(HttpStatusCode.Accepted, "Status202Accepted")] + [InlineData(HttpStatusCode.NonAuthoritativeInformation, "Status203NonAuthoritative")] + [InlineData(HttpStatusCode.NoContent, "Status204NoContent")] + [InlineData(HttpStatusCode.ResetContent, "Status205ResetContent")] + [InlineData(HttpStatusCode.PartialContent, "Status206PartialContent")] + [InlineData(HttpStatusCode.MultiStatus, "Status207MultiStatus")] + [InlineData(HttpStatusCode.AlreadyReported, "Status208AlreadyReported")] + [InlineData(HttpStatusCode.IMUsed, "Status226IMUsed")] + [InlineData(HttpStatusCode.MultipleChoices, "Status300MultipleChoices")] + [InlineData(HttpStatusCode.MovedPermanently, "Status301MovedPermanently")] + [InlineData(HttpStatusCode.Found, "Status302Found")] + [InlineData(HttpStatusCode.SeeOther, "Status303SeeOther")] + [InlineData(HttpStatusCode.NotModified, "Status304NotModified")] + [InlineData(HttpStatusCode.UseProxy, "Status305UseProxy")] + [InlineData(HttpStatusCode.Unused, "Status306SwitchProxy")] + [InlineData(HttpStatusCode.TemporaryRedirect, "Status307TemporaryRedirect")] + [InlineData(HttpStatusCode.PermanentRedirect, "Status308PermanentRedirect")] + [InlineData(HttpStatusCode.BadRequest, "Status400BadRequest")] + [InlineData(HttpStatusCode.Unauthorized, "Status401Unauthorized")] + [InlineData(HttpStatusCode.PaymentRequired, "Status402PaymentRequired")] + [InlineData(HttpStatusCode.Forbidden, "Status403Forbidden")] + [InlineData(HttpStatusCode.NotFound, "Status404NotFound")] + [InlineData(HttpStatusCode.MethodNotAllowed, "Status405MethodNotAllowed")] + [InlineData(HttpStatusCode.NotAcceptable, "Status406NotAcceptable")] + [InlineData(HttpStatusCode.ProxyAuthenticationRequired, "Status407ProxyAuthenticationRequired")] + [InlineData(HttpStatusCode.RequestTimeout, "Status408RequestTimeout")] + [InlineData(HttpStatusCode.Conflict, "Status409Conflict")] + [InlineData(HttpStatusCode.Gone, "Status410Gone")] + [InlineData(HttpStatusCode.LengthRequired, "Status411LengthRequired")] + [InlineData(HttpStatusCode.PreconditionFailed, "Status412PreconditionFailed")] + [InlineData(HttpStatusCode.RequestEntityTooLarge, "Status413RequestEntityTooLarge")] + [InlineData(HttpStatusCode.RequestUriTooLong, "Status414RequestUriTooLong")] + [InlineData(HttpStatusCode.UnsupportedMediaType, "Status415UnsupportedMediaType")] + [InlineData(HttpStatusCode.RequestedRangeNotSatisfiable, "Status416RequestedRangeNotSatisfiable")] + [InlineData(HttpStatusCode.ExpectationFailed, "Status417ExpectationFailed")] + [InlineData(HttpStatusCode.MisdirectedRequest, "Status421MisdirectedRequest")] + [InlineData(HttpStatusCode.UnprocessableEntity, "Status422UnprocessableEntity")] + [InlineData(HttpStatusCode.Locked, "Status423Locked")] + [InlineData(HttpStatusCode.FailedDependency, "Status424FailedDependency")] + [InlineData(HttpStatusCode.UpgradeRequired, "Status426UpgradeRequired")] + [InlineData(HttpStatusCode.PreconditionRequired, "Status428PreconditionRequired")] + [InlineData(HttpStatusCode.TooManyRequests, "Status429TooManyRequests")] + [InlineData(HttpStatusCode.RequestHeaderFieldsTooLarge, "Status431RequestHeaderFieldsTooLarge")] + [InlineData(HttpStatusCode.UnavailableForLegalReasons, "Status451UnavailableForLegalReasons")] + [InlineData(HttpStatusCode.InternalServerError, "Status500InternalServerError")] + [InlineData(HttpStatusCode.NotImplemented, "Status501NotImplemented")] + [InlineData(HttpStatusCode.BadGateway, "Status502BadGateway")] + [InlineData(HttpStatusCode.ServiceUnavailable, "Status503ServiceUnavailable")] + [InlineData(HttpStatusCode.GatewayTimeout, "Status504GatewayTimeout")] + [InlineData(HttpStatusCode.HttpVersionNotSupported, "Status505HttpVersionNotsupported")] + [InlineData(HttpStatusCode.VariantAlsoNegotiates, "Status506VariantAlsoNegotiates")] + [InlineData(HttpStatusCode.InsufficientStorage, "Status507InsufficientStorage")] + [InlineData(HttpStatusCode.LoopDetected, "Status508LoopDetected")] + [InlineData(HttpStatusCode.NotExtended, "Status510NotExtended")] + [InlineData(HttpStatusCode.NetworkAuthenticationRequired, "Status511NetworkAuthenticationRequired")] + public void ToStatusCodesConstant(HttpStatusCode httpStatusCode, string expected) + => Assert.Equal(expected, httpStatusCode.ToStatusCodesConstant()); + [Theory] [InlineData(true, HttpStatusCode.Continue)] [InlineData(false, HttpStatusCode.OK)] diff --git a/test/Atc.Tests/Extensions/StringExtensionsTests.cs b/test/Atc.Tests/Extensions/StringExtensionsTests.cs index 953bef06..21afb697 100644 --- a/test/Atc.Tests/Extensions/StringExtensionsTests.cs +++ b/test/Atc.Tests/Extensions/StringExtensionsTests.cs @@ -657,4 +657,27 @@ public void ToStreamFromBase64() // Assert Assert.NotNull(actual); } + + [Theory] + [InlineData("OK", true, (int)HttpStatusCode.OK)] + [InlineData("NotFound", true, (int)HttpStatusCode.NotFound)] + [InlineData("BadRequest", true, (int)HttpStatusCode.BadRequest)] + [InlineData("InternalServerError", true, (int)HttpStatusCode.InternalServerError)] + [InlineData("InvalidStatusCode", false, 0)] + [InlineData("", false, 0)] + public void TryParseToHttpStatusCode( + string input, + bool expectedResult, + int expectedStatusCodeAsInt) + { + // Arrange + var expectedStatusCode = (HttpStatusCode)expectedStatusCodeAsInt; + + // Act + var result = input.TryParseToHttpStatusCode(out var httpStatusCode); + + // Assert + Assert.Equal(expectedResult, result); + Assert.Equal(expectedStatusCode, httpStatusCode); + } } \ No newline at end of file diff --git a/test/Atc.XUnit.Tests/Atc.XUnit.Tests.csproj b/test/Atc.XUnit.Tests/Atc.XUnit.Tests.csproj index b5f55710..433f3022 100644 --- a/test/Atc.XUnit.Tests/Atc.XUnit.Tests.csproj +++ b/test/Atc.XUnit.Tests/Atc.XUnit.Tests.csproj @@ -6,9 +6,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive