From d9b7cd7db5c7302cdc3e281cdac448aae997e95f Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Wed, 17 Jan 2024 08:20:03 +0200 Subject: [PATCH 1/3] Added ExcludeFromCodeCoverage to Extensions.cs --- Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Extensions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Extensions.cs b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Extensions.cs index 94ca89d..dd4d46d 100644 --- a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Extensions.cs +++ b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Extensions.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Net.Http; @@ -9,6 +10,7 @@ namespace Frends.HTTP.RequestBytes; +[ExcludeFromCodeCoverage] internal static class Extensions { internal static void SetHandlerSettingsBasedOnOptions(this HttpClientHandler handler, Options options) From 07320849a1dd75a812d792e7fcade6c531328c49 Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Wed, 17 Jan 2024 08:27:09 +0200 Subject: [PATCH 2/3] CodeQL and Lint fixes --- .../UnitTests.cs | 18 ++++++++++-------- .../Frends.HTTP.RequestBytes/Extensions.cs | 2 +- .../Frends.HTTP.RequestBytes/RequestBytes.cs | 11 ++++------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.Tests/UnitTests.cs b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.Tests/UnitTests.cs index 36f8fe3..295f547 100644 --- a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.Tests/UnitTests.cs +++ b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.Tests/UnitTests.cs @@ -94,7 +94,7 @@ public async Task HttpRequestBytesReturnShoulReturnEmpty() _mockHttpMessageHandler.When(input.Url) .Respond("application/octet-stream", String.Empty); - var result = (dynamic) await HTTP.RequestBytes(input, options, CancellationToken.None); + var result = (dynamic)await HTTP.RequestBytes(input, options, CancellationToken.None); Assert.AreEqual(0, result.BodySizeInMegaBytes); Assert.IsEmpty(result.Body); } @@ -109,8 +109,10 @@ public async Task HttpRequestBytesShouldBeAbleToReturnBinary() var options = new Options { ConnectionTimeoutSeconds = 60 }; var actualFileBytes = File.ReadAllBytes(localTestFilePath); + + using var ms = new MemoryStream(actualFileBytes); _mockHttpMessageHandler.When(input.Url) - .Respond("image/png", new MemoryStream(actualFileBytes)); + .Respond("image/png", ms); var result = (dynamic)await HTTP.RequestBytes(input, options, CancellationToken.None); @@ -200,8 +202,8 @@ public async Task RequestShouldAddBasicAuthHeaders() ConnectionTimeoutSeconds = 60, ThrowExceptionOnErrorResponse = true, Authentication = Authentication.Basic, - Username = "Foo", - Password = "Bar" + Username = Guid.NewGuid().ToString(), + Password = Guid.NewGuid().ToString() }; var sentAuthValue = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes($"{options.Username}:{options.Password}")); @@ -236,7 +238,7 @@ public async Task RequestShouldAddOAuthBearerHeader() _mockHttpMessageHandler.Expect($"{_basePath}/endpoint").WithHeaders("Authorization", "Bearer fooToken") .Respond("application/octet-stream", "FooBar"); - var result = (dynamic) await HTTP.RequestBytes(input, options, CancellationToken.None); + var result = (dynamic)await HTTP.RequestBytes(input, options, CancellationToken.None); _mockHttpMessageHandler.VerifyNoOutstandingExpectation(); Assert.AreEqual(expectedReturn, result.Body); @@ -263,7 +265,7 @@ public async Task AuthorizationHeaderShouldOverrideOption() _mockHttpMessageHandler.Expect($"{_basePath}/endpoint").WithHeaders("Authorization", "Basic fooToken") .Respond("application/octet-stream", "FooBar"); - var result = (dynamic) await HTTP.RequestBytes(input, options, CancellationToken.None); + var result = (dynamic)await HTTP.RequestBytes(input, options, CancellationToken.None); _mockHttpMessageHandler.VerifyNoOutstandingExpectation(); Assert.AreEqual(expectedReturn, result.Body); @@ -286,7 +288,7 @@ public async Task PatchShouldComeThrough() _mockHttpMessageHandler.Expect(new HttpMethod("PATCH"), input.Url).WithContent(message) .Respond("application/octet-stream", message); - var result = (dynamic) await HTTP.RequestBytes(input, options, CancellationToken.None); + var result = (dynamic)await HTTP.RequestBytes(input, options, CancellationToken.None); _mockHttpMessageHandler.VerifyNoOutstandingExpectation(); Assert.AreEqual(message, Encoding.UTF8.GetString(result.Body)); @@ -312,7 +314,7 @@ public async Task RequestShouldSetEncodingWithContentTypeCharsetIgnoringCase() _mockHttpMessageHandler.Expect(HttpMethod.Post, input.Url).WithHeaders("cONTENT-tYpE", expectedContentType).WithContent(requestMessage) .Respond("application/octet-stream", "foo åäö"); - var result = (dynamic) await HTTP.RequestBytes(input, options, CancellationToken.None); + var result = (dynamic)await HTTP.RequestBytes(input, options, CancellationToken.None); _mockHttpMessageHandler.VerifyNoOutstandingExpectation(); Assert.AreEqual("foo åäö", Encoding.UTF8.GetString(result.Body)); diff --git a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Extensions.cs b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Extensions.cs index dd4d46d..d73fc0b 100644 --- a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Extensions.cs +++ b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/Extensions.cs @@ -126,7 +126,7 @@ private static X509Certificate2[] GetCertificatesFromStore(string thumbprint, return new[] { certificate }; } - var chain = new X509Chain(); + using var chain = new X509Chain(); chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; chain.Build(certificate); diff --git a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/RequestBytes.cs b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/RequestBytes.cs index 08984fd..a8c9432 100644 --- a/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/RequestBytes.cs +++ b/Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes/RequestBytes.cs @@ -107,17 +107,14 @@ private static IDictionary GetHeaderDictionary(Header[] headers, private static HttpContent GetContent(Input input, IDictionary headers) { - //Check if Content-Type exists and is set and valid - var contentTypeIsSetAndValid = false; - MediaTypeWithQualityHeaderValue validContentType = null; if (headers.TryGetValue("content-type", out string contentTypeValue)) { - contentTypeIsSetAndValid = MediaTypeWithQualityHeaderValue.TryParse(contentTypeValue, out validContentType); + var contentTypeIsSetAndValid = MediaTypeWithQualityHeaderValue.TryParse(contentTypeValue, out var validContentType); + if (contentTypeIsSetAndValid) + return new StringContent(input.Message ?? string.Empty, Encoding.GetEncoding(validContentType.CharSet ?? Encoding.UTF8.WebName)); } - return contentTypeIsSetAndValid - ? new StringContent(input.Message ?? "", Encoding.GetEncoding(validContentType.CharSet ?? Encoding.UTF8.WebName)) - : new StringContent(input.Message ?? ""); + return new StringContent(input.Message ?? string.Empty); } private static HttpClient GetHttpClientForOptions(Options options) From 1463b55195b5705494a8cf89c0fa6a6a1892e9cc Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Thu, 18 Jan 2024 08:01:21 +0200 Subject: [PATCH 3/3] Changes --- Frends.HTTP.RequestBytes/CHANGELOG.md | 5 +++++ .../Frends.HTTP.RequestBytes/Frends.HTTP.RequestBytes.csproj | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Frends.HTTP.RequestBytes/CHANGELOG.md b/Frends.HTTP.RequestBytes/CHANGELOG.md index bdd9318..a401a7b 100644 --- a/Frends.HTTP.RequestBytes/CHANGELOG.md +++ b/Frends.HTTP.RequestBytes/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [1.0.1] - 2024-01-17 +### Fixed +- Fixed issues which CodeQL found in the codebase. + Fixed Code Coverage + ## [1.0.0] - 2023-01-27 ### Added - Initial implementation of Frends.HTTP.Request. 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 7ac738a..a98a2fb 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.0 + 1.0.1 Frends Frends Frends