From 7cf283ec13a94681b3c2cccc48f0e28d95ea9591 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 17 Apr 2018 19:43:38 +0200 Subject: [PATCH] Restricted ResponseHeaders (#126) --- src/WireMock.Net/Owin/OwinResponseMapper.cs | 23 +++++++++++----- .../FluentMockServerTests.cs | 18 +++++++++++++ test/WireMock.Net.Tests/ResponseTests.cs | 26 +++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 test/WireMock.Net.Tests/ResponseTests.cs diff --git a/src/WireMock.Net/Owin/OwinResponseMapper.cs b/src/WireMock.Net/Owin/OwinResponseMapper.cs index 4702dd2d..fd9a3660 100644 --- a/src/WireMock.Net/Owin/OwinResponseMapper.cs +++ b/src/WireMock.Net/Owin/OwinResponseMapper.cs @@ -5,6 +5,7 @@ using System.Linq; 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 @@ namespace WireMock.Owin { 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 a3faaa43..aaf3288d 100644 --- a/test/WireMock.Net.Tests/FluentMockServerTests.cs +++ b/test/WireMock.Net.Tests/FluentMockServerTests.cs @@ -545,6 +545,24 @@ namespace WireMock.Net.Tests 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 00000000..700ca009 --- /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