diff --git a/src/WireMock.Net/Http/HttpKnownHeaderNames.cs b/src/WireMock.Net/Http/HttpKnownHeaderNames.cs index c17bc0ab..857e1e7e 100644 --- a/src/WireMock.Net/Http/HttpKnownHeaderNames.cs +++ b/src/WireMock.Net/Http/HttpKnownHeaderNames.cs @@ -2,6 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; +using System.Linq; + namespace WireMock.Http { /// @@ -9,6 +12,29 @@ namespace WireMock.Http /// internal static class HttpKnownHeaderNames { + // https://docs.microsoft.com/en-us/dotnet/api/system.net.webheadercollection.isrestricted + private static readonly string[] RestrictedResponseHeaders = + { + Accept, + Connection, + ContentLength, + ContentType, + Date, + Expect, + Host, + IfModifiedSince, + Range, + Referer, + TransferEncoding, + UserAgent, + ProxyConnection + }; + + /// Tests whether the specified HTTP header can be set for the response. + /// The header to test. + /// true if the header is restricted; otherwise, false. + public static bool IsRestrictedResponseHeader(string headerName) => RestrictedResponseHeaders.Contains(headerName, StringComparer.OrdinalIgnoreCase); + public const string Accept = "Accept"; public const string AcceptCharset = "Accept-Charset"; public const string AcceptEncoding = "Accept-Encoding"; diff --git a/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs b/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs index aefdcafa..bed90f27 100644 --- a/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs +++ b/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs @@ -8,8 +8,6 @@ using Newtonsoft.Json; using WireMock.Http; using WireMock.Util; #if !USE_ASPNETCORE -using System.Net; -using Microsoft.Owin; using IResponse = Microsoft.Owin.IOwinResponse; #else using Microsoft.AspNetCore.Http; @@ -85,16 +83,15 @@ namespace WireMock.Owin.Mappers } else { -#if !USE_ASPNETCORE - // For non-NETSTANDARD, check if this response header can be added (#148) - if (!WebHeaderCollection.IsRestricted(pair.Key, true)) + // Check if this response header can be added (#148 and #227) + if (!HttpKnownHeaderNames.IsRestrictedResponseHeader(pair.Key)) { +#if !USE_ASPNETCORE response.Headers.AppendValues(pair.Key, pair.Value.ToArray()); - } #else - // NETSTANDARD can add any header (or so it seems) - response.Headers.Append(pair.Key, pair.Value.ToArray()); + response.Headers.Append(pair.Key, pair.Value.ToArray()); #endif + } } } } diff --git a/test/WireMock.Net.Tests/FluentMockServerTests.cs b/test/WireMock.Net.Tests/FluentMockServerTests.cs index a152cad2..5b6f0074 100644 --- a/test/WireMock.Net.Tests/FluentMockServerTests.cs +++ b/test/WireMock.Net.Tests/FluentMockServerTests.cs @@ -154,25 +154,23 @@ namespace WireMock.Net.Tests // Check.That(result).Contains("google"); //} -#if !NET452 [Fact] - public async Task FluentMockServer_Should_not_exclude_restrictedResponseHeader_for_ASPNETCORE() + public async Task FluentMockServer_Should_exclude_restrictedResponseHeader() { // Assign string path = $"/foo_{Guid.NewGuid()}"; - var _server = FluentMockServer.Start(); + var server = FluentMockServer.Start(); - _server + server .Given(Request.Create().WithPath(path).UsingGet()) - .RespondWith(Response.Create().WithHeader("Keep-Alive", "k").WithHeader("test", "t")); + .RespondWith(Response.Create().WithHeader("Transfer-Encoding", "chunked").WithHeader("test", "t")); // Act - var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + path); + var response = await new HttpClient().GetAsync("http://localhost:" + server.Ports[0] + path); // Assert Check.That(response.Headers.Contains("test")).IsTrue(); - Check.That(response.Headers.Contains("Keep-Alive")).IsTrue(); + Check.That(response.Headers.Contains("Transfer-Encoding")).IsFalse(); } -#endif } } \ No newline at end of file