Skip to content

Commit

Permalink
HandleRequestsSynchronously (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH authored Aug 7, 2020
1 parent c4ee91c commit b55435d
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/WireMock.Net.Abstractions/Admin/Settings/SettingsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ public class SettingsModel
/// Gets or sets wether to allow a body for all HTTP methods.
/// </summary>
public bool? AllowBodyForAllHttpMethods { get; set; }

/// <summary>
/// Gets or sets wether to handle all requests synchronously.
/// </summary>
public bool? HandleRequestsSynchronously { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ internal interface IWireMockMiddlewareOptions
bool? DisableJsonBodyParsing { get; set; }

bool? DisableRequestBodyDecompressing { get; set; }

bool? HandleRequestsSynchronously { get; set; }
}
}
13 changes: 12 additions & 1 deletion src/WireMock.Net/Owin/WireMockMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace WireMock.Owin
{
internal class WireMockMiddleware : OwinMiddleware
{
private readonly object _lock = new object();
private static readonly Task CompletedTask = Task.FromResult(false);
private readonly IWireMockMiddlewareOptions _options;
private readonly IOwinRequestMapper _requestMapper;
Expand Down Expand Up @@ -65,7 +66,17 @@ public override Task Invoke(IContext ctx)
public Task Invoke(IContext ctx)
#endif
{
return InvokeInternal(ctx);
if (_options.HandleRequestsSynchronously.GetValueOrDefault(true))
{
lock (_lock)
{
return InvokeInternal(ctx);
}
}
else
{
return InvokeInternal(ctx);
}
}

private async Task InvokeInternal(IContext ctx)
Expand Down
3 changes: 3 additions & 0 deletions src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@ internal class WireMockMiddlewareOptions : IWireMockMiddlewareOptions

/// <inheritdoc cref="IWireMockMiddlewareOptions.DisableRequestBodyDecompressing"/>
public bool? DisableRequestBodyDecompressing { get; set; }

/// <inheritdoc cref="IWireMockMiddlewareOptions.HandleRequestsSynchronously"/>
public bool? HandleRequestsSynchronously { get; set; }
}
}
22 changes: 11 additions & 11 deletions src/WireMock.Net/Server/FluentMockServer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using WireMock.Settings;

namespace WireMock.Server
{
[Obsolete("Use WireMockServer. This will removed in next version (1.3.x)")]
public class FluentMockServer : WireMockServer
{
public FluentMockServer(IFluentMockServerSettings settings) : base((IWireMockServerSettings) settings)
{
}
}
using WireMock.Settings;

namespace WireMock.Server
{
[Obsolete("Use WireMockServer. This will removed in next version (1.3.x)")]
public class FluentMockServer : WireMockServer
{
public FluentMockServer(IFluentMockServerSettings settings) : base((IWireMockServerSettings) settings)
{
}
}
}
8 changes: 7 additions & 1 deletion src/WireMock.Net/Server/WireMockServer.Admin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ private ResponseMessage SettingsGet(RequestMessage requestMessage)
MaxRequestLogCount = _options.MaxRequestLogCount,
RequestLogExpirationDuration = _options.RequestLogExpirationDuration,
GlobalProcessingDelay = (int?)_options.RequestProcessingDelay?.TotalMilliseconds,
AllowBodyForAllHttpMethods = _options.AllowBodyForAllHttpMethods
AllowBodyForAllHttpMethods = _options.AllowBodyForAllHttpMethods,
HandleRequestsSynchronously = _options.HandleRequestsSynchronously
};

return ToJson(model);
Expand All @@ -371,6 +372,11 @@ private ResponseMessage SettingsUpdate(RequestMessage requestMessage)
_options.AllowBodyForAllHttpMethods = settings.AllowBodyForAllHttpMethods.Value;
}

if (settings.HandleRequestsSynchronously != null)
{
_options.HandleRequestsSynchronously = settings.HandleRequestsSynchronously.Value;
}

return ResponseMessageBuilder.Create("Settings updated");
}
#endregion Settings
Expand Down
1 change: 1 addition & 0 deletions src/WireMock.Net/Server/WireMockServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ protected WireMockServer(IWireMockServerSettings settings)
_options.PostWireMockMiddlewareInit = _settings.PostWireMockMiddlewareInit;
_options.Logger = _settings.Logger;
_options.DisableJsonBodyParsing = _settings.DisableJsonBodyParsing;
_options.HandleRequestsSynchronously = settings.HandleRequestsSynchronously;

_matcherMapper = new MatcherMapper(_settings);
_mappingConverter = new MappingConverter(_matcherMapper);
Expand Down
8 changes: 7 additions & 1 deletion src/WireMock.Net/Settings/IWireMockServerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public interface IWireMockServerSettings
/// - false : also null, 0, empty or invalid HttpStatus codes are allowed.
/// - true : only codes defined in <see cref="System.Net.HttpStatusCode"/> are allowed.
/// </summary>
/// [PublicAPI]
[PublicAPI]
bool? AllowOnlyDefinedHttpStatusCodeInResponse { get; set; }

/// <summary>
Expand All @@ -157,5 +157,11 @@ public interface IWireMockServerSettings
/// </summary>
[PublicAPI]
bool? DisableRequestBodyDecompressing { get; set; }

/// <summary>
/// Handle all requests synchronously. (default set to false).
/// </summary>
[PublicAPI]
bool? HandleRequestsSynchronously { get; set; }
}
}
4 changes: 4 additions & 0 deletions src/WireMock.Net/Settings/WireMockServerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,9 @@ public class WireMockServerSettings : IWireMockServerSettings
/// <inheritdoc cref="IWireMockServerSettings.DisableRequestBodyDecompressing"/>
[PublicAPI]
public bool? DisableRequestBodyDecompressing { get; set; }

/// <inheritdoc cref="IWireMockServerSettings.HandleRequestsSynchronously"/>
[PublicAPI]
public bool? HandleRequestsSynchronously { get; set; }
}
}
3 changes: 2 additions & 1 deletion src/WireMock.Net/Settings/WireMockServerSettingsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public static IWireMockServerSettings ParseArguments([NotNull] string[] args, [C
AllowCSharpCodeMatcher = parser.GetBoolValue("AllowCSharpCodeMatcher"),
AllowBodyForAllHttpMethods = parser.GetBoolValue("AllowBodyForAllHttpMethods"),
AllowOnlyDefinedHttpStatusCodeInResponse = parser.GetBoolValue("AllowOnlyDefinedHttpStatusCodeInResponse"),
DisableJsonBodyParsing = parser.GetBoolValue("DisableJsonBodyParsing")
DisableJsonBodyParsing = parser.GetBoolValue("DisableJsonBodyParsing"),
HandleRequestsSynchronously = parser.GetBoolValue("HandleRequestsSynchronously")
};

if (logger != null)
Expand Down

0 comments on commit b55435d

Please sign in to comment.