Skip to content

Commit

Permalink
Merge pull request #30 from FrendsPlatform/issue-29
Browse files Browse the repository at this point in the history
HTTP.RequestBytes - Added ExcludeFromCodeCoverage to Extensions.cs
  • Loading branch information
Svenskapojkarna authored Jan 19, 2024
2 parents 563c29d + 1463b55 commit aa7ed7b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
5 changes: 5 additions & 0 deletions Frends.HTTP.RequestBytes/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);

Expand Down Expand Up @@ -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}"));
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net.Http;
Expand All @@ -9,6 +10,7 @@

namespace Frends.HTTP.RequestBytes;

[ExcludeFromCodeCoverage]
internal static class Extensions
{
internal static void SetHandlerSettingsBasedOnOptions(this HttpClientHandler handler, Options options)
Expand Down Expand Up @@ -124,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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,14 @@ private static IDictionary<string, string> GetHeaderDictionary(Header[] headers,

private static HttpContent GetContent(Input input, IDictionary<string, string> 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)
Expand Down

0 comments on commit aa7ed7b

Please sign in to comment.