Skip to content

Commit

Permalink
Restricted ResponseHeaders (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Apr 17, 2018
1 parent 2c0f00d commit 7cf283e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/WireMock.Net/Owin/OwinResponseMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string, Action<IOwinResponse, WireMockList<string>>> RestrictedResponseHeaders = new Dictionary<string, Action<IOwinResponse, WireMockList<string>>>(StringComparer.OrdinalIgnoreCase) {
#else
private static readonly IDictionary<string, Action<HttpResponse, WireMockList<string>>> RestrictedResponseHeaders = new Dictionary<string, Action<HttpResponse, WireMockList<string>>>(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 }
};

/// <summary>
Expand Down
18 changes: 18 additions & 0 deletions test/WireMock.Net.Tests/FluentMockServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
26 changes: 26 additions & 0 deletions test/WireMock.Net.Tests/ResponseTests.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}

0 comments on commit 7cf283e

Please sign in to comment.