From da28015009f646f75d7ead0f30b3be9180a4b449 Mon Sep 17 00:00:00 2001 From: Janne Pennanen Date: Mon, 19 Aug 2024 13:30:03 +0300 Subject: [PATCH 1/7] Removed isContentAllowed check, HttpClient seems to no longer fail if a GET has content for example --- .../Definitions/SendMethod.cs | 24 ------------------- .../Frends.HTTP.RequestBytes/RequestBytes.cs | 5 +--- 2 files changed, 1 insertion(+), 28 deletions(-) delete mode 100644 Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Definitions/SendMethod.cs diff --git a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Definitions/SendMethod.cs b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Definitions/SendMethod.cs deleted file mode 100644 index bfb2046..0000000 --- a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Definitions/SendMethod.cs +++ /dev/null @@ -1,24 +0,0 @@ -namespace Frends.HTTP.RequestBytes.Definitions; - -/// -/// Allowed methods for sending content -/// -public enum SendMethod -{ - /// - /// POST Request. - /// - POST, - /// - /// PUT Request. - /// - PUT, - /// - /// PATCH Request. - /// - PATCH, - /// - /// DELETE Request. - /// - DELETE -} diff --git a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/RequestBytes.cs b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/RequestBytes.cs index a8c9432..e31e7f7 100644 --- a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/RequestBytes.cs +++ b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/RequestBytes.cs @@ -151,12 +151,9 @@ private static async Task GetHttpRequestResponseAsync( { cancellationToken.ThrowIfCancellationRequested(); - // Only POST, PUT, PATCH and DELETE can have content, otherwise the HttpClient will fail - var isContentAllowed = Enum.TryParse(method, ignoreCase: true, result: out SendMethod _); - using (var request = new HttpRequestMessage(new HttpMethod(method), new Uri(url)) { - Content = isContentAllowed ? content : null, + Content = content }) { From 5761ec316634292d1f1db215a9545208b4486bc5 Mon Sep 17 00:00:00 2001 From: Janne Pennanen Date: Mon, 19 Aug 2024 13:31:59 +0300 Subject: [PATCH 2/7] Added test case for GET request with Content-Type: text/plain --- .../UnitTests.cs | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.Tests/UnitTests.cs b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.Tests/UnitTests.cs index 295f547..56c2911 100644 --- a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.Tests/UnitTests.cs +++ b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.Tests/UnitTests.cs @@ -143,7 +143,29 @@ public async Task RequestTestGetWithParameters() } [TestMethod] - public void RequestShuldThrowExceptionIfOptionIsSet() + public async Task RequestTestGetWithContent() + { + var expectedReturn = Encoding.ASCII.GetBytes("OK"); + + + var contentType = new Header { Name = "Content-Type", Value = "text/plain" }; + var input = GetInputParams( + url: "http://localhost:9191/endpoint", + method: Method.GET, + headers: new Header [1] { contentType }, + message: "test" + ); + var options = new Options { ConnectionTimeoutSeconds = 60 }; + + _mockHttpMessageHandler.When(input.Url).WithHeaders("Content-Type", "text/plain").WithPartialContent("test") + .Respond("application/octet-stream", "OK"); + + var result = (dynamic) await HTTP.RequestBytes(input, options, CancellationToken.None); + Assert.AreEqual(expectedReturn, result.Body); + } + + [TestMethod] + public void RequestShouldThrowExceptionIfOptionIsSet() { const string expectedReturn = @"'FooBar'"; From 0010f56d15b4fe97756f2203d5c587463a4e5c91 Mon Sep 17 00:00:00 2001 From: Janne Pennanen Date: Mon, 19 Aug 2024 13:32:19 +0300 Subject: [PATCH 3/7] Changelog entry and version bump --- Frends.HTTP.RequestBytes/CHANGELOG.md | 4 ++++ .../Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.csproj | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Frends.HTTP.RequestBytes/CHANGELOG.md b/Frends.HTTP.RequestBytes/CHANGELOG.md index a401a7b..f456af5 100644 --- a/Frends.HTTP.RequestBytes/CHANGELOG.md +++ b/Frends.HTTP.RequestBytes/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [1.0.2] - 2024-08-19 +### Changed +- Removed handling where only PATCH, PUT, POST and DELETE requests were allowed to have the Content-Type header and content, due to HttpClient failing if e.g., a GET request had a Content-Type header. HttpClient has since been updated to tolerate such requests. + ## [1.0.1] - 2024-01-17 ### Fixed - Fixed issues which CodeQL found in the codebase. diff --git a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.csproj b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.csproj index a98a2fb..9c68170 100644 --- a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.csproj +++ b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.csproj @@ -2,7 +2,7 @@ net6.0 - 1.0.1 + 1.0.2 Frends Frends Frends From 01f73bcb8a8cfc07e8fe6fe22bb7a2ee0d6060de Mon Sep 17 00:00:00 2001 From: Janne Pennanen Date: Tue, 20 Aug 2024 10:15:46 +0300 Subject: [PATCH 4/7] Add content headers even if there is no content --- .../Frends.HTTP.RequestBytes/RequestBytes.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/RequestBytes.cs b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/RequestBytes.cs index e31e7f7..758040a 100644 --- a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/RequestBytes.cs +++ b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/RequestBytes.cs @@ -162,7 +162,7 @@ private static async Task GetHttpRequestResponseAsync( foreach (var header in headers) { var requestHeaderAddedSuccessfully = request.Headers.TryAddWithoutValidation(header.Key, header.Value); - if (!requestHeaderAddedSuccessfully && request.Content != null) + if (!requestHeaderAddedSuccessfully) { //Could not add to request headers try to add to content headers // this check is probably not needed anymore as the new HttpClient does not seem fail on malformed headers From d9b7152bbc858cbb6b2984d9ab7720c2d39c975d Mon Sep 17 00:00:00 2001 From: Janne Pennanen Date: Tue, 20 Aug 2024 10:25:44 +0300 Subject: [PATCH 5/7] Adjusted changelog --- Frends.HTTP.RequestBytes/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Frends.HTTP.RequestBytes/CHANGELOG.md b/Frends.HTTP.RequestBytes/CHANGELOG.md index f456af5..5af7b4e 100644 --- a/Frends.HTTP.RequestBytes/CHANGELOG.md +++ b/Frends.HTTP.RequestBytes/CHANGELOG.md @@ -2,7 +2,7 @@ ## [1.0.2] - 2024-08-19 ### Changed -- Removed handling where only PATCH, PUT, POST and DELETE requests were allowed to have the Content-Type header and content, due to HttpClient failing if e.g., a GET request had a Content-Type header. HttpClient has since been updated to tolerate such requests. +- Removed handling where only PATCH, PUT, POST and DELETE requests were allowed to have the Content-Type header and content, due to HttpClient failing if e.g., a GET request had content. HttpClient has since been updated to tolerate such requests. ## [1.0.1] - 2024-01-17 ### Fixed @@ -11,4 +11,4 @@ ## [1.0.0] - 2023-01-27 ### Added -- Initial implementation of Frends.HTTP.Request. +- Initial implementation of Frends.HTTP.RequestBytes. From 6a2ceb934dab633a41aa48f1b0fec41b43b8f5c1 Mon Sep 17 00:00:00 2001 From: Janne Pennanen Date: Tue, 20 Aug 2024 10:41:15 +0300 Subject: [PATCH 6/7] Whitespace formatting fixes --- .../Frends.HTTP.RequestBytes.Tests/UnitTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.Tests/UnitTests.cs b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.Tests/UnitTests.cs index 56c2911..1bbff33 100644 --- a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.Tests/UnitTests.cs +++ b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.Tests/UnitTests.cs @@ -152,7 +152,7 @@ public async Task RequestTestGetWithContent() var input = GetInputParams( url: "http://localhost:9191/endpoint", method: Method.GET, - headers: new Header [1] { contentType }, + headers: new Header[1] { contentType }, message: "test" ); var options = new Options { ConnectionTimeoutSeconds = 60 }; @@ -160,7 +160,7 @@ public async Task RequestTestGetWithContent() _mockHttpMessageHandler.When(input.Url).WithHeaders("Content-Type", "text/plain").WithPartialContent("test") .Respond("application/octet-stream", "OK"); - var result = (dynamic) await HTTP.RequestBytes(input, options, CancellationToken.None); + var result = (dynamic)await HTTP.RequestBytes(input, options, CancellationToken.None); Assert.AreEqual(expectedReturn, result.Body); } From a71bc8d0bbae509a10e29fcc04329572fce03531 Mon Sep 17 00:00:00 2001 From: Janne Pennanen Date: Tue, 20 Aug 2024 15:09:22 +0300 Subject: [PATCH 7/7] Bump to v1.1.0 instead of v1.0.2 --- Frends.HTTP.RequestBytes/CHANGELOG.md | 2 +- .../Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Frends.HTTP.RequestBytes/CHANGELOG.md b/Frends.HTTP.RequestBytes/CHANGELOG.md index 5af7b4e..7236035 100644 --- a/Frends.HTTP.RequestBytes/CHANGELOG.md +++ b/Frends.HTTP.RequestBytes/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## [1.0.2] - 2024-08-19 +## [1.1.0] - 2024-08-19 ### Changed - Removed handling where only PATCH, PUT, POST and DELETE requests were allowed to have the Content-Type header and content, due to HttpClient failing if e.g., a GET request had content. HttpClient has since been updated to tolerate such requests. diff --git a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.csproj b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.csproj index 9c68170..5c27912 100644 --- a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.csproj +++ b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.csproj @@ -2,7 +2,7 @@ net6.0 - 1.0.2 + 1.1.0 Frends Frends Frends