From 98aeae32080b55f96ef0862ca8d8c7a326307455 Mon Sep 17 00:00:00 2001 From: Todd Date: Tue, 19 Sep 2023 16:35:45 -0500 Subject: [PATCH] #740 bug: extra space in header values --- Test/Flurl.Test/Http/PostTests.cs | 10 ++++++++++ src/Flurl.Http/FlurlClient.cs | 13 +++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Test/Flurl.Test/Http/PostTests.cs b/Test/Flurl.Test/Http/PostTests.cs index 235ada79..491417e6 100644 --- a/Test/Flurl.Test/Http/PostTests.cs +++ b/Test/Flurl.Test/Http/PostTests.cs @@ -1,4 +1,5 @@ using System.Net.Http; +using System.Net.Http.Headers; using System.Threading.Tasks; using Flurl.Http; using NUnit.Framework; @@ -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; } diff --git a/src/Flurl.Http/FlurlClient.cs b/src/Flurl.Http/FlurlClient.cs index 214fa39c..f2538582 100644 --- a/src/Flurl.Http/FlurlClient.cs +++ b/src/Flurl.Http/FlurlClient.cs @@ -147,16 +147,21 @@ public async Task 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)); } }