diff --git a/Flurl.sln b/Flurl.sln index 63ee2597..dd4c22c9 100644 --- a/Flurl.sln +++ b/Flurl.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26730.3 +VisualStudioVersion = 15.0.27130.2036 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Flurl", "src\Flurl\Flurl.csproj", "{117B6C6E-53F9-45AE-9439-F4FB7E21B116}" EndProject diff --git a/Test/Flurl.Test/Http/SettingsExtensionsTests.cs b/Test/Flurl.Test/Http/SettingsExtensionsTests.cs index 3b3b70c9..e1fd1bd5 100644 --- a/Test/Flurl.Test/Http/SettingsExtensionsTests.cs +++ b/Test/Flurl.Test/Http/SettingsExtensionsTests.cs @@ -38,12 +38,22 @@ public void can_set_header() { [Test] public void can_set_headers_from_anon_object() { - var sc = GetSettingsContainer().WithHeaders(new { a = "b", one = 2 }); + // null values shouldn't be added + var sc = GetSettingsContainer().WithHeaders(new { a = "b", one = 2, three = (object)null }); Assert.AreEqual(2, sc.Headers.Count); Assert.AreEqual("b", sc.Headers["a"]); Assert.AreEqual(2, sc.Headers["one"]); } + [Test] + public void can_remove_header_by_setting_null() { + var sc = GetSettingsContainer().WithHeaders(new { a = 1, b = 2 }); + Assert.AreEqual(2, sc.Headers.Count); + sc.WithHeader("b", null); + Assert.AreEqual(1, sc.Headers.Count); + Assert.AreEqual("a", sc.Headers.Keys.Single()); + } + [Test] public void can_set_headers_from_dictionary() { var sc = GetSettingsContainer().WithHeaders(new Dictionary { { "a", "b" }, { "one", 2 } }); diff --git a/src/Flurl.Http/Flurl.Http.csproj b/src/Flurl.Http/Flurl.Http.csproj index 330f12db..40542fbc 100644 --- a/src/Flurl.Http/Flurl.Http.csproj +++ b/src/Flurl.Http/Flurl.Http.csproj @@ -4,7 +4,7 @@ net45;netstandard1.1;netstandard1.3;netstandard2.0; True Flurl.Http - 2.2.0 + 2.2.1 Todd Menier A fluent, portable, testable HTTP client library. http://tmenier.github.io/Flurl diff --git a/src/Flurl.Http/HeaderExtensions.cs b/src/Flurl.Http/HeaderExtensions.cs index 0d854d52..b2165ad2 100644 --- a/src/Flurl.Http/HeaderExtensions.cs +++ b/src/Flurl.Http/HeaderExtensions.cs @@ -21,7 +21,10 @@ public static class HeaderExtensions /// HTTP header value. /// This IFlurlClient or IFlurlRequest. public static T WithHeader(this T clientOrRequest, string name, object value) where T : IHttpSettingsContainer { - clientOrRequest.Headers[name] = value; + if (value == null && clientOrRequest.Headers.ContainsKey(name)) + clientOrRequest.Headers.Remove(name); + else if (value != null) + clientOrRequest.Headers[name] = value; return clientOrRequest; }