From 44388ce80dd29d1fec0ff2db9481b3039eb9e12f Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 25 Nov 2025 20:54:06 +0100 Subject: [PATCH] Fix random delay in mapping json file (#1386) --- .../Serialization/MappingConverter.cs | 10 +-- ...ppingBuilderTests.GetMappings.verified.txt | 87 +++++++++++++++++++ ...derTests.ToCSharpCode_Builder.verified.txt | 42 +++++++++ ...lderTests.ToCSharpCode_Server.verified.txt | 42 +++++++++ .../MappingBuilderTests.ToJson.verified.txt | 87 +++++++++++++++++++ .../WireMock.Net.Tests/MappingBuilderTests.cs | 30 +++++++ 6 files changed, 293 insertions(+), 5 deletions(-) diff --git a/src/WireMock.Net.Minimal/Serialization/MappingConverter.cs b/src/WireMock.Net.Minimal/Serialization/MappingConverter.cs index 5819d7cc..3b1a2aef 100644 --- a/src/WireMock.Net.Minimal/Serialization/MappingConverter.cs +++ b/src/WireMock.Net.Minimal/Serialization/MappingConverter.cs @@ -226,14 +226,14 @@ internal class MappingConverter(MatcherMapper mapper) } } - if (response.Delay is { }) - { - sb.AppendLine($" .WithDelay({response.Delay.Value.TotalMilliseconds})"); - } - else if (response is { MinimumDelayMilliseconds: > 0, MaximumDelayMilliseconds: > 0 }) + if (response is { MinimumDelayMilliseconds: > 0, MaximumDelayMilliseconds: > 0 }) { sb.AppendLine($" .WithRandomDelay({response.MinimumDelayMilliseconds}, {response.MaximumDelayMilliseconds})"); } + else if (response.Delay is { }) + { + sb.AppendLine($" .WithDelay({response.Delay.Value.TotalMilliseconds})"); + } if (response.UseTransformer) { diff --git a/test/WireMock.Net.Tests/MappingBuilderTests.GetMappings.verified.txt b/test/WireMock.Net.Tests/MappingBuilderTests.GetMappings.verified.txt index 26e0ecb4..0ca80951 100644 --- a/test/WireMock.Net.Tests/MappingBuilderTests.GetMappings.verified.txt +++ b/test/WireMock.Net.Tests/MappingBuilderTests.GetMappings.verified.txt @@ -190,5 +190,92 @@ BodyDestination: SameAsSource, Body: Buy milk } + }, + { + Guid: 98fae52e-76df-47d9-876f-2ee32e931006, + UpdatedAt: 2023-01-14 15:16:17, + Request: { + Path: { + Matchers: [ + { + Name: WildcardMatcher, + Pattern: /delay, + IgnoreCase: false + } + ] + }, + Methods: [ + GET + ] + }, + Response: { + Delay: 1000 + } + }, + { + Guid: 98fae52e-76df-47d9-876f-2ee32e931007, + UpdatedAt: 2023-01-14 15:16:17, + Request: { + Path: { + Matchers: [ + { + Name: WildcardMatcher, + Pattern: /random-delay, + IgnoreCase: false + } + ] + }, + Methods: [ + GET + ] + }, + Response: { + MinimumRandomDelay: 1234, + MaximumRandomDelay: 60000 + } + }, + { + Guid: 98fae52e-76df-47d9-876f-2ee32e931008, + UpdatedAt: 2023-01-14 15:16:17, + Request: { + Path: { + Matchers: [ + { + Name: WildcardMatcher, + Pattern: /prob, + IgnoreCase: false + } + ] + }, + Methods: [ + GET + ] + }, + Response: { + StatusCode: 300 + }, + Probability: 0.1 + }, + { + Guid: 98fae52e-76df-47d9-876f-2ee32e931009, + UpdatedAt: 2023-01-14 15:16:17, + Request: { + Path: { + Matchers: [ + { + Name: WildcardMatcher, + Pattern: /prob, + IgnoreCase: false + } + ] + }, + Methods: [ + GET + ] + }, + Response: { + StatusCode: 201 + }, + Probability: 0.9 } ] \ No newline at end of file diff --git a/test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Builder.verified.txt b/test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Builder.verified.txt index 51fb86e7..e1ac98de 100644 --- a/test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Builder.verified.txt +++ b/test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Builder.verified.txt @@ -78,3 +78,45 @@ builder .WithBody("Buy milk") ); +builder + .Given(Request.Create() + .UsingMethod("GET") + .WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/delay", false, WireMock.Matchers.MatchOperator.Or)) + ) + .WithGuid("98fae52e-76df-47d9-876f-2ee32e931006") + .RespondWith(Response.Create() + .WithDelay(1000) + ); + +builder + .Given(Request.Create() + .UsingMethod("GET") + .WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/random-delay", false, WireMock.Matchers.MatchOperator.Or)) + ) + .WithGuid("98fae52e-76df-47d9-876f-2ee32e931007") + .RespondWith(Response.Create() + .WithRandomDelay(1234, 60000) + ); + +builder + .Given(Request.Create() + .UsingMethod("GET") + .WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/prob", false, WireMock.Matchers.MatchOperator.Or)) + ) + .WithGuid("98fae52e-76df-47d9-876f-2ee32e931008") + .WithProbability(0.1) + .RespondWith(Response.Create() + .WithStatusCode(300) + ); + +builder + .Given(Request.Create() + .UsingMethod("GET") + .WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/prob", false, WireMock.Matchers.MatchOperator.Or)) + ) + .WithGuid("98fae52e-76df-47d9-876f-2ee32e931009") + .WithProbability(0.9) + .RespondWith(Response.Create() + .WithStatusCode(201) + ); + diff --git a/test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Server.verified.txt b/test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Server.verified.txt index 9f5d82f8..3a8739e2 100644 --- a/test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Server.verified.txt +++ b/test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Server.verified.txt @@ -78,3 +78,45 @@ server .WithBody("Buy milk") ); +server + .Given(Request.Create() + .UsingMethod("GET") + .WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/delay", false, WireMock.Matchers.MatchOperator.Or)) + ) + .WithGuid("98fae52e-76df-47d9-876f-2ee32e931006") + .RespondWith(Response.Create() + .WithDelay(1000) + ); + +server + .Given(Request.Create() + .UsingMethod("GET") + .WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/random-delay", false, WireMock.Matchers.MatchOperator.Or)) + ) + .WithGuid("98fae52e-76df-47d9-876f-2ee32e931007") + .RespondWith(Response.Create() + .WithRandomDelay(1234, 60000) + ); + +server + .Given(Request.Create() + .UsingMethod("GET") + .WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/prob", false, WireMock.Matchers.MatchOperator.Or)) + ) + .WithGuid("98fae52e-76df-47d9-876f-2ee32e931008") + .WithProbability(0.1) + .RespondWith(Response.Create() + .WithStatusCode(300) + ); + +server + .Given(Request.Create() + .UsingMethod("GET") + .WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/prob", false, WireMock.Matchers.MatchOperator.Or)) + ) + .WithGuid("98fae52e-76df-47d9-876f-2ee32e931009") + .WithProbability(0.9) + .RespondWith(Response.Create() + .WithStatusCode(201) + ); + diff --git a/test/WireMock.Net.Tests/MappingBuilderTests.ToJson.verified.txt b/test/WireMock.Net.Tests/MappingBuilderTests.ToJson.verified.txt index 4c0d6c6c..a64193c8 100644 --- a/test/WireMock.Net.Tests/MappingBuilderTests.ToJson.verified.txt +++ b/test/WireMock.Net.Tests/MappingBuilderTests.ToJson.verified.txt @@ -186,5 +186,92 @@ BodyDestination: SameAsSource, Body: Buy milk } + }, + { + Guid: 98fae52e-76df-47d9-876f-2ee32e931006, + UpdatedAt: 2023-01-14T15:16:17, + Request: { + Path: { + Matchers: [ + { + Name: WildcardMatcher, + Pattern: /delay, + IgnoreCase: false + } + ] + }, + Methods: [ + GET + ] + }, + Response: { + Delay: 1000 + } + }, + { + Guid: 98fae52e-76df-47d9-876f-2ee32e931007, + UpdatedAt: 2023-01-14T15:16:17, + Request: { + Path: { + Matchers: [ + { + Name: WildcardMatcher, + Pattern: /random-delay, + IgnoreCase: false + } + ] + }, + Methods: [ + GET + ] + }, + Response: { + MinimumRandomDelay: 1234, + MaximumRandomDelay: 60000 + } + }, + { + Guid: 98fae52e-76df-47d9-876f-2ee32e931008, + UpdatedAt: 2023-01-14T15:16:17, + Request: { + Path: { + Matchers: [ + { + Name: WildcardMatcher, + Pattern: /prob, + IgnoreCase: false + } + ] + }, + Methods: [ + GET + ] + }, + Response: { + StatusCode: 300 + }, + Probability: 0.1 + }, + { + Guid: 98fae52e-76df-47d9-876f-2ee32e931009, + UpdatedAt: 2023-01-14T15:16:17, + Request: { + Path: { + Matchers: [ + { + Name: WildcardMatcher, + Pattern: /prob, + IgnoreCase: false + } + ] + }, + Methods: [ + GET + ] + }, + Response: { + StatusCode: 201 + }, + Probability: 0.9 } ] \ No newline at end of file diff --git a/test/WireMock.Net.Tests/MappingBuilderTests.cs b/test/WireMock.Net.Tests/MappingBuilderTests.cs index 5e445d07..d95ae89a 100644 --- a/test/WireMock.Net.Tests/MappingBuilderTests.cs +++ b/test/WireMock.Net.Tests/MappingBuilderTests.cs @@ -2,6 +2,7 @@ #if !(NET452 || NET461 || NETCOREAPP3_1) using System; +using System.Net; using System.Threading.Tasks; using Moq; using VerifyTests; @@ -118,6 +119,35 @@ public class MappingBuilderTests .RespondWith(Response.Create() .WithBody("Buy milk")); + _sut.Given(Request.Create() + .WithPath("/delay") + .UsingGet() + ).RespondWith(Response.Create() + .WithDelay(1000) + ); + + _sut.Given(Request.Create() + .WithPath("/random-delay") + .UsingGet() + ).RespondWith(Response.Create() + .WithRandomDelay(1234) + ); + + _sut.Given(Request.Create() + .WithPath("/prob") + .UsingGet() + ).WithProbability(0.1) + .RespondWith(Response.Create() + .WithStatusCode(HttpStatusCode.Ambiguous) + ); + _sut.Given(Request.Create() + .WithPath("/prob") + .UsingGet() + ).WithProbability(0.9) + .RespondWith(Response.Create() + .WithStatusCode(HttpStatusCode.Created) + ); + _numMappings = _sut.GetMappings().Length; }