mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
Allow Timeout.InfiniteTimeSpan for WithDelay (#746)
* Update WithDelay * Allow Timeout.InfiniteTimeSpan for WithDelay
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
|
||||
namespace WireMock.ResponseBuilders
|
||||
{
|
||||
@@ -8,14 +8,14 @@ namespace WireMock.ResponseBuilders
|
||||
public interface IDelayResponseBuilder : ICallbackResponseBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// The with delay.
|
||||
/// The delay defined as a <see cref="TimeSpan"/>.
|
||||
/// </summary>
|
||||
/// <param name="delay">The TimeSpan to delay.</param>
|
||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithDelay(TimeSpan delay);
|
||||
|
||||
/// <summary>
|
||||
/// The with delay.
|
||||
/// The delay defined as milliseconds.
|
||||
/// </summary>
|
||||
/// <param name="milliseconds">The milliseconds to delay.</param>
|
||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||
|
||||
@@ -346,9 +346,7 @@ namespace WireMock.ResponseBuilders
|
||||
return WithTransformer(TransformerType.Handlebars, false, options);
|
||||
}
|
||||
|
||||
#pragma warning disable CS1574
|
||||
/// <inheritdoc cref="ITransformResponseBuilder.WithTransformer(TransformerType, bool, ReplaceNodeOptions)"/>
|
||||
#pragma warning restore CS1574
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithTransformer(TransformerType transformerType, bool transformContentFromBodyAsFile = false, ReplaceNodeOptions options = ReplaceNodeOptions.None)
|
||||
{
|
||||
UseTransformer = true;
|
||||
@@ -358,26 +356,26 @@ namespace WireMock.ResponseBuilders
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IDelayResponseBuilder.WithDelay(TimeSpan)"/>
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithDelay(TimeSpan delay)
|
||||
{
|
||||
Guard.Condition(delay, d => d > TimeSpan.Zero, nameof(delay));
|
||||
Guard.Condition(delay, d => d == Timeout.InfiniteTimeSpan || d > TimeSpan.Zero);
|
||||
|
||||
Delay = delay;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IDelayResponseBuilder.WithDelay(int)"/>
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithDelay(int milliseconds)
|
||||
{
|
||||
return WithDelay(TimeSpan.FromMilliseconds(milliseconds));
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IDelayResponseBuilder.WithRandomDelay(int, int)"/>
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithRandomDelay(int minimumMilliseconds = 0, int maximumMilliseconds = 60_000)
|
||||
{
|
||||
Guard.Condition(minimumMilliseconds, min => min >= 0, nameof(minimumMilliseconds));
|
||||
Guard.Condition(maximumMilliseconds, max => max > minimumMilliseconds, nameof(maximumMilliseconds));
|
||||
Guard.Condition(minimumMilliseconds, min => min >= 0);
|
||||
Guard.Condition(maximumMilliseconds, max => max > minimumMilliseconds);
|
||||
|
||||
MinimumDelayMilliseconds = minimumMilliseconds;
|
||||
MaximumDelayMilliseconds = maximumMilliseconds;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Stef.Validation;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Matchers.Request;
|
||||
@@ -90,7 +92,7 @@ namespace WireMock.Serialization
|
||||
}
|
||||
else
|
||||
{
|
||||
mappingModel.Response.Delay = (int?)response.Delay?.TotalMilliseconds;
|
||||
mappingModel.Response.Delay = (int?)(response.Delay == Timeout.InfiniteTimeSpan ? TimeSpan.MaxValue.TotalMilliseconds : response.Delay?.TotalMilliseconds);
|
||||
}
|
||||
|
||||
if (mapping.Webhooks?.Length == 1)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using FluentAssertions;
|
||||
using WireMock.Models;
|
||||
using WireMock.RequestBuilders;
|
||||
@@ -192,10 +193,36 @@ namespace WireMock.Net.Tests.Serialization
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithDelay_ReturnsCorrectModel()
|
||||
public void ToMappingModel_WithDelayAsTimeSpan_ReturnsCorrectModel()
|
||||
{
|
||||
// Arrange
|
||||
var tests = new[]
|
||||
{
|
||||
new { Delay = Timeout.InfiniteTimeSpan, Expected = (int) TimeSpan.MaxValue.TotalMilliseconds },
|
||||
new { Delay = TimeSpan.FromSeconds(1), Expected = 1000},
|
||||
new { Delay = TimeSpan.MaxValue, Expected = (int) TimeSpan.MaxValue.TotalMilliseconds }
|
||||
};
|
||||
|
||||
foreach (var test in tests)
|
||||
{
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithDelay(test.Delay);
|
||||
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Response.Delay.Should().Be(test.Expected);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithDelayAsMilleSeconds_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
int delay = 1000;
|
||||
var delay = 1000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithDelay(delay);
|
||||
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null, null);
|
||||
|
||||
Reference in New Issue
Block a user