mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-07-07 05:15:17 +02: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
|
namespace WireMock.ResponseBuilders
|
||||||
{
|
{
|
||||||
@@ -8,14 +8,14 @@ namespace WireMock.ResponseBuilders
|
|||||||
public interface IDelayResponseBuilder : ICallbackResponseBuilder
|
public interface IDelayResponseBuilder : ICallbackResponseBuilder
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The with delay.
|
/// The delay defined as a <see cref="TimeSpan"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="delay">The TimeSpan to delay.</param>
|
/// <param name="delay">The TimeSpan to delay.</param>
|
||||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||||
IResponseBuilder WithDelay(TimeSpan delay);
|
IResponseBuilder WithDelay(TimeSpan delay);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The with delay.
|
/// The delay defined as milliseconds.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="milliseconds">The milliseconds to delay.</param>
|
/// <param name="milliseconds">The milliseconds to delay.</param>
|
||||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||||
|
|||||||
@@ -346,9 +346,7 @@ namespace WireMock.ResponseBuilders
|
|||||||
return WithTransformer(TransformerType.Handlebars, false, options);
|
return WithTransformer(TransformerType.Handlebars, false, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma warning disable CS1574
|
/// <inheritdoc />
|
||||||
/// <inheritdoc cref="ITransformResponseBuilder.WithTransformer(TransformerType, bool, ReplaceNodeOptions)"/>
|
|
||||||
#pragma warning restore CS1574
|
|
||||||
public IResponseBuilder WithTransformer(TransformerType transformerType, bool transformContentFromBodyAsFile = false, ReplaceNodeOptions options = ReplaceNodeOptions.None)
|
public IResponseBuilder WithTransformer(TransformerType transformerType, bool transformContentFromBodyAsFile = false, ReplaceNodeOptions options = ReplaceNodeOptions.None)
|
||||||
{
|
{
|
||||||
UseTransformer = true;
|
UseTransformer = true;
|
||||||
@@ -358,26 +356,26 @@ namespace WireMock.ResponseBuilders
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IDelayResponseBuilder.WithDelay(TimeSpan)"/>
|
/// <inheritdoc />
|
||||||
public IResponseBuilder WithDelay(TimeSpan delay)
|
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;
|
Delay = delay;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IDelayResponseBuilder.WithDelay(int)"/>
|
/// <inheritdoc />
|
||||||
public IResponseBuilder WithDelay(int milliseconds)
|
public IResponseBuilder WithDelay(int milliseconds)
|
||||||
{
|
{
|
||||||
return WithDelay(TimeSpan.FromMilliseconds(milliseconds));
|
return WithDelay(TimeSpan.FromMilliseconds(milliseconds));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IDelayResponseBuilder.WithRandomDelay(int, int)"/>
|
/// <inheritdoc />
|
||||||
public IResponseBuilder WithRandomDelay(int minimumMilliseconds = 0, int maximumMilliseconds = 60_000)
|
public IResponseBuilder WithRandomDelay(int minimumMilliseconds = 0, int maximumMilliseconds = 60_000)
|
||||||
{
|
{
|
||||||
Guard.Condition(minimumMilliseconds, min => min >= 0, nameof(minimumMilliseconds));
|
Guard.Condition(minimumMilliseconds, min => min >= 0);
|
||||||
Guard.Condition(maximumMilliseconds, max => max > minimumMilliseconds, nameof(maximumMilliseconds));
|
Guard.Condition(maximumMilliseconds, max => max > minimumMilliseconds);
|
||||||
|
|
||||||
MinimumDelayMilliseconds = minimumMilliseconds;
|
MinimumDelayMilliseconds = minimumMilliseconds;
|
||||||
MaximumDelayMilliseconds = maximumMilliseconds;
|
MaximumDelayMilliseconds = maximumMilliseconds;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
@@ -90,7 +92,7 @@ namespace WireMock.Serialization
|
|||||||
}
|
}
|
||||||
else
|
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)
|
if (mapping.Webhooks?.Length == 1)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using WireMock.Models;
|
using WireMock.Models;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
@@ -192,10 +193,36 @@ namespace WireMock.Net.Tests.Serialization
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[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
|
// Assign
|
||||||
int delay = 1000;
|
var delay = 1000;
|
||||||
var request = Request.Create();
|
var request = Request.Create();
|
||||||
var response = Response.Create().WithDelay(delay);
|
var response = Response.Create().WithDelay(delay);
|
||||||
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null, null);
|
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null, null);
|
||||||
|
|||||||
Reference in New Issue
Block a user