Fixed restricted headers on response (#71)

* Fixed restricted headers on response

* appveyor
This commit is contained in:
Stef Heyenrath
2017-12-07 22:16:19 +01:00
committed by GitHub
parent ea16ee866b
commit 601af2d6b2
7 changed files with 54 additions and 35 deletions

View File

@@ -25,10 +25,10 @@ namespace WireMock.Owin
#endif
#if NETSTANDARD
public RequestDelegate Next { get; private set; }
public RequestDelegate Next { get; }
#endif
private readonly OwinResponseMapper _responseMapper = new OwinResponseMapper();
private readonly OwinResponseMapper _responseMapper = new OwinResponseMapper();
#if !NETSTANDARD
public override async Task Invoke(IOwinContext ctx)

View File

@@ -1,8 +1,10 @@
using System.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WireMock.Http;
using WireMock.Util;
#if !NETSTANDARD
using Microsoft.Owin;
#else
@@ -18,6 +20,19 @@ 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
#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 }
};
/// <summary>
/// MapAsync ResponseMessage to OwinResponse
/// </summary>
@@ -33,18 +48,22 @@ namespace WireMock.Owin
{
response.StatusCode = responseMessage.StatusCode;
if (responseMessage.Headers.ContainsKey(HttpKnownHeaderNames.ContentType))
// Set headers
foreach (var pair in responseMessage.Headers)
{
response.ContentType = responseMessage.Headers[HttpKnownHeaderNames.ContentType].FirstOrDefault();
}
var headers = responseMessage.Headers.Where(h => h.Key != HttpKnownHeaderNames.ContentType).ToList();
if (RestrictedResponseHeaders.ContainsKey(pair.Key))
{
RestrictedResponseHeaders[pair.Key]?.Invoke(response, pair.Value);
}
else
{
#if !NETSTANDARD
headers.ForEach(pair => response.Headers.AppendValues(pair.Key, pair.Value.ToArray()));
response.Headers.AppendValues(pair.Key, pair.Value.ToArray());
#else
headers.ForEach(pair => response.Headers.Append(pair.Key, pair.Value.ToArray()));
response.Headers.Append(pair.Key, pair.Value.ToArray());
#endif
}
}
if (responseMessage.Body == null && responseMessage.BodyAsBytes == null && responseMessage.BodyAsFile == null)
{