From f2fab98abb30d9d93512232d020c67bb1784136b Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Thu, 27 Jan 2022 12:33:48 +0100 Subject: [PATCH] MatcherMapper : Always use Pattern (#716) --- .../11111110-a633-40e8-a244-5cb80bc0ab66.json | 25 ++- .../RequestBuilders/IBodyRequestBuilder.cs | 154 +++++++++--------- .../Serialization/MatcherMapper.cs | 4 +- .../Serialization/MappingConverterTests.cs | 2 +- .../Serialization/MatcherMapperTests.cs | 50 +++++- 5 files changed, 143 insertions(+), 92 deletions(-) diff --git a/examples/WireMock.Net.Console.NET5/__admin/mappings/11111110-a633-40e8-a244-5cb80bc0ab66.json b/examples/WireMock.Net.Console.NET5/__admin/mappings/11111110-a633-40e8-a244-5cb80bc0ab66.json index 9c761369..99738c88 100644 --- a/examples/WireMock.Net.Console.NET5/__admin/mappings/11111110-a633-40e8-a244-5cb80bc0ab66.json +++ b/examples/WireMock.Net.Console.NET5/__admin/mappings/11111110-a633-40e8-a244-5cb80bc0ab66.json @@ -1,22 +1,33 @@ -{ +{ "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", - "Pattern": "/static/mapping" + "Patterns": [ "/static/mapping", "/static/mapping2" ] } ] }, + "Body": { + "Matcher": { + "Name": "JsonMatcher", + "Pattern": { + "post1": "value 1", + "post2": "value 2" + }, + "IgnoreCase": true + } + }, "Methods": [ - "get" + "get", + "post" ] }, "Response": { "BodyAsJson": { "body": "static mapping" }, - "Headers": { - "Content-Type": "application/json", - "Test-X": [ "test 1", "test 2" ] - } + "Headers": { + "Content-Type": "application/json", + "Test-X": [ "test 1", "test 2" ] + } } } \ No newline at end of file diff --git a/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs index f4e768a1..29cb632c 100644 --- a/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs +++ b/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs @@ -1,80 +1,80 @@ -using JetBrains.Annotations; -using System; -using WireMock.Matchers; -using WireMock.Matchers.Request; +using JetBrains.Annotations; +using System; +using WireMock.Matchers; +using WireMock.Matchers.Request; using WireMock.Util; -namespace WireMock.RequestBuilders -{ - /// - /// The BodyRequestBuilder interface. - /// - public interface IBodyRequestBuilder : IRequestMatcher - { - /// - /// WithBody: IMatcher - /// - /// The matcher. - /// The . - IRequestBuilder WithBody([NotNull] IMatcher matcher); - - /// - /// WithBody: IMatcher[] - /// - /// The matchers. - /// The . - IRequestBuilder WithBody([NotNull] IMatcher[] matchers); - - /// - /// WithBody: Body as string - /// - /// The body. - /// The match behaviour. - /// The . - IRequestBuilder WithBody(string body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); - - /// - /// WithBody: Body as byte[] - /// - /// The body. - /// The match behaviour. - /// The . - IRequestBuilder WithBody(byte[] body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); - - /// - /// WithBody: Body as object - /// - /// The body. - /// The match behaviour. - /// The . - IRequestBuilder WithBody(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); - - /// - /// WithBody: func (string) - /// - /// The function. - /// The . - IRequestBuilder WithBody([NotNull] Func func); - - /// - /// WithBody: func (byte[]) - /// - /// The function. - /// The . - IRequestBuilder WithBody([NotNull] Func func); - - /// - /// WithBody: func (json object) - /// - /// The function. - /// The . - IRequestBuilder WithBody([NotNull] Func func); - - /// - /// WithBody: func (BodyData object) - /// - /// The function. - /// The . - IRequestBuilder WithBody([NotNull] Func func); - } +namespace WireMock.RequestBuilders +{ + /// + /// The BodyRequestBuilder interface. + /// + public interface IBodyRequestBuilder : IRequestMatcher + { + /// + /// WithBody: IMatcher + /// + /// The matcher. + /// The . + IRequestBuilder WithBody([NotNull] IMatcher matcher); + + /// + /// WithBody: IMatcher[] + /// + /// The matchers. + /// The . + IRequestBuilder WithBody([NotNull] IMatcher[] matchers); + + /// + /// WithBody: Body as string + /// + /// The body. + /// The match behaviour. + /// The . + IRequestBuilder WithBody(string body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); + + /// + /// WithBody: Body as byte[] + /// + /// The body. + /// The match behaviour. + /// The . + IRequestBuilder WithBody(byte[] body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); + + /// + /// WithBody: Body as object + /// + /// The body. + /// The match behaviour. + /// The . + IRequestBuilder WithBody(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); + + /// + /// WithBody: func (string) + /// + /// The function. + /// The . + IRequestBuilder WithBody([NotNull] Func func); + + /// + /// WithBody: func (byte[]) + /// + /// The function. + /// The . + IRequestBuilder WithBody([NotNull] Func func); + + /// + /// WithBody: func (json object) + /// + /// The function. + /// The . + IRequestBuilder WithBody([NotNull] Func func); + + /// + /// WithBody: func (BodyData object) + /// + /// The function. + /// The . + IRequestBuilder WithBody([NotNull] Func func); + } } \ No newline at end of file diff --git a/src/WireMock.Net/Serialization/MatcherMapper.cs b/src/WireMock.Net/Serialization/MatcherMapper.cs index 8c6b703b..48090d3d 100644 --- a/src/WireMock.Net/Serialization/MatcherMapper.cs +++ b/src/WireMock.Net/Serialization/MatcherMapper.cs @@ -161,12 +161,12 @@ namespace WireMock.Serialization // If the matcher is a IValueMatcher, get the value (can be string or object). case IValueMatcher valueMatcher: - model.Patterns = new[] { valueMatcher.Value }; + model.Pattern = valueMatcher.Value; break; // If the matcher is a ExactObjectMatcher, get the ValueAsObject or ValueAsBytes. case ExactObjectMatcher exactObjectMatcher: - model.Patterns = new[] { exactObjectMatcher.ValueAsObject ?? exactObjectMatcher.ValueAsBytes }; + model.Pattern = exactObjectMatcher.ValueAsObject ?? exactObjectMatcher.ValueAsBytes; break; } diff --git a/test/WireMock.Net.Tests/Serialization/MappingConverterTests.cs b/test/WireMock.Net.Tests/Serialization/MappingConverterTests.cs index 2ecd797e..40c31e91 100644 --- a/test/WireMock.Net.Tests/Serialization/MappingConverterTests.cs +++ b/test/WireMock.Net.Tests/Serialization/MappingConverterTests.cs @@ -164,7 +164,7 @@ namespace WireMock.Net.Tests.Serialization } [Fact] - public void ToMappingModel_WithTimeSetrtings_ReturnsCorrectTimeSettings() + public void ToMappingModel_WithTimeSettings_ReturnsCorrectTimeSettings() { // Assign var start = DateTime.Now; diff --git a/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs b/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs index bb3b67bd..69f87a6e 100644 --- a/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs +++ b/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs @@ -187,16 +187,36 @@ namespace WireMock.Net.Tests.Serialization } [Fact] - public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_As_String() + public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_1_Value_As_String() + { + // Assign + var pattern = "{ \"post1\": \"value1\", \"post2\": \"value2\" }"; + var patterns = new[] { pattern }; + var model = new MatcherModel + { + Name = "JsonMatcher", + Patterns = patterns + }; + + // Act + var matcher = (JsonMatcher)_sut.Map(model); + + // Assert + matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch); + matcher.Value.Should().BeEquivalentTo(patterns); + } + + [Fact] + public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_2_Values_As_String() { // Assign var pattern1 = "{ \"AccountIds\": [ 1, 2, 3 ] }"; - var pattern2 = "{ \"X\": \"x\" }"; + var pattern2 = "{ \"post1\": \"value1\", \"post2\": \"value2\" }"; var patterns = new[] { pattern1, pattern2 }; var model = new MatcherModel { Name = "JsonMatcher", - Pattern = patterns + Patterns = patterns }; // Act @@ -227,11 +247,31 @@ namespace WireMock.Net.Tests.Serialization } [Fact] - public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_As_Object() + public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_1_Value_As_Object() + { + // Assign + object pattern = new { post1 = "value1", post2 = "value2" }; + var patterns = new[] { pattern }; + var model = new MatcherModel + { + Name = "JsonMatcher", + Patterns = patterns + }; + + // Act + var matcher = (JsonMatcher)_sut.Map(model); + + // Assert + matcher.MatchBehaviour.Should().Be(MatchBehaviour.AcceptOnMatch); + matcher.Value.Should().BeEquivalentTo(patterns); + } + + [Fact] + public void MatcherMapper_Map_MatcherModel_JsonMatcher_Patterns_2_Values_As_Object() { // Assign object pattern1 = new { AccountIds = new[] { 1, 2, 3 } }; - object pattern2 = new { X = "x" }; + object pattern2 = new { post1 = "value1", post2 = "value2" }; var patterns = new[] { pattern1, pattern2 }; var model = new MatcherModel {