Skip to content

Commit 44388ce

Browse files
authored
Fix random delay in mapping json file (#1386)
1 parent 5e25ca7 commit 44388ce

6 files changed

Lines changed: 292 additions & 4 deletions

src/WireMock.Net.Minimal/Serialization/MappingConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,13 @@ public string ToCSharpCode(IMapping mapping, MappingConverterSettings? settings
226226
}
227227
}
228228

229-
if (response.Delay is { })
229+
if (response is { MinimumDelayMilliseconds: > 0, MaximumDelayMilliseconds: > 0 })
230230
{
231-
sb.AppendLine($" .WithDelay({response.Delay.Value.TotalMilliseconds})");
231+
sb.AppendLine($" .WithRandomDelay({response.MinimumDelayMilliseconds}, {response.MaximumDelayMilliseconds})");
232232
}
233-
else if (response is { MinimumDelayMilliseconds: > 0, MaximumDelayMilliseconds: > 0 })
233+
else if (response.Delay is { })
234234
{
235-
sb.AppendLine($" .WithRandomDelay({response.MinimumDelayMilliseconds}, {response.MaximumDelayMilliseconds})");
235+
sb.AppendLine($" .WithDelay({response.Delay.Value.TotalMilliseconds})");
236236
}
237237

238238
if (response.UseTransformer)

test/WireMock.Net.Tests/MappingBuilderTests.GetMappings.verified.txt

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,92 @@
190190
BodyDestination: SameAsSource,
191191
Body: Buy milk
192192
}
193+
},
194+
{
195+
Guid: 98fae52e-76df-47d9-876f-2ee32e931006,
196+
UpdatedAt: 2023-01-14 15:16:17,
197+
Request: {
198+
Path: {
199+
Matchers: [
200+
{
201+
Name: WildcardMatcher,
202+
Pattern: /delay,
203+
IgnoreCase: false
204+
}
205+
]
206+
},
207+
Methods: [
208+
GET
209+
]
210+
},
211+
Response: {
212+
Delay: 1000
213+
}
214+
},
215+
{
216+
Guid: 98fae52e-76df-47d9-876f-2ee32e931007,
217+
UpdatedAt: 2023-01-14 15:16:17,
218+
Request: {
219+
Path: {
220+
Matchers: [
221+
{
222+
Name: WildcardMatcher,
223+
Pattern: /random-delay,
224+
IgnoreCase: false
225+
}
226+
]
227+
},
228+
Methods: [
229+
GET
230+
]
231+
},
232+
Response: {
233+
MinimumRandomDelay: 1234,
234+
MaximumRandomDelay: 60000
235+
}
236+
},
237+
{
238+
Guid: 98fae52e-76df-47d9-876f-2ee32e931008,
239+
UpdatedAt: 2023-01-14 15:16:17,
240+
Request: {
241+
Path: {
242+
Matchers: [
243+
{
244+
Name: WildcardMatcher,
245+
Pattern: /prob,
246+
IgnoreCase: false
247+
}
248+
]
249+
},
250+
Methods: [
251+
GET
252+
]
253+
},
254+
Response: {
255+
StatusCode: 300
256+
},
257+
Probability: 0.1
258+
},
259+
{
260+
Guid: 98fae52e-76df-47d9-876f-2ee32e931009,
261+
UpdatedAt: 2023-01-14 15:16:17,
262+
Request: {
263+
Path: {
264+
Matchers: [
265+
{
266+
Name: WildcardMatcher,
267+
Pattern: /prob,
268+
IgnoreCase: false
269+
}
270+
]
271+
},
272+
Methods: [
273+
GET
274+
]
275+
},
276+
Response: {
277+
StatusCode: 201
278+
},
279+
Probability: 0.9
193280
}
194281
]

test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Builder.verified.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,45 @@ builder
7878
.WithBody("Buy milk")
7979
);
8080

81+
builder
82+
.Given(Request.Create()
83+
.UsingMethod("GET")
84+
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/delay", false, WireMock.Matchers.MatchOperator.Or))
85+
)
86+
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931006")
87+
.RespondWith(Response.Create()
88+
.WithDelay(1000)
89+
);
90+
91+
builder
92+
.Given(Request.Create()
93+
.UsingMethod("GET")
94+
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/random-delay", false, WireMock.Matchers.MatchOperator.Or))
95+
)
96+
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931007")
97+
.RespondWith(Response.Create()
98+
.WithRandomDelay(1234, 60000)
99+
);
100+
101+
builder
102+
.Given(Request.Create()
103+
.UsingMethod("GET")
104+
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/prob", false, WireMock.Matchers.MatchOperator.Or))
105+
)
106+
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931008")
107+
.WithProbability(0.1)
108+
.RespondWith(Response.Create()
109+
.WithStatusCode(300)
110+
);
111+
112+
builder
113+
.Given(Request.Create()
114+
.UsingMethod("GET")
115+
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/prob", false, WireMock.Matchers.MatchOperator.Or))
116+
)
117+
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931009")
118+
.WithProbability(0.9)
119+
.RespondWith(Response.Create()
120+
.WithStatusCode(201)
121+
);
122+

test/WireMock.Net.Tests/MappingBuilderTests.ToCSharpCode_Server.verified.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,45 @@ server
7878
.WithBody("Buy milk")
7979
);
8080

81+
server
82+
.Given(Request.Create()
83+
.UsingMethod("GET")
84+
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/delay", false, WireMock.Matchers.MatchOperator.Or))
85+
)
86+
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931006")
87+
.RespondWith(Response.Create()
88+
.WithDelay(1000)
89+
);
90+
91+
server
92+
.Given(Request.Create()
93+
.UsingMethod("GET")
94+
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/random-delay", false, WireMock.Matchers.MatchOperator.Or))
95+
)
96+
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931007")
97+
.RespondWith(Response.Create()
98+
.WithRandomDelay(1234, 60000)
99+
);
100+
101+
server
102+
.Given(Request.Create()
103+
.UsingMethod("GET")
104+
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/prob", false, WireMock.Matchers.MatchOperator.Or))
105+
)
106+
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931008")
107+
.WithProbability(0.1)
108+
.RespondWith(Response.Create()
109+
.WithStatusCode(300)
110+
);
111+
112+
server
113+
.Given(Request.Create()
114+
.UsingMethod("GET")
115+
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/prob", false, WireMock.Matchers.MatchOperator.Or))
116+
)
117+
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931009")
118+
.WithProbability(0.9)
119+
.RespondWith(Response.Create()
120+
.WithStatusCode(201)
121+
);
122+

test/WireMock.Net.Tests/MappingBuilderTests.ToJson.verified.txt

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,92 @@
186186
BodyDestination: SameAsSource,
187187
Body: Buy milk
188188
}
189+
},
190+
{
191+
Guid: 98fae52e-76df-47d9-876f-2ee32e931006,
192+
UpdatedAt: 2023-01-14T15:16:17,
193+
Request: {
194+
Path: {
195+
Matchers: [
196+
{
197+
Name: WildcardMatcher,
198+
Pattern: /delay,
199+
IgnoreCase: false
200+
}
201+
]
202+
},
203+
Methods: [
204+
GET
205+
]
206+
},
207+
Response: {
208+
Delay: 1000
209+
}
210+
},
211+
{
212+
Guid: 98fae52e-76df-47d9-876f-2ee32e931007,
213+
UpdatedAt: 2023-01-14T15:16:17,
214+
Request: {
215+
Path: {
216+
Matchers: [
217+
{
218+
Name: WildcardMatcher,
219+
Pattern: /random-delay,
220+
IgnoreCase: false
221+
}
222+
]
223+
},
224+
Methods: [
225+
GET
226+
]
227+
},
228+
Response: {
229+
MinimumRandomDelay: 1234,
230+
MaximumRandomDelay: 60000
231+
}
232+
},
233+
{
234+
Guid: 98fae52e-76df-47d9-876f-2ee32e931008,
235+
UpdatedAt: 2023-01-14T15:16:17,
236+
Request: {
237+
Path: {
238+
Matchers: [
239+
{
240+
Name: WildcardMatcher,
241+
Pattern: /prob,
242+
IgnoreCase: false
243+
}
244+
]
245+
},
246+
Methods: [
247+
GET
248+
]
249+
},
250+
Response: {
251+
StatusCode: 300
252+
},
253+
Probability: 0.1
254+
},
255+
{
256+
Guid: 98fae52e-76df-47d9-876f-2ee32e931009,
257+
UpdatedAt: 2023-01-14T15:16:17,
258+
Request: {
259+
Path: {
260+
Matchers: [
261+
{
262+
Name: WildcardMatcher,
263+
Pattern: /prob,
264+
IgnoreCase: false
265+
}
266+
]
267+
},
268+
Methods: [
269+
GET
270+
]
271+
},
272+
Response: {
273+
StatusCode: 201
274+
},
275+
Probability: 0.9
189276
}
190277
]

test/WireMock.Net.Tests/MappingBuilderTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#if !(NET452 || NET461 || NETCOREAPP3_1)
44
using System;
5+
using System.Net;
56
using System.Threading.Tasks;
67
using Moq;
78
using VerifyTests;
@@ -118,6 +119,35 @@ public MappingBuilderTests()
118119
.RespondWith(Response.Create()
119120
.WithBody("Buy milk"));
120121

122+
_sut.Given(Request.Create()
123+
.WithPath("/delay")
124+
.UsingGet()
125+
).RespondWith(Response.Create()
126+
.WithDelay(1000)
127+
);
128+
129+
_sut.Given(Request.Create()
130+
.WithPath("/random-delay")
131+
.UsingGet()
132+
).RespondWith(Response.Create()
133+
.WithRandomDelay(1234)
134+
);
135+
136+
_sut.Given(Request.Create()
137+
.WithPath("/prob")
138+
.UsingGet()
139+
).WithProbability(0.1)
140+
.RespondWith(Response.Create()
141+
.WithStatusCode(HttpStatusCode.Ambiguous)
142+
);
143+
_sut.Given(Request.Create()
144+
.WithPath("/prob")
145+
.UsingGet()
146+
).WithProbability(0.9)
147+
.RespondWith(Response.Create()
148+
.WithStatusCode(HttpStatusCode.Created)
149+
);
150+
121151
_numMappings = _sut.GetMappings().Length;
122152
}
123153

0 commit comments

Comments
 (0)