mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-26 11:21:51 +01:00
Implement Random Delay (#633)
* implement random delays * fixing CodeFactor issue * fix code comments * ... * UT Co-authored-by: Michael Yarichuk <michael.yarichuk@gmail.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using WireMock.Models;
|
||||
@@ -163,5 +163,61 @@ namespace WireMock.Net.Tests.Serialization
|
||||
model.Priority.Should().Be(42);
|
||||
model.Response.UseTransformer.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithDelay_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
int 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);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Response.Delay.Should().Be(delay);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithRandomMininumDelay_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
int minimumDelay = 1000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithRandomDelay(minimumDelay);
|
||||
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Response.Delay.Should().BeNull();
|
||||
model.Response.MinimumRandomDelay.Should().Be(minimumDelay);
|
||||
model.Response.MaximumRandomDelay.Should().Be(60_000);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithRandomDelay_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
int minimumDelay = 1000;
|
||||
int maximumDelay = 2000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithRandomDelay(minimumDelay, maximumDelay);
|
||||
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Response.Delay.Should().BeNull();
|
||||
model.Response.MinimumRandomDelay.Should().Be(minimumDelay);
|
||||
model.Response.MaximumRandomDelay.Should().Be(maximumDelay);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -115,6 +115,38 @@ namespace WireMock.Net.Tests
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_randomly_delay_responses_for_a_given_route()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.WithPath("/*"))
|
||||
.RespondWith(Response.Create()
|
||||
.WithBody(@"{ msg: ""Hello world!""}")
|
||||
.WithRandomDelay(10, 1000));
|
||||
|
||||
var watch = new Stopwatch();
|
||||
watch.Start();
|
||||
|
||||
var httClient = new HttpClient();
|
||||
async Task<long> ExecuteTimedRequestAsync()
|
||||
{
|
||||
watch.Reset();
|
||||
await httClient.GetStringAsync("http://localhost:" + server.Ports[0] + "/foo");
|
||||
return watch.ElapsedMilliseconds;
|
||||
}
|
||||
|
||||
// Act
|
||||
await ExecuteTimedRequestAsync();
|
||||
await ExecuteTimedRequestAsync();
|
||||
await ExecuteTimedRequestAsync();
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_Should_delay_responses()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user