mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 00:25:06 +01:00
Fixed restricted headers on response (#71)
* Fixed restricted headers on response * appveyor
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio 15
|
# Visual Studio 15
|
||||||
VisualStudioVersion = 15.0.27004.2005
|
VisualStudioVersion = 15.0.27004.2010
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EF242EDF-7133-4277-9A0C-18744DE08707}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EF242EDF-7133-4277-9A0C-18744DE08707}"
|
||||||
EndProject
|
EndProject
|
||||||
@@ -39,7 +39,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.StandAlone.Net
|
|||||||
{B6269AAC-170A-43D5-8B9A-579DED3D9A95} = {B6269AAC-170A-43D5-8B9A-579DED3D9A95}
|
{B6269AAC-170A-43D5-8B9A-579DED3D9A95} = {B6269AAC-170A-43D5-8B9A-579DED3D9A95}
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.ConsoleApplication", "examples\WireMock.Net.ConsoleApplication\WireMock.Net.ConsoleApplication.csproj", "{668F689E-57B4-422E-8846-C0FF643CA268}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.Console.NET452", "examples\WireMock.Net.ConsoleApplication\WireMock.Net.Console.NET452.csproj", "{668F689E-57B4-422E-8846-C0FF643CA268}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ environment:
|
|||||||
before_build:
|
before_build:
|
||||||
- dotnet restore .\src\WireMock.Net\WireMock.Net.csproj
|
- dotnet restore .\src\WireMock.Net\WireMock.Net.csproj
|
||||||
- dotnet restore .\src\WireMock.Net.Standalone\WireMock.Net.Standalone.csproj
|
- dotnet restore .\src\WireMock.Net.Standalone\WireMock.Net.Standalone.csproj
|
||||||
- nuget restore .\examples\WireMock.Net.ConsoleApplication\WireMock.Net.ConsoleApplication.csproj -PackagesDirectory packages
|
|
||||||
|
|
||||||
build_script:
|
build_script:
|
||||||
# build WireMock.Net
|
# build WireMock.Net
|
||||||
|
|||||||
@@ -21,6 +21,10 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
Urls = new[] { url1, url2, url3 },
|
Urls = new[] { url1, url2, url3 },
|
||||||
StartAdminInterface = true,
|
StartAdminInterface = true,
|
||||||
ReadStaticMappings = true,
|
ReadStaticMappings = true,
|
||||||
|
//ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||||
|
//{
|
||||||
|
// SaveMapping = true
|
||||||
|
//},
|
||||||
PreWireMockMiddlewareInit = app => { System.Console.WriteLine($"PreWireMockMiddlewareInit : {app.GetType()}"); },
|
PreWireMockMiddlewareInit = app => { System.Console.WriteLine($"PreWireMockMiddlewareInit : {app.GetType()}"); },
|
||||||
PostWireMockMiddlewareInit = app => { System.Console.WriteLine($"PostWireMockMiddlewareInit : {app.GetType()}"); }
|
PostWireMockMiddlewareInit = app => { System.Console.WriteLine($"PostWireMockMiddlewareInit : {app.GetType()}"); }
|
||||||
});
|
});
|
||||||
@@ -30,6 +34,12 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
|
|
||||||
// server.AllowPartialMapping();
|
// server.AllowPartialMapping();
|
||||||
|
|
||||||
|
// .WithHeader("Stef", "Stef")
|
||||||
|
//server
|
||||||
|
// .Given(Request.Create().WithPath("*"))
|
||||||
|
// .RespondWith(Response.Create()
|
||||||
|
// .WithProxy("http://restcountries.eu"));
|
||||||
|
|
||||||
server
|
server
|
||||||
.Given(Request.Create().WithPath("/headers", "/headers_test").UsingPost().WithHeader("Content-Type", "application/json*"))
|
.Given(Request.Create().WithPath("/headers", "/headers_test").UsingPost().WithHeader("Content-Type", "application/json*"))
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create()
|
||||||
|
|||||||
@@ -25,10 +25,10 @@ namespace WireMock.Owin
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if NETSTANDARD
|
#if NETSTANDARD
|
||||||
public RequestDelegate Next { get; private set; }
|
public RequestDelegate Next { get; }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private readonly OwinResponseMapper _responseMapper = new OwinResponseMapper();
|
private readonly OwinResponseMapper _responseMapper = new OwinResponseMapper();
|
||||||
|
|
||||||
#if !NETSTANDARD
|
#if !NETSTANDARD
|
||||||
public override async Task Invoke(IOwinContext ctx)
|
public override async Task Invoke(IOwinContext ctx)
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
using System.IO;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using WireMock.Http;
|
using WireMock.Util;
|
||||||
#if !NETSTANDARD
|
#if !NETSTANDARD
|
||||||
using Microsoft.Owin;
|
using Microsoft.Owin;
|
||||||
#else
|
#else
|
||||||
@@ -18,6 +20,19 @@ namespace WireMock.Owin
|
|||||||
{
|
{
|
||||||
private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
|
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>
|
/// <summary>
|
||||||
/// MapAsync ResponseMessage to OwinResponse
|
/// MapAsync ResponseMessage to OwinResponse
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -33,18 +48,22 @@ namespace WireMock.Owin
|
|||||||
{
|
{
|
||||||
response.StatusCode = responseMessage.StatusCode;
|
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();
|
if (RestrictedResponseHeaders.ContainsKey(pair.Key))
|
||||||
}
|
{
|
||||||
|
RestrictedResponseHeaders[pair.Key]?.Invoke(response, pair.Value);
|
||||||
var headers = responseMessage.Headers.Where(h => h.Key != HttpKnownHeaderNames.ContentType).ToList();
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
#if !NETSTANDARD
|
#if !NETSTANDARD
|
||||||
headers.ForEach(pair => response.Headers.AppendValues(pair.Key, pair.Value.ToArray()));
|
response.Headers.AppendValues(pair.Key, pair.Value.ToArray());
|
||||||
#else
|
#else
|
||||||
headers.ForEach(pair => response.Headers.Append(pair.Key, pair.Value.ToArray()));
|
response.Headers.Append(pair.Key, pair.Value.ToArray());
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (responseMessage.Body == null && responseMessage.BodyAsBytes == null && responseMessage.BodyAsFile == null)
|
if (responseMessage.Body == null && responseMessage.BodyAsBytes == null && responseMessage.BodyAsFile == null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class Response : IResponseBuilder
|
public class Response : IResponseBuilder
|
||||||
{
|
{
|
||||||
private HttpClient httpClientForProxy;
|
private HttpClient _httpClientForProxy;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The delay
|
/// The delay
|
||||||
@@ -274,35 +274,23 @@ namespace WireMock.ResponseBuilders
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc cref="ITransformResponseBuilder.WithTransformer"/>
|
||||||
/// The with transformer.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// The <see cref="IResponseBuilder"/>.
|
|
||||||
/// </returns>
|
|
||||||
public IResponseBuilder WithTransformer()
|
public IResponseBuilder WithTransformer()
|
||||||
{
|
{
|
||||||
UseTransformer = true;
|
UseTransformer = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc cref="IDelayResponseBuilder.WithDelay(TimeSpan)"/>
|
||||||
/// The with delay.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="delay">The TimeSpan to delay.</param>
|
|
||||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
|
||||||
public IResponseBuilder WithDelay(TimeSpan delay)
|
public IResponseBuilder WithDelay(TimeSpan delay)
|
||||||
{
|
{
|
||||||
Check.Condition(delay, d => d > TimeSpan.Zero, nameof(delay));
|
Check.Condition(delay, d => d > TimeSpan.Zero, nameof(delay));
|
||||||
|
|
||||||
Delay = delay;
|
Delay = delay;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc cref="IDelayResponseBuilder.WithDelay(int)"/>
|
||||||
/// The with delay.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="milliseconds">The milliseconds to delay.</param>
|
|
||||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
|
||||||
public IResponseBuilder WithDelay(int milliseconds)
|
public IResponseBuilder WithDelay(int milliseconds)
|
||||||
{
|
{
|
||||||
return WithDelay(TimeSpan.FromMilliseconds(milliseconds));
|
return WithDelay(TimeSpan.FromMilliseconds(milliseconds));
|
||||||
@@ -320,7 +308,7 @@ namespace WireMock.ResponseBuilders
|
|||||||
|
|
||||||
ProxyUrl = proxyUrl;
|
ProxyUrl = proxyUrl;
|
||||||
X509Certificate2ThumbprintOrSubjectName = clientX509Certificate2ThumbprintOrSubjectName;
|
X509Certificate2ThumbprintOrSubjectName = clientX509Certificate2ThumbprintOrSubjectName;
|
||||||
httpClientForProxy = HttpClientHelper.CreateHttpClient(clientX509Certificate2ThumbprintOrSubjectName);
|
_httpClientForProxy = HttpClientHelper.CreateHttpClient(clientX509Certificate2ThumbprintOrSubjectName);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -334,14 +322,17 @@ namespace WireMock.ResponseBuilders
|
|||||||
Check.NotNull(requestMessage, nameof(requestMessage));
|
Check.NotNull(requestMessage, nameof(requestMessage));
|
||||||
|
|
||||||
if (Delay != null)
|
if (Delay != null)
|
||||||
|
{
|
||||||
await Task.Delay(Delay.Value);
|
await Task.Delay(Delay.Value);
|
||||||
|
}
|
||||||
|
|
||||||
if (ProxyUrl != null && httpClientForProxy != null)
|
if (ProxyUrl != null && _httpClientForProxy != null)
|
||||||
{
|
{
|
||||||
var requestUri = new Uri(requestMessage.Url);
|
var requestUri = new Uri(requestMessage.Url);
|
||||||
var proxyUri = new Uri(ProxyUrl);
|
var proxyUri = new Uri(ProxyUrl);
|
||||||
var proxyUriWithRequestPathAndQuery = new Uri(proxyUri, requestUri.PathAndQuery);
|
var proxyUriWithRequestPathAndQuery = new Uri(proxyUri, requestUri.PathAndQuery);
|
||||||
return await HttpClientHelper.SendAsync(httpClientForProxy, requestMessage, proxyUriWithRequestPathAndQuery.AbsoluteUri);
|
|
||||||
|
return await HttpClientHelper.SendAsync(_httpClientForProxy, requestMessage, proxyUriWithRequestPathAndQuery.AbsoluteUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (UseTransformer)
|
if (UseTransformer)
|
||||||
|
|||||||
Reference in New Issue
Block a user