mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 08:35:15 +01:00
@@ -6,6 +6,7 @@ using WireMock.Logging;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.Models;
|
||||
using WireMock.Owin;
|
||||
using WireMock.Services;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
@@ -14,6 +15,8 @@ namespace WireMock.Net.Tests.Owin;
|
||||
public class MappingMatcherTests
|
||||
{
|
||||
private readonly Mock<IWireMockMiddlewareOptions> _optionsMock;
|
||||
private readonly Mock<IRandomizerDoubleBetween0And1> _randomizerDoubleBetween0And1Mock;
|
||||
|
||||
private readonly MappingMatcher _sut;
|
||||
|
||||
public MappingMatcherTests()
|
||||
@@ -29,7 +32,10 @@ public class MappingMatcherTests
|
||||
loggerMock.Setup(l => l.Error(It.IsAny<string>()));
|
||||
_optionsMock.Setup(o => o.Logger).Returns(loggerMock.Object);
|
||||
|
||||
_sut = new MappingMatcher(_optionsMock.Object);
|
||||
_randomizerDoubleBetween0And1Mock = new Mock<IRandomizerDoubleBetween0And1>();
|
||||
_randomizerDoubleBetween0And1Mock.Setup(r => r.Generate()).Returns(0.0);
|
||||
|
||||
_sut = new MappingMatcher(_optionsMock.Object, _randomizerDoubleBetween0And1Mock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -76,8 +82,8 @@ public class MappingMatcherTests
|
||||
var guid2 = Guid.Parse("00000000-0000-0000-0000-000000000002");
|
||||
var mappings = InitMappings
|
||||
(
|
||||
(guid1, new[] { 0.1 }),
|
||||
(guid2, new[] { 1.0 })
|
||||
(guid1, new[] { 0.1 }, null),
|
||||
(guid2, new[] { 1.0 }, null)
|
||||
);
|
||||
_optionsMock.Setup(o => o.Mappings).Returns(mappings);
|
||||
|
||||
@@ -104,8 +110,8 @@ public class MappingMatcherTests
|
||||
var guid2 = Guid.Parse("00000000-0000-0000-0000-000000000002");
|
||||
var mappings = InitMappings
|
||||
(
|
||||
(guid1, new[] { 0.1 }),
|
||||
(guid2, new[] { 0.9 })
|
||||
(guid1, new[] { 0.1 }, null),
|
||||
(guid2, new[] { 0.9 }, null)
|
||||
);
|
||||
_optionsMock.Setup(o => o.Mappings).Returns(mappings);
|
||||
|
||||
@@ -131,8 +137,8 @@ public class MappingMatcherTests
|
||||
|
||||
_optionsMock.SetupGet(o => o.AllowPartialMapping).Returns(true);
|
||||
var mappings = InitMappings(
|
||||
(guid1, new[] { 0.1 }),
|
||||
(guid2, new[] { 0.9 })
|
||||
(guid1, new[] { 0.1 }, null),
|
||||
(guid2, new[] { 0.9 }, null)
|
||||
);
|
||||
_optionsMock.Setup(o => o.Mappings).Returns(mappings);
|
||||
|
||||
@@ -158,8 +164,8 @@ public class MappingMatcherTests
|
||||
var guid1 = Guid.Parse("00000000-0000-0000-0000-000000000001");
|
||||
var guid2 = Guid.Parse("00000000-0000-0000-0000-000000000002");
|
||||
var mappings = InitMappings(
|
||||
(guid1, new[] { 1.0 }),
|
||||
(guid2, new[] { 1.0, 1.0 })
|
||||
(guid1, new[] { 1.0 }, null),
|
||||
(guid2, new[] { 1.0, 1.0 }, null)
|
||||
);
|
||||
_optionsMock.Setup(o => o.Mappings).Returns(mappings);
|
||||
|
||||
@@ -178,7 +184,31 @@ public class MappingMatcherTests
|
||||
result.Partial.RequestMatchResult.AverageTotalScore.Should().Be(1.0);
|
||||
}
|
||||
|
||||
private static ConcurrentDictionary<Guid, IMapping> InitMappings(params (Guid guid, double[] scores)[] matches)
|
||||
[Fact]
|
||||
public void MappingMatcher_FindBestMatch_WhenProbabilityFailsFirst_ShouldReturnSecondMatch()
|
||||
{
|
||||
// Assign
|
||||
var guid1 = Guid.Parse("00000000-0000-0000-0000-000000000001");
|
||||
var guid2 = Guid.Parse("00000000-0000-0000-0000-000000000002");
|
||||
var mappings = InitMappings
|
||||
(
|
||||
(guid1, new[] { 1.0 }, 1.0),
|
||||
(guid2, new[] { 1.0 }, null)
|
||||
);
|
||||
_optionsMock.Setup(o => o.Mappings).Returns(mappings);
|
||||
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
|
||||
|
||||
// Act
|
||||
var result = _sut.FindBestMatch(request);
|
||||
|
||||
// Assert
|
||||
result.Match.Should().NotBeNull();
|
||||
result.Match!.Mapping.Guid.Should().Be(guid2);
|
||||
result.Match.RequestMatchResult.AverageTotalScore.Should().Be(1.0);
|
||||
}
|
||||
|
||||
private static ConcurrentDictionary<Guid, IMapping> InitMappings(params (Guid guid, double[] scores, double? probability)[] matches)
|
||||
{
|
||||
var mappings = new ConcurrentDictionary<Guid, IMapping>();
|
||||
|
||||
@@ -193,6 +223,8 @@ public class MappingMatcherTests
|
||||
requestMatchResult.AddScore(typeof(object), score);
|
||||
}
|
||||
|
||||
mappingMock.SetupGet(m => m.Probability).Returns(match.probability);
|
||||
|
||||
mappingMock.Setup(m => m.GetRequestMatchResult(It.IsAny<RequestMessage>(), It.IsAny<string>())).Returns(requestMatchResult);
|
||||
|
||||
mappings.TryAdd(match.guid, mappingMock.Object);
|
||||
|
||||
@@ -177,7 +177,7 @@ public class WireMockMiddlewareTests
|
||||
_mappingMock.SetupGet(m => m.Provider).Returns(responseBuilder);
|
||||
_mappingMock.SetupGet(m => m.Settings).Returns(settings);
|
||||
|
||||
var newMappingFromProxy = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null, null);
|
||||
var newMappingFromProxy = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null, null, null);
|
||||
_mappingMock.Setup(m => m.ProvideResponseAsync(It.IsAny<RequestMessage>())).ReturnsAsync((new ResponseMessage(), newMappingFromProxy));
|
||||
|
||||
var requestBuilder = Request.Create().UsingAnyMethod();
|
||||
@@ -231,7 +231,7 @@ public class WireMockMiddlewareTests
|
||||
_mappingMock.SetupGet(m => m.Provider).Returns(responseBuilder);
|
||||
_mappingMock.SetupGet(m => m.Settings).Returns(settings);
|
||||
|
||||
var newMappingFromProxy = new Mapping(Guid.NewGuid(), _updatedAt, "my-title", "my-description", null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null, data: null);
|
||||
var newMappingFromProxy = new Mapping(Guid.NewGuid(), _updatedAt, "my-title", "my-description", null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
_mappingMock.Setup(m => m.ProvideResponseAsync(It.IsAny<RequestMessage>())).ReturnsAsync((new ResponseMessage(), newMappingFromProxy));
|
||||
|
||||
var requestBuilder = Request.Create().UsingAnyMethod();
|
||||
|
||||
@@ -136,7 +136,8 @@ public partial class MappingConverterTests
|
||||
null,
|
||||
false,
|
||||
null,
|
||||
data: null
|
||||
data: null,
|
||||
probability: 0.3
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
.WithBody("b")
|
||||
)
|
||||
.WithGuid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc")
|
||||
.WithProbability(0.3)
|
||||
.RespondWith(Response.Create()
|
||||
.WithHeader("Keep-Alive)", "test")
|
||||
.WithBody("bbb")
|
||||
|
||||
@@ -10,6 +10,7 @@ builder
|
||||
.WithBody("b")
|
||||
)
|
||||
.WithGuid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc")
|
||||
.WithProbability(0.3)
|
||||
.RespondWith(Response.Create()
|
||||
.WithHeader("Keep-Alive)", "test")
|
||||
.WithBody("bbb")
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
.WithBody("b")
|
||||
)
|
||||
.WithGuid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc")
|
||||
.WithProbability(0.3)
|
||||
.RespondWith(Response.Create()
|
||||
.WithHeader("Keep-Alive)", "test")
|
||||
.WithBody("bbb")
|
||||
|
||||
@@ -10,6 +10,7 @@ server
|
||||
.WithBody("b")
|
||||
)
|
||||
.WithGuid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc")
|
||||
.WithProbability(0.3)
|
||||
.RespondWith(Response.Create()
|
||||
.WithHeader("Keep-Alive)", "test")
|
||||
.WithBody("bbb")
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
Guid: Guid_1,
|
||||
UpdatedAt: DateTime_1,
|
||||
Title: ,
|
||||
Description: ,
|
||||
Priority: 42,
|
||||
Request: {},
|
||||
Response: {},
|
||||
UseWebhooksFireAndForget: false,
|
||||
Probability: 0.4
|
||||
}
|
||||
@@ -57,7 +57,7 @@ public partial class MappingConverterTests
|
||||
}
|
||||
}
|
||||
};
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, false, null, data: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, false, null, data: null, probability: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -130,7 +130,7 @@ public partial class MappingConverterTests
|
||||
}
|
||||
}
|
||||
};
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, true, null, data: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, true, null, data: null, probability: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -168,7 +168,7 @@ public partial class MappingConverterTests
|
||||
var description = "my-description";
|
||||
var request = Request.Create();
|
||||
var response = Response.Create();
|
||||
var mapping = new Mapping(_guid, _updatedAt, title, description, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, title, description, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -188,7 +188,7 @@ public partial class MappingConverterTests
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithBodyAsJson(new { x = "x" }).WithTransformer();
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -217,7 +217,7 @@ public partial class MappingConverterTests
|
||||
End = end,
|
||||
TTL = ttl
|
||||
};
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, timeSettings, data: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, timeSettings, data: null, probability: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -248,7 +248,7 @@ public partial class MappingConverterTests
|
||||
{
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithDelay(test.Delay);
|
||||
var mapping = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, string.Empty, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
var mapping = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, string.Empty, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -266,7 +266,7 @@ public partial class MappingConverterTests
|
||||
var delay = 1000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithDelay(delay);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -286,7 +286,7 @@ public partial class MappingConverterTests
|
||||
int minimumDelay = 1000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithRandomDelay(minimumDelay);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -309,7 +309,7 @@ public partial class MappingConverterTests
|
||||
int maximumDelay = 2000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithRandomDelay(minimumDelay, maximumDelay);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -323,5 +323,25 @@ public partial class MappingConverterTests
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ToMappingModel_WithProbability_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
double probability = 0.4;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create();
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: probability);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Probability.Should().Be(0.4);
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests;
|
||||
|
||||
public partial class WireMockServerTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithProbability()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start();
|
||||
server
|
||||
.Given(Request.Create().UsingGet().WithPath("/foo"))
|
||||
.WithProbability(0.5)
|
||||
.RespondWith(Response.Create().WithStatusCode(200));
|
||||
|
||||
server
|
||||
.Given(Request.Create().UsingGet().WithPath("/foo"))
|
||||
.RespondWith(Response.Create().WithStatusCode(500));
|
||||
|
||||
// Act
|
||||
var requestUri = new Uri($"http://localhost:{server.Port}/foo");
|
||||
var response = await server.CreateClient().GetAsync(requestUri).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Assert.True(new[] { HttpStatusCode.OK, HttpStatusCode.InternalServerError }.Contains(response.StatusCode));
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user