mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-29 13:52:08 +02:00
Delay
This commit is contained in:
@@ -54,5 +54,13 @@ namespace WireMock.Admin.Mappings
|
||||
/// The headers.
|
||||
/// </value>
|
||||
public IDictionary<string, string> Headers { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the delay in milliseconds.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The delay in milliseconds.
|
||||
/// </value>
|
||||
public int? Delay { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using JetBrains.Annotations;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
|
||||
namespace WireMock.RequestBuilders
|
||||
{
|
||||
@@ -13,45 +12,35 @@ namespace WireMock.RequestBuilders
|
||||
/// <summary>
|
||||
/// The with body.
|
||||
/// </summary>
|
||||
/// <param name="matcher">
|
||||
/// The matcher.
|
||||
/// </param>
|
||||
/// <param name="matcher">The matcher.</param>
|
||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||
IRequestBuilder WithBody([NotNull] IMatcher matcher);
|
||||
|
||||
/// <summary>
|
||||
/// The with body.
|
||||
/// </summary>
|
||||
/// <param name="body">
|
||||
/// The body.
|
||||
/// </param>
|
||||
/// <param name="body">The body.</param>
|
||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||
IRequestBuilder WithBody(string body);
|
||||
|
||||
/// <summary>
|
||||
/// The with body byte[].
|
||||
/// </summary>
|
||||
/// <param name="body">
|
||||
/// The body as byte[].
|
||||
/// </param>
|
||||
/// <param name="body">The body as byte[].</param>
|
||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||
IRequestBuilder WithBody(byte[] body);
|
||||
|
||||
/// <summary>
|
||||
/// The with body string func.
|
||||
/// </summary>
|
||||
/// <param name="body">
|
||||
/// The body string function.
|
||||
/// </param>
|
||||
/// <param name="body">The body string function.</param>
|
||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||
IRequestBuilder WithBody(Func<string, bool> body);
|
||||
|
||||
/// <summary>
|
||||
/// The with body byte[] func.
|
||||
/// </summary>
|
||||
/// <param name="body">
|
||||
/// The body byte[] function.
|
||||
/// </param>
|
||||
/// <param name="body">The body byte[] function.</param>
|
||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||
IRequestBuilder WithBody(Func<byte[], bool> body);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using JetBrains.Annotations;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.RequestBuilders
|
||||
@@ -14,12 +13,8 @@ namespace WireMock.RequestBuilders
|
||||
/// <summary>
|
||||
/// The with parameters.
|
||||
/// </summary>
|
||||
/// <param name="key">
|
||||
/// The key.
|
||||
/// </param>
|
||||
/// <param name="values">
|
||||
/// The values.
|
||||
/// </param>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <param name="values">The values.</param>
|
||||
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
|
||||
IRequestBuilder WithParam([NotNull] string key, params string[] values);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace WireMock.ResponseBuilders
|
||||
{
|
||||
@@ -8,10 +9,17 @@ namespace WireMock.ResponseBuilders
|
||||
public interface IDelayResponseBuilder : IResponseProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// The after delay.
|
||||
/// The with delay.
|
||||
/// </summary>
|
||||
/// <param name="delay">The delay.</param>
|
||||
/// <param name="delay">The TimeSpan to delay.</param>
|
||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithDelay(TimeSpan delay);
|
||||
|
||||
/// <summary>
|
||||
/// The with delay.
|
||||
/// </summary>
|
||||
/// <param name="milliseconds">The milliseconds to delay.</param>
|
||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithDelay(int milliseconds);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -17,7 +15,10 @@ namespace WireMock.ResponseBuilders
|
||||
/// </summary>
|
||||
public class Response : IResponseBuilder
|
||||
{
|
||||
private TimeSpan _delay = TimeSpan.Zero;
|
||||
/// <summary>
|
||||
/// The delay
|
||||
/// </summary>
|
||||
public TimeSpan? Delay { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether [use transformer].
|
||||
@@ -160,7 +161,7 @@ namespace WireMock.ResponseBuilders
|
||||
{
|
||||
Check.NotNull(body, nameof(body));
|
||||
|
||||
ResponseMessage.Body = JsonConvert.SerializeObject(body, new JsonSerializerSettings { Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore } );
|
||||
ResponseMessage.Body = JsonConvert.SerializeObject(body, new JsonSerializerSettings { Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore });
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -191,20 +192,27 @@ namespace WireMock.ResponseBuilders
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The after delay.
|
||||
/// The with delay.
|
||||
/// </summary>
|
||||
/// <param name="delay">
|
||||
/// The delay.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="IResponseProvider"/>.
|
||||
/// </returns>
|
||||
/// <param name="delay">The TimeSpan to delay.</param>
|
||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||
public IResponseBuilder WithDelay(TimeSpan delay)
|
||||
{
|
||||
_delay = delay;
|
||||
Check.Condition(delay, d => d > TimeSpan.Zero, nameof(delay));
|
||||
Delay = delay;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The with delay.
|
||||
/// </summary>
|
||||
/// <param name="milliseconds">The milliseconds to delay.</param>
|
||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||
public IResponseBuilder WithDelay(int milliseconds)
|
||||
{
|
||||
return WithDelay(TimeSpan.FromMilliseconds(milliseconds));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The provide response.
|
||||
/// </summary>
|
||||
@@ -243,7 +251,8 @@ namespace WireMock.ResponseBuilders
|
||||
responseMessage = ResponseMessage;
|
||||
}
|
||||
|
||||
await Task.Delay(_delay);
|
||||
if (Delay != null)
|
||||
await Task.Delay(Delay.Value);
|
||||
|
||||
return responseMessage;
|
||||
}
|
||||
|
||||
@@ -282,6 +282,10 @@ namespace WireMock.Server
|
||||
|
||||
if (mappingModel.Response.UseTransformer)
|
||||
responseBuilder = responseBuilder.WithTransformer();
|
||||
|
||||
if (mappingModel.Response.Delay != null)
|
||||
responseBuilder = responseBuilder.WithDelay(mappingModel.Response.Delay.Value);
|
||||
|
||||
return responseBuilder;
|
||||
}
|
||||
|
||||
@@ -332,7 +336,8 @@ namespace WireMock.Server
|
||||
StatusCode = response.ResponseMessage.StatusCode,
|
||||
Headers = response.ResponseMessage.Headers,
|
||||
Body = response.ResponseMessage.Body,
|
||||
UseTransformer = response.UseTransformer
|
||||
UseTransformer = response.UseTransformer,
|
||||
Delay = response.Delay?.Milliseconds
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user