Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content-Type header fixes to HTTP.RequestBytes #34

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Frends.HTTP.RequestBytes/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Changelog

## [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.

## [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.
- Initial implementation of Frends.HTTP.RequestBytes.
Original file line number Diff line number Diff line change
Expand Up @@ -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'";

Expand Down

This file was deleted.

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

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<Version>1.0.1</Version>
<Version>1.1.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,9 @@ private static async Task<HttpResponseMessage> 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
})
{

Expand All @@ -165,7 +162,7 @@ private static async Task<HttpResponseMessage> 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
Expand Down
Loading