mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-05-04 14:44:36 +02:00
@@ -56,6 +56,7 @@ namespace WireMock.Net.StandAlone
|
|||||||
MaxRequestLogCount = parser.GetIntValue("MaxRequestLogCount"),
|
MaxRequestLogCount = parser.GetIntValue("MaxRequestLogCount"),
|
||||||
RequestLogExpirationDuration = parser.GetIntValue("RequestLogExpirationDuration"),
|
RequestLogExpirationDuration = parser.GetIntValue("RequestLogExpirationDuration"),
|
||||||
AllowCSharpCodeMatcher = parser.GetBoolValue("AllowCSharpCodeMatcher"),
|
AllowCSharpCodeMatcher = parser.GetBoolValue("AllowCSharpCodeMatcher"),
|
||||||
|
AllowBodyForAllHttpMethods = parser.GetBoolValue("AllowBodyForAllHttpMethods")
|
||||||
};
|
};
|
||||||
|
|
||||||
if (logger != null)
|
if (logger != null)
|
||||||
|
|||||||
@@ -24,5 +24,10 @@
|
|||||||
/// Gets or sets the MaxRequestLog count.
|
/// Gets or sets the MaxRequestLog count.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int? MaxRequestLogCount { get; set; }
|
public int? MaxRequestLogCount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets wether to allow a body for all HTTP methods.
|
||||||
|
/// </summary>
|
||||||
|
public bool? AllowBodyForAllHttpMethods { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,7 +20,7 @@ namespace WireMock.Owin
|
|||||||
|
|
||||||
IStringMatcher AuthorizationMatcher { get; set; }
|
IStringMatcher AuthorizationMatcher { get; set; }
|
||||||
|
|
||||||
bool AllowPartialMapping { get; set; }
|
bool? AllowPartialMapping { get; set; }
|
||||||
|
|
||||||
ConcurrentDictionary<Guid, IMapping> Mappings { get; }
|
ConcurrentDictionary<Guid, IMapping> Mappings { get; }
|
||||||
|
|
||||||
@@ -37,5 +37,7 @@ namespace WireMock.Owin
|
|||||||
Action<IAppBuilder> PostWireMockMiddlewareInit { get; set; }
|
Action<IAppBuilder> PostWireMockMiddlewareInit { get; set; }
|
||||||
|
|
||||||
IFileSystemHandler FileSystemHandler { get; set; }
|
IFileSystemHandler FileSystemHandler { get; set; }
|
||||||
|
|
||||||
|
bool? AllowBodyForAllHttpMethods { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -16,7 +16,8 @@ namespace WireMock.Owin.Mappers
|
|||||||
/// MapAsync IRequest to RequestMessage
|
/// MapAsync IRequest to RequestMessage
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request">The OwinRequest/HttpRequest</param>
|
/// <param name="request">The OwinRequest/HttpRequest</param>
|
||||||
|
/// <param name="options">The WireMockMiddlewareOptions</param>
|
||||||
/// <returns>RequestMessage</returns>
|
/// <returns>RequestMessage</returns>
|
||||||
Task<RequestMessage> MapAsync(IRequest request);
|
Task<RequestMessage> MapAsync(IRequest request, IWireMockMiddlewareOptions options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,7 +20,7 @@ namespace WireMock.Owin.Mappers
|
|||||||
internal class OwinRequestMapper : IOwinRequestMapper
|
internal class OwinRequestMapper : IOwinRequestMapper
|
||||||
{
|
{
|
||||||
/// <inheritdoc cref="IOwinRequestMapper.MapAsync"/>
|
/// <inheritdoc cref="IOwinRequestMapper.MapAsync"/>
|
||||||
public async Task<RequestMessage> MapAsync(IRequest request)
|
public async Task<RequestMessage> MapAsync(IRequest request, IWireMockMiddlewareOptions options)
|
||||||
{
|
{
|
||||||
(UrlDetails urldetails, string clientIP) = ParseRequest(request);
|
(UrlDetails urldetails, string clientIP) = ParseRequest(request);
|
||||||
|
|
||||||
@@ -47,7 +47,7 @@ namespace WireMock.Owin.Mappers
|
|||||||
}
|
}
|
||||||
|
|
||||||
BodyData body = null;
|
BodyData body = null;
|
||||||
if (request.Body != null && BodyParser.ShouldParseBody(method))
|
if (request.Body != null && BodyParser.ShouldParseBody(method, options.AllowBodyForAllHttpMethods == true))
|
||||||
{
|
{
|
||||||
body = await BodyParser.Parse(request.Body, request.ContentType);
|
body = await BodyParser.Parse(request.Body, request.ContentType);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ namespace WireMock.Owin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_options.AllowPartialMapping)
|
if (_options.AllowPartialMapping == true)
|
||||||
{
|
{
|
||||||
var partialMappings = mappings
|
var partialMappings = mappings
|
||||||
.Where(pm => (pm.Mapping.IsAdminInterface && pm.RequestMatchResult.IsPerfectMatch) || !pm.Mapping.IsAdminInterface)
|
.Where(pm => (pm.Mapping.IsAdminInterface && pm.RequestMatchResult.IsPerfectMatch) || !pm.Mapping.IsAdminInterface)
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ namespace WireMock.Owin
|
|||||||
|
|
||||||
private async Task InvokeInternal(IContext ctx)
|
private async Task InvokeInternal(IContext ctx)
|
||||||
{
|
{
|
||||||
var request = await _requestMapper.MapAsync(ctx.Request);
|
var request = await _requestMapper.MapAsync(ctx.Request, _options);
|
||||||
|
|
||||||
bool logRequest = false;
|
bool logRequest = false;
|
||||||
ResponseMessage response = null;
|
ResponseMessage response = null;
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace WireMock.Owin
|
|||||||
|
|
||||||
public IStringMatcher AuthorizationMatcher { get; set; }
|
public IStringMatcher AuthorizationMatcher { get; set; }
|
||||||
|
|
||||||
public bool AllowPartialMapping { get; set; }
|
public bool? AllowPartialMapping { get; set; }
|
||||||
|
|
||||||
public ConcurrentDictionary<Guid, IMapping> Mappings { get; } = new ConcurrentDictionary<Guid, IMapping>();
|
public ConcurrentDictionary<Guid, IMapping> Mappings { get; } = new ConcurrentDictionary<Guid, IMapping>();
|
||||||
|
|
||||||
@@ -39,5 +39,8 @@ namespace WireMock.Owin
|
|||||||
|
|
||||||
/// <inheritdoc cref="IWireMockMiddlewareOptions.FileSystemHandler"/>
|
/// <inheritdoc cref="IWireMockMiddlewareOptions.FileSystemHandler"/>
|
||||||
public IFileSystemHandler FileSystemHandler { get; set; }
|
public IFileSystemHandler FileSystemHandler { get; set; }
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IWireMockMiddlewareOptions.AllowBodyForAllHttpMethods"/>
|
||||||
|
public bool? AllowBodyForAllHttpMethods { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -339,7 +339,8 @@ namespace WireMock.Server
|
|||||||
AllowPartialMapping = _options.AllowPartialMapping,
|
AllowPartialMapping = _options.AllowPartialMapping,
|
||||||
MaxRequestLogCount = _options.MaxRequestLogCount,
|
MaxRequestLogCount = _options.MaxRequestLogCount,
|
||||||
RequestLogExpirationDuration = _options.RequestLogExpirationDuration,
|
RequestLogExpirationDuration = _options.RequestLogExpirationDuration,
|
||||||
GlobalProcessingDelay = (int?)_options.RequestProcessingDelay?.TotalMilliseconds
|
GlobalProcessingDelay = (int?)_options.RequestProcessingDelay?.TotalMilliseconds,
|
||||||
|
AllowBodyForAllHttpMethods = _options.AllowBodyForAllHttpMethods
|
||||||
};
|
};
|
||||||
|
|
||||||
return ToJson(model);
|
return ToJson(model);
|
||||||
@@ -361,6 +362,11 @@ namespace WireMock.Server
|
|||||||
_options.RequestProcessingDelay = TimeSpan.FromMilliseconds(settings.GlobalProcessingDelay.Value);
|
_options.RequestProcessingDelay = TimeSpan.FromMilliseconds(settings.GlobalProcessingDelay.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (settings.AllowBodyForAllHttpMethods != null)
|
||||||
|
{
|
||||||
|
_options.AllowBodyForAllHttpMethods = settings.AllowBodyForAllHttpMethods.Value;
|
||||||
|
}
|
||||||
|
|
||||||
return ResponseMessageBuilder.Create("Settings updated");
|
return ResponseMessageBuilder.Create("Settings updated");
|
||||||
}
|
}
|
||||||
#endregion Settings
|
#endregion Settings
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
using JetBrains.Annotations;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using JetBrains.Annotations;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
using WireMock.Exceptions;
|
using WireMock.Exceptions;
|
||||||
using WireMock.Handlers;
|
using WireMock.Handlers;
|
||||||
@@ -257,6 +257,12 @@ namespace WireMock.Server
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (settings.AllowBodyForAllHttpMethods == true)
|
||||||
|
{
|
||||||
|
_options.AllowBodyForAllHttpMethods = _settings.AllowBodyForAllHttpMethods;
|
||||||
|
_settings.Logger.Info("AllowBodyForAllHttpMethods is set to {0}", _settings.AllowBodyForAllHttpMethods == true);
|
||||||
|
}
|
||||||
|
|
||||||
if (settings.AllowPartialMapping == true)
|
if (settings.AllowPartialMapping == true)
|
||||||
{
|
{
|
||||||
AllowPartialMapping();
|
AllowPartialMapping();
|
||||||
|
|||||||
@@ -94,5 +94,9 @@ namespace WireMock.Settings
|
|||||||
/// <inheritdoc cref="IFluentMockServerSettings.AllowCSharpCodeMatcher"/>
|
/// <inheritdoc cref="IFluentMockServerSettings.AllowCSharpCodeMatcher"/>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public bool? AllowCSharpCodeMatcher { get; set; }
|
public bool? AllowCSharpCodeMatcher { get; set; }
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IFluentMockServerSettings.AllowBodyForAllHttpMethods"/>
|
||||||
|
[PublicAPI]
|
||||||
|
public bool? AllowBodyForAllHttpMethods { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -124,6 +124,13 @@ namespace WireMock.Settings
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Allow the usage of CSharpCodeMatcher (default is not allowed).
|
/// Allow the usage of CSharpCodeMatcher (default is not allowed).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[PublicAPI]
|
||||||
bool? AllowCSharpCodeMatcher { get; set; }
|
bool? AllowCSharpCodeMatcher { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Allow a Body for all HTTP Methods. (default set to false).
|
||||||
|
/// </summary>
|
||||||
|
[PublicAPI]
|
||||||
|
bool? AllowBodyForAllHttpMethods { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,14 +59,19 @@ namespace WireMock.Util
|
|||||||
|
|
||||||
private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings { DateParseHandling = DateParseHandling.None };
|
private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings { DateParseHandling = DateParseHandling.None };
|
||||||
|
|
||||||
public static bool ShouldParseBody([CanBeNull] string method)
|
public static bool ShouldParseBody([CanBeNull] string httpMethod, bool allowBodyForAllHttpMethods)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(method))
|
if (string.IsNullOrEmpty(httpMethod))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (BodyAllowedForMethods.TryGetValue(method.ToUpper(), out bool allowed))
|
if (allowBodyForAllHttpMethods)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (BodyAllowedForMethods.TryGetValue(httpMethod.ToUpper(), out bool allowed))
|
||||||
{
|
{
|
||||||
return allowed;
|
return allowed;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,7 +116,25 @@ namespace WireMock.Net.Tests
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
|
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
|
||||||
Check.That(options.AllowPartialMapping).IsTrue();
|
Check.That(options.AllowPartialMapping).Equals(true);
|
||||||
|
|
||||||
|
// Verify
|
||||||
|
_loggerMock.Verify(l => l.Info(It.IsAny<string>(), It.IsAny<bool>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void FluentMockServer_FluentMockServerSettings_AllowBodyForAllHttpMethods()
|
||||||
|
{
|
||||||
|
// Assign and Act
|
||||||
|
var server = FluentMockServer.Start(new FluentMockServerSettings
|
||||||
|
{
|
||||||
|
Logger = _loggerMock.Object,
|
||||||
|
AllowBodyForAllHttpMethods = true
|
||||||
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
|
||||||
|
Check.That(options.AllowBodyForAllHttpMethods).Equals(true);
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
_loggerMock.Verify(l => l.Info(It.IsAny<string>(), It.IsAny<bool>()));
|
_loggerMock.Verify(l => l.Info(It.IsAny<string>(), It.IsAny<bool>()));
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ namespace WireMock.Net.Tests.Owin
|
|||||||
_requestMapperMock = new Mock<IOwinRequestMapper>();
|
_requestMapperMock = new Mock<IOwinRequestMapper>();
|
||||||
_requestMapperMock.SetupAllProperties();
|
_requestMapperMock.SetupAllProperties();
|
||||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
|
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
|
||||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>())).ReturnsAsync(request);
|
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||||
|
|
||||||
_responseMapperMock = new Mock<IOwinResponseMapper>();
|
_responseMapperMock = new Mock<IOwinResponseMapper>();
|
||||||
_responseMapperMock.SetupAllProperties();
|
_responseMapperMock.SetupAllProperties();
|
||||||
@@ -87,7 +87,7 @@ namespace WireMock.Net.Tests.Owin
|
|||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1", null, new Dictionary<string, string[]>());
|
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1", null, new Dictionary<string, string[]>());
|
||||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>())).ReturnsAsync(request);
|
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||||
|
|
||||||
_optionsMock.SetupGet(o => o.AuthorizationMatcher).Returns(new ExactMatcher());
|
_optionsMock.SetupGet(o => o.AuthorizationMatcher).Returns(new ExactMatcher());
|
||||||
_mappingMock.SetupGet(m => m.IsAdminInterface).Returns(true);
|
_mappingMock.SetupGet(m => m.IsAdminInterface).Returns(true);
|
||||||
@@ -108,7 +108,7 @@ namespace WireMock.Net.Tests.Owin
|
|||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1", null, new Dictionary<string, string[]> { { "h", new[] { "x" } } });
|
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1", null, new Dictionary<string, string[]> { { "h", new[] { "x" } } });
|
||||||
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>())).ReturnsAsync(request);
|
_requestMapperMock.Setup(m => m.MapAsync(It.IsAny<IRequest>(), It.IsAny<IWireMockMiddlewareOptions>())).ReturnsAsync(request);
|
||||||
|
|
||||||
_optionsMock.SetupGet(o => o.AuthorizationMatcher).Returns(new ExactMatcher());
|
_optionsMock.SetupGet(o => o.AuthorizationMatcher).Returns(new ExactMatcher());
|
||||||
_mappingMock.SetupGet(m => m.IsAdminInterface).Returns(true);
|
_mappingMock.SetupGet(m => m.IsAdminInterface).Returns(true);
|
||||||
|
|||||||
@@ -152,9 +152,26 @@ Content-Type: text/html
|
|||||||
[InlineData("OPTIONS", true)]
|
[InlineData("OPTIONS", true)]
|
||||||
[InlineData("CONNECT", false)]
|
[InlineData("CONNECT", false)]
|
||||||
[InlineData("PATCH", true)]
|
[InlineData("PATCH", true)]
|
||||||
public void BodyParser_ShouldParseBody_ExpectedResultForKnownMethods(string method, bool resultShouldBe)
|
public void BodyParser_ShouldParseBodyForMethodAndAllowAllIsFalse_ExpectedResultForKnownMethods(string method, bool resultShouldBe)
|
||||||
{
|
{
|
||||||
Check.That(BodyParser.ShouldParseBody(method)).Equals(resultShouldBe);
|
Check.That(BodyParser.ShouldParseBody(method, false)).Equals(resultShouldBe);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("HEAD")]
|
||||||
|
[InlineData("GET")]
|
||||||
|
[InlineData("PUT")]
|
||||||
|
[InlineData("POST")]
|
||||||
|
[InlineData("DELETE")]
|
||||||
|
[InlineData("TRACE")]
|
||||||
|
[InlineData("OPTIONS")]
|
||||||
|
[InlineData("CONNECT")]
|
||||||
|
[InlineData("PATCH")]
|
||||||
|
[InlineData("REPORT")]
|
||||||
|
[InlineData("SOME-UNKNOWN-METHOD")]
|
||||||
|
public void BodyParser_ShouldParseBodyForMethodAndAllowAllIsTrue_ExpectedResultShouldBeTrue(string method)
|
||||||
|
{
|
||||||
|
Check.That(BodyParser.ShouldParseBody(method, true)).IsTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
@@ -162,7 +179,7 @@ Content-Type: text/html
|
|||||||
[InlineData("SOME-UNKNOWN-METHOD")]
|
[InlineData("SOME-UNKNOWN-METHOD")]
|
||||||
public void BodyParser_ShouldParseBody_DefaultIsTrueForUnknownMethods(string method)
|
public void BodyParser_ShouldParseBody_DefaultIsTrueForUnknownMethods(string method)
|
||||||
{
|
{
|
||||||
Check.That(BodyParser.ShouldParseBody(method)).IsTrue();
|
Check.That(BodyParser.ShouldParseBody(method, false)).IsTrue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user