Skip to content

Commit

Permalink
#740 bug: extra space in header values
Browse files Browse the repository at this point in the history
  • Loading branch information
Todd committed Sep 19, 2023
1 parent 5a9f2d6 commit 98aeae3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Test/Flurl.Test/Http/PostTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Flurl.Http;
using NUnit.Framework;
Expand Down Expand Up @@ -75,6 +76,15 @@ public async Task can_receive_string() {
Assert.AreEqual("good job", data);
}

[Test] // #740
public async Task doesnt_add_space_in_content_type_header() {
var req = new FlurlRequest("https://fake.com");
await req.WithHeader("Content-Type", "application/octet-stream;some=b").PostStringAsync("hello");

Assert.IsTrue(req.Headers.TryGetFirst("Content-Type", out var val));
Assert.AreEqual("application/octet-stream;some=b", val);
}

private class TestData
{
public int id { get; set; }
Expand Down
13 changes: 9 additions & 4 deletions src/Flurl.Http/FlurlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,21 @@ public async Task<IFlurlResponse> SendAsync(IFlurlRequest request, HttpCompletio

private void SyncHeaders(IFlurlRequest req, HttpRequestMessage reqMsg) {
// copy any client-level (default) headers to FlurlRequest
foreach (var header in this.Headers.Where(h => !req.Headers.Contains(h.Name)).ToList())
req.Headers.Add(header.Name, header.Value);
foreach (var header in this.Headers.ToList()) {
if (!req.Headers.Contains(header.Name))
req.Headers.Add(header.Name, header.Value);
}

// copy headers from FlurlRequest to HttpRequestMessage
foreach (var header in req.Headers)
reqMsg.SetHeader(header.Name, header.Value.Trim(), false);

if (reqMsg.Content == null)
return;

// copy headers from HttpContent to FlurlRequest
if (reqMsg.Content != null) {
foreach (var header in reqMsg.Content.Headers)
foreach (var header in reqMsg.Content.Headers.ToList()) {
if (!req.Headers.Contains(header.Key))
req.Headers.AddOrReplace(header.Key, string.Join(",", header.Value));
}
}
Expand Down

0 comments on commit 98aeae3

Please sign in to comment.