From 4bfb97b1922657a3bfe7f4d1d7df15dbc02f117c Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Mon, 12 Oct 2020 21:48:37 +0200 Subject: [PATCH] wip --- .../Server/IWireMockServer.cs | 4 +-- .../Owin/IWireMockMiddlewareOptions.cs | 3 ++ src/WireMock.Net/Owin/MappingMatcher.cs | 10 ++++++ .../Owin/WireMockMiddlewareOptions.cs | 3 ++ .../Server/WireMockServer.Admin.cs | 1 + src/WireMock.Net/Server/WireMockServer.cs | 15 +++++++-- .../Settings/IPartialMappingSettings.cs | 13 ++++++++ .../Settings/IWireMockServerSettings.cs | 6 ++++ .../Settings/PartialMappingSettings.cs | 15 +++++++++ .../Settings/WireMockServerSettings.cs | 4 +++ test/WireMock.Net.Tests/RequestTests.cs | 33 +++++++++++++++++-- 11 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 src/WireMock.Net/Settings/IPartialMappingSettings.cs create mode 100644 src/WireMock.Net/Settings/PartialMappingSettings.cs diff --git a/src/WireMock.Net.Abstractions/Server/IWireMockServer.cs b/src/WireMock.Net.Abstractions/Server/IWireMockServer.cs index 7848b3df..801cc1cc 100644 --- a/src/WireMock.Net.Abstractions/Server/IWireMockServer.cs +++ b/src/WireMock.Net.Abstractions/Server/IWireMockServer.cs @@ -62,9 +62,9 @@ namespace WireMock.Server void AddGlobalProcessingDelay(TimeSpan delay); /// - /// Allows the partial mapping. + /// Set the partial mapping to allowed (if true, you can also provide 'enforceHttpMethod'). /// - void AllowPartialMapping(bool allow = true); + void AllowPartialMapping(bool allow = true, bool enforceHttpMethod = false); /// /// Deletes a LogEntry. diff --git a/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs b/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs index 44f900e2..5bca118a 100644 --- a/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs +++ b/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs @@ -4,6 +4,7 @@ using WireMock.Handlers; using WireMock.Logging; using WireMock.Matchers; using WireMock.Util; +using WireMock.Settings; #if !USE_ASPNETCORE using Owin; #else @@ -22,6 +23,8 @@ namespace WireMock.Owin bool? AllowPartialMapping { get; set; } + IPartialMappingSettings PartialMappingSettings { get; set; } + ConcurrentDictionary Mappings { get; } ConcurrentDictionary Scenarios { get; } diff --git a/src/WireMock.Net/Owin/MappingMatcher.cs b/src/WireMock.Net/Owin/MappingMatcher.cs index 71b4d814..1edcb5ad 100644 --- a/src/WireMock.Net/Owin/MappingMatcher.cs +++ b/src/WireMock.Net/Owin/MappingMatcher.cs @@ -42,6 +42,16 @@ namespace WireMock.Owin .OrderBy(m => m.RequestMatchResult) .ThenBy(m => m.Mapping.Priority) .ToList(); + + if (_options.PartialMappingSettings?.EnforceHttpMethod == true) + { + // Check if any partialMappings contain a HttpMethodMatcher, and check if this returns a 0 + foreach (var partialMapping in partialMappings) + { + + } + } + var partialMatch = partialMappings.FirstOrDefault(pm => pm.RequestMatchResult.AverageTotalScore > 0.0); if (_options.AllowPartialMapping == true) diff --git a/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs b/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs index e6b1b138..ab2e7280 100644 --- a/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs +++ b/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs @@ -4,6 +4,7 @@ using WireMock.Handlers; using WireMock.Logging; using WireMock.Matchers; using WireMock.Util; +using WireMock.Settings; #if !USE_ASPNETCORE using Owin; #else @@ -22,6 +23,8 @@ namespace WireMock.Owin public bool? AllowPartialMapping { get; set; } + public IPartialMappingSettings PartialMappingSettings { get; set; } + public ConcurrentDictionary Mappings { get; } = new ConcurrentDictionary(); public ConcurrentDictionary Scenarios { get; } = new ConcurrentDictionary(); diff --git a/src/WireMock.Net/Server/WireMockServer.Admin.cs b/src/WireMock.Net/Server/WireMockServer.Admin.cs index d0bd7d73..961fa0d2 100644 --- a/src/WireMock.Net/Server/WireMockServer.Admin.cs +++ b/src/WireMock.Net/Server/WireMockServer.Admin.cs @@ -367,6 +367,7 @@ namespace WireMock.Server if (settings.AllowPartialMapping != null) { _options.AllowPartialMapping = settings.AllowPartialMapping.Value; + // TODO stef _options.PartialMappingSettings = settings. } if (settings.GlobalProcessingDelay != null) diff --git a/src/WireMock.Net/Server/WireMockServer.cs b/src/WireMock.Net/Server/WireMockServer.cs index 8702c338..e1efbf35 100644 --- a/src/WireMock.Net/Server/WireMockServer.cs +++ b/src/WireMock.Net/Server/WireMockServer.cs @@ -283,7 +283,7 @@ namespace WireMock.Server if (settings.AllowPartialMapping == true) { - AllowPartialMapping(); + AllowPartialMapping(true, settings.PartialMappingSettings); } if (settings.StartAdminInterface == true) @@ -389,10 +389,21 @@ namespace WireMock.Server /// [PublicAPI] - public void AllowPartialMapping(bool allow = true) + public void AllowPartialMapping(bool allow = true, bool enforceHttpMethod = false) + { + AllowPartialMapping(allow, new PartialMappingSettings + { + EnforceHttpMethod = enforceHttpMethod + }); + } + + /// + [PublicAPI] + public void AllowPartialMapping(bool allow = true, IPartialMappingSettings partialMappingSettings = null) { _settings.Logger.Info("AllowPartialMapping is set to {0}", allow); _options.AllowPartialMapping = allow; + _options.PartialMappingSettings = partialMappingSettings; } /// diff --git a/src/WireMock.Net/Settings/IPartialMappingSettings.cs b/src/WireMock.Net/Settings/IPartialMappingSettings.cs new file mode 100644 index 00000000..22222419 --- /dev/null +++ b/src/WireMock.Net/Settings/IPartialMappingSettings.cs @@ -0,0 +1,13 @@ +namespace WireMock.Settings +{ + /// + /// IPartialMappingSettings + /// + public interface IPartialMappingSettings + { + /// + /// ... + /// + bool EnforceHttpMethod { get; set; } + } +} \ No newline at end of file diff --git a/src/WireMock.Net/Settings/IWireMockServerSettings.cs b/src/WireMock.Net/Settings/IWireMockServerSettings.cs index 65ce9a30..1371579f 100644 --- a/src/WireMock.Net/Settings/IWireMockServerSettings.cs +++ b/src/WireMock.Net/Settings/IWireMockServerSettings.cs @@ -73,6 +73,12 @@ namespace WireMock.Settings [PublicAPI] bool? AllowPartialMapping { get; set; } + /// + /// Gets or sets the partial mapping settings (optional). + /// + [PublicAPI] + IPartialMappingSettings PartialMappingSettings { get; set; } + /// /// The username needed for __admin access. /// diff --git a/src/WireMock.Net/Settings/PartialMappingSettings.cs b/src/WireMock.Net/Settings/PartialMappingSettings.cs new file mode 100644 index 00000000..02f432c9 --- /dev/null +++ b/src/WireMock.Net/Settings/PartialMappingSettings.cs @@ -0,0 +1,15 @@ +using JetBrains.Annotations; + +namespace WireMock.Settings +{ + /// + /// PartialMappingSettings + /// + /// + public class PartialMappingSettings : IPartialMappingSettings + { + /// + [PublicAPI] + public bool EnforceHttpMethod { get; set; } + } +} \ No newline at end of file diff --git a/src/WireMock.Net/Settings/WireMockServerSettings.cs b/src/WireMock.Net/Settings/WireMockServerSettings.cs index ac1beb14..494045e1 100644 --- a/src/WireMock.Net/Settings/WireMockServerSettings.cs +++ b/src/WireMock.Net/Settings/WireMockServerSettings.cs @@ -53,6 +53,10 @@ namespace WireMock.Settings [PublicAPI] public bool? AllowPartialMapping { get; set; } + /// + [PublicAPI] + public IPartialMappingSettings PartialMappingSettings { get; set; } + /// [PublicAPI] public string AdminUsername { get; set; } diff --git a/test/WireMock.Net.Tests/RequestTests.cs b/test/WireMock.Net.Tests/RequestTests.cs index 32081267..fd1b69cc 100644 --- a/test/WireMock.Net.Tests/RequestTests.cs +++ b/test/WireMock.Net.Tests/RequestTests.cs @@ -1,5 +1,6 @@ -using NFluent; -using System.Collections.Generic; +using System.Collections.Generic; +using FluentAssertions; +using NFluent; using WireMock.Matchers.Request; using WireMock.Models; using WireMock.RequestBuilders; @@ -13,6 +14,34 @@ namespace WireMock.Net.Tests { private const string ClientIp = "::1"; + [Fact] + public void Should_Match_When_Verb_Does_Match() + { + // Arrange + var requestPut = Request.Create().WithPath("/bar").UsingPut(); + + // Act + var request = new RequestMessage(new UrlDetails("http://localhost/bar"), "PUT", ClientIp); + + // Assert + var requestMatchResult = new RequestMatchResult(); + requestPut.GetMatchingScore(request, requestMatchResult).Should().Be(1.0); + } + + [Fact] + public void Should_NotMatch_When_Verb_Does_Not_Match() + { + // Arrange + var requestGet = Request.Create().WithPath("/bar").UsingGet(); + + // Act + var request = new RequestMessage(new UrlDetails("http://localhost/bar"), "PUT", ClientIp); + + // Assert + var requestMatchResult = new RequestMatchResult(); + requestGet.GetMatchingScore(request, requestMatchResult).Should().Be(0.0); + } + [Fact] public void Should_exclude_requests_matching_given_http_method_but_not_url() {