mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-23 01:29:05 +01:00
IsRestrictedResponseHeader (#228)
This commit is contained in:
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
@@ -9,6 +12,29 @@ namespace WireMock.Http
|
||||
/// </summary>
|
||||
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 AcceptCharset = "Accept-Charset";
|
||||
public const string AcceptEncoding = "Accept-Encoding";
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user