diff --git a/src/HttpClientToCurl/Builder/Concrete/Common/Constants.cs b/src/HttpClientToCurl/Builder/Concrete/Common/Constants.cs new file mode 100644 index 0000000..c2c17d2 --- /dev/null +++ b/src/HttpClientToCurl/Builder/Concrete/Common/Constants.cs @@ -0,0 +1,7 @@ +namespace HttpClientToCurl.Builder.Concrete.Common; + +public static class Constants +{ + internal const string ContentLength = "Content-Length"; + internal const string FormUrlEncodedContentType = "application/x-www-form-urlencoded"; +} diff --git a/src/HttpClientToCurl/Builder/Concrete/Common/Extensions.cs b/src/HttpClientToCurl/Builder/Concrete/Common/Extensions.cs index 8aa0f28..cf286c7 100644 --- a/src/HttpClientToCurl/Builder/Concrete/Common/Extensions.cs +++ b/src/HttpClientToCurl/Builder/Concrete/Common/Extensions.cs @@ -1,4 +1,3 @@ -using System.Net; using System.Text; using System.Web; using HttpClientToCurl.Utility; @@ -129,7 +128,7 @@ internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpC if (needAddDefaultHeaders && httpClient.DefaultRequestHeaders.Any()) { var defaultHeaders = - httpClient.DefaultRequestHeaders.Where(dh => dh.Key != HttpRequestHeader.ContentLength.ToString()); + httpClient.DefaultRequestHeaders.Where(dh => dh.Key != Constants.ContentLength); foreach (var header in defaultHeaders) { stringBuilder @@ -144,7 +143,7 @@ internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpC if (httpRequestMessage.Headers.Any()) { - var headers = httpRequestMessage.Headers.Where(h => h.Key != HttpRequestHeader.ContentLength.ToString()); + var headers = httpRequestMessage.Headers.Where(h => h.Key != Constants.ContentLength); foreach (var header in headers) { stringBuilder @@ -159,8 +158,7 @@ internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpC if (httpRequestMessage.Content is not null && httpRequestMessage.Content.Headers.Any()) { - foreach (var header in httpRequestMessage.Content.Headers.Where(h => - h.Key != HttpRequestHeader.ContentLength.ToString())) + foreach (var header in httpRequestMessage.Content.Headers.Where(h => h.Key != Constants.ContentLength)) { stringBuilder .Append("-H") @@ -185,7 +183,7 @@ internal static StringBuilder AddBody(this StringBuilder stringBuilder, HttpCont string contentType = content?.Headers?.ContentType?.MediaType; string body = content?.ReadAsStringAsync().GetAwaiter().GetResult(); - if (contentType == "application/x-www-form-urlencoded") + if (contentType == Constants.FormUrlEncodedContentType) { stringBuilder.AddFormUrlEncodedContentBody(body); } diff --git a/src/HttpClientToCurl/HttpClientToCurl.csproj b/src/HttpClientToCurl/HttpClientToCurl.csproj index 35120ab..b999219 100644 --- a/src/HttpClientToCurl/HttpClientToCurl.csproj +++ b/src/HttpClientToCurl/HttpClientToCurl.csproj @@ -2,8 +2,8 @@ Amin Golmahalleh - 2.0.5 - 2.0.5 + 2.0.6 + 2.0.6 netstandard2.1 enable disable diff --git a/tests/HttpClientToCurlGeneratorTest/UnitTest/MediaTypes/Json/SuccessCurlGeneratorTests.cs b/tests/HttpClientToCurlGeneratorTest/UnitTest/MediaTypes/Json/SuccessCurlGeneratorTests.cs index e984291..67fada3 100644 --- a/tests/HttpClientToCurlGeneratorTest/UnitTest/MediaTypes/Json/SuccessCurlGeneratorTests.cs +++ b/tests/HttpClientToCurlGeneratorTest/UnitTest/MediaTypes/Json/SuccessCurlGeneratorTests.cs @@ -45,7 +45,7 @@ public void GenerateCurl_With_ContentLength_For_PostMethod() var requestUri = "api/test"; var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json) }; httpRequestMessage.Headers.Add("Authorization", "Bearer f69406a4-6b62-4734-a8dc-158f0fc308ab"); - httpRequestMessage.Headers.Add("ContentLength", "123"); + httpRequestMessage.Content.Headers.Add("Content-Length", "123"); using var httpClient = new HttpClient(); httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");