IsRestrictedResponseHeader (#228)

This commit is contained in:
Stef Heyenrath
2018-11-07 20:07:50 +00:00
committed by GitHub
parent d506df9645
commit dffeb95116
3 changed files with 37 additions and 16 deletions

View File

@@ -2,6 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System;
using System.Linq;
namespace WireMock.Http namespace WireMock.Http
{ {
/// <summary> /// <summary>
@@ -9,6 +12,29 @@ namespace WireMock.Http
/// </summary> /// </summary>
internal static class HttpKnownHeaderNames 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
};
/// <summary>Tests whether the specified HTTP header can be set for the response.</summary>
/// <param name="headerName">The header to test.</param>
/// <returns>true if the header is restricted; otherwise, false.</returns>
public static bool IsRestrictedResponseHeader(string headerName) => RestrictedResponseHeaders.Contains(headerName, StringComparer.OrdinalIgnoreCase);
public const string Accept = "Accept"; public const string Accept = "Accept";
public const string AcceptCharset = "Accept-Charset"; public const string AcceptCharset = "Accept-Charset";
public const string AcceptEncoding = "Accept-Encoding"; public const string AcceptEncoding = "Accept-Encoding";

View File

@@ -8,8 +8,6 @@ using Newtonsoft.Json;
using WireMock.Http; using WireMock.Http;
using WireMock.Util; using WireMock.Util;
#if !USE_ASPNETCORE #if !USE_ASPNETCORE
using System.Net;
using Microsoft.Owin;
using IResponse = Microsoft.Owin.IOwinResponse; using IResponse = Microsoft.Owin.IOwinResponse;
#else #else
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@@ -85,16 +83,15 @@ namespace WireMock.Owin.Mappers
} }
else else
{ {
#if !USE_ASPNETCORE // Check if this response header can be added (#148 and #227)
// For non-NETSTANDARD, check if this response header can be added (#148) if (!HttpKnownHeaderNames.IsRestrictedResponseHeader(pair.Key))
if (!WebHeaderCollection.IsRestricted(pair.Key, true))
{ {
#if !USE_ASPNETCORE
response.Headers.AppendValues(pair.Key, pair.Value.ToArray()); response.Headers.AppendValues(pair.Key, pair.Value.ToArray());
}
#else #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 #endif
}
} }
} }
} }

View File

@@ -154,25 +154,23 @@ namespace WireMock.Net.Tests
// Check.That(result).Contains("google"); // Check.That(result).Contains("google");
//} //}
#if !NET452
[Fact] [Fact]
public async Task FluentMockServer_Should_not_exclude_restrictedResponseHeader_for_ASPNETCORE() public async Task FluentMockServer_Should_exclude_restrictedResponseHeader()
{ {
// Assign // Assign
string path = $"/foo_{Guid.NewGuid()}"; string path = $"/foo_{Guid.NewGuid()}";
var _server = FluentMockServer.Start(); var server = FluentMockServer.Start();
_server server
.Given(Request.Create().WithPath(path).UsingGet()) .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 // 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 // Assert
Check.That(response.Headers.Contains("test")).IsTrue(); Check.That(response.Headers.Contains("test")).IsTrue();
Check.That(response.Headers.Contains("Keep-Alive")).IsTrue(); Check.That(response.Headers.Contains("Transfer-Encoding")).IsFalse();
} }
#endif
} }
} }