diff --git a/src/WireMock.Net/Owin/OwinResponseMapper.cs b/src/WireMock.Net/Owin/OwinResponseMapper.cs index 4702dd2df..fd9a36605 100644 --- a/src/WireMock.Net/Owin/OwinResponseMapper.cs +++ b/src/WireMock.Net/Owin/OwinResponseMapper.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; +using WireMock.Http; using WireMock.Util; #if !NETSTANDARD using Microsoft.Owin; @@ -21,17 +22,27 @@ public class OwinResponseMapper { private readonly Encoding _utf8NoBom = new UTF8Encoding(false); - // https://stackoverflow.com/questions/239725/cannot-set-some-http-headers-when-using-system-net-webrequest + // https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx #if !NETSTANDARD private static readonly IDictionary>> RestrictedResponseHeaders = new Dictionary>>(StringComparer.OrdinalIgnoreCase) { #else private static readonly IDictionary>> RestrictedResponseHeaders = new Dictionary>>(StringComparer.OrdinalIgnoreCase) { #endif - { "Content-Length", null }, - { "Content-Type", (r, v) => r.ContentType = v.FirstOrDefault() }, - { "Keep-Alive", null }, - { "Transfer-Encoding", null }, - { "WWW-Authenticate", null } + { HttpKnownHeaderNames.Accept, null }, + { HttpKnownHeaderNames.Connection, null }, + { HttpKnownHeaderNames.ContentLength, null }, + { HttpKnownHeaderNames.ContentType, (r, v) => r.ContentType = v.FirstOrDefault() }, + { HttpKnownHeaderNames.Date, null }, + { HttpKnownHeaderNames.Expect, null }, + { HttpKnownHeaderNames.Host, null }, + { HttpKnownHeaderNames.IfModifiedSince, null }, + { HttpKnownHeaderNames.KeepAlive, null }, + { HttpKnownHeaderNames.Range, null }, + { HttpKnownHeaderNames.Referer, null }, + { HttpKnownHeaderNames.TransferEncoding, null }, + { HttpKnownHeaderNames.UserAgent, null }, + { HttpKnownHeaderNames.ProxyConnection, null }, + { HttpKnownHeaderNames.WWWAuthenticate, null } }; /// diff --git a/test/WireMock.Net.Tests/FluentMockServerTests.cs b/test/WireMock.Net.Tests/FluentMockServerTests.cs index a3faaa438..aaf3288dd 100644 --- a/test/WireMock.Net.Tests/FluentMockServerTests.cs +++ b/test/WireMock.Net.Tests/FluentMockServerTests.cs @@ -545,6 +545,24 @@ public async Task FluentMockServer_Should_respond_to_request_callback() Check.That(response).IsEqualTo("/fooBar"); } + [Fact] + public async Task FluentMockServer_Should_IgnoreRestrictedHeader() + { + // Assign + _server = FluentMockServer.Start(); + _server + .Given(Request.Create().WithPath("/head").UsingHead()) + .RespondWith(Response.Create().WithHeader("Content-Length", "1024")); + + var request = new HttpRequestMessage(HttpMethod.Head, "http://localhost:" + _server.Ports[0] + "/head"); + + // Act + var response = await new HttpClient().SendAsync(request); + + // Assert + Check.That(response.Content.Headers.GetValues("Content-Length")).ContainsExactly("0"); + } + public void Dispose() { _server?.Stop(); diff --git a/test/WireMock.Net.Tests/ResponseTests.cs b/test/WireMock.Net.Tests/ResponseTests.cs new file mode 100644 index 000000000..700ca009b --- /dev/null +++ b/test/WireMock.Net.Tests/ResponseTests.cs @@ -0,0 +1,26 @@ +using System; +using NFluent; +using WireMock.ResponseBuilders; +using Xunit; + +namespace WireMock.Net.Tests +{ + public class ResponseTests + { + private const string ClientIp = "::1"; + + [Fact] + public async void Response_Create_WithHeader_ContentLength() + { + // Assign + var requestMock = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp); + IResponseBuilder builder = Response.Create().WithHeader("Content-Length", "1024"); + + // Act + var response = await builder.ProvideResponseAsync(requestMock); + + // Assert + Check.That(response.Headers["Content-Length"].ToString()).Equals("1024"); + } + } +} \ No newline at end of file