mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-17 23:14:23 +01:00
WebHook : UseFireAndForget + Delay (#803)
* UseFireAndForget * ... * delay * async * updated code accorsing to proposal * Change nuget to package reference for WireMock.Net.Console.Net472.Classic, move the new FireAndForget into the main mapping, out of individual webhook mappings making it all or nothing, update tests, change Middleware to await or not the firing of all webhooks. Update models as needed. (#804) Co-authored-by: Matt Philmon <Matt_Philmon@carmax.com> * small update * Tweak middleware and fix bug in example (#806) Co-authored-by: Matt Philmon <Matt_Philmon@carmax.com> * .ConfigureAwait(false) Co-authored-by: mattisking <mattisking@gmail.com> Co-authored-by: Matt Philmon <Matt_Philmon@carmax.com>
This commit is contained in:
@@ -176,7 +176,7 @@ namespace WireMock.Net.Tests.Owin
|
||||
_mappingMock.SetupGet(m => m.Provider).Returns(responseBuilder);
|
||||
_mappingMock.SetupGet(m => m.Settings).Returns(settings);
|
||||
|
||||
var newMappingFromProxy = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, null);
|
||||
var newMappingFromProxy = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null);
|
||||
_mappingMock.Setup(m => m.ProvideResponseAsync(It.IsAny<RequestMessage>())).ReturnsAsync((new ResponseMessage(), newMappingFromProxy));
|
||||
|
||||
var requestBuilder = Request.Create().UsingAnyMethod();
|
||||
@@ -230,7 +230,7 @@ namespace WireMock.Net.Tests.Owin
|
||||
_mappingMock.SetupGet(m => m.Provider).Returns(responseBuilder);
|
||||
_mappingMock.SetupGet(m => m.Settings).Returns(settings);
|
||||
|
||||
var newMappingFromProxy = new Mapping(Guid.NewGuid(), "my-title", "my-description", null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, null);
|
||||
var newMappingFromProxy = new Mapping(Guid.NewGuid(), "my-title", "my-description", null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null);
|
||||
_mappingMock.Setup(m => m.ProvideResponseAsync(It.IsAny<RequestMessage>())).ReturnsAsync((new ResponseMessage(), newMappingFromProxy));
|
||||
|
||||
var requestBuilder = Request.Create().UsingAnyMethod();
|
||||
|
||||
@@ -11,286 +11,287 @@ using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Serialization
|
||||
namespace WireMock.Net.Tests.Serialization;
|
||||
|
||||
public class MappingConverterTests
|
||||
{
|
||||
public class MappingConverterTests
|
||||
private readonly WireMockServerSettings _settings = new();
|
||||
|
||||
private readonly MappingConverter _sut;
|
||||
|
||||
public MappingConverterTests()
|
||||
{
|
||||
private readonly WireMockServerSettings _settings = new WireMockServerSettings();
|
||||
_sut = new MappingConverter(new MatcherMapper(_settings));
|
||||
}
|
||||
|
||||
private readonly MappingConverter _sut;
|
||||
|
||||
public MappingConverterTests()
|
||||
[Fact]
|
||||
public void ToMappingModel_With_SingleWebHook()
|
||||
{
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
var response = Response.Create();
|
||||
var webhooks = new IWebhook[]
|
||||
{
|
||||
_sut = new MappingConverter(new MatcherMapper(_settings));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_With_SingleWebHook()
|
||||
{
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
var response = Response.Create();
|
||||
var webhooks = new IWebhook[]
|
||||
new Webhook
|
||||
{
|
||||
new Webhook
|
||||
Request = new WebhookRequest
|
||||
{
|
||||
Request = new WebhookRequest
|
||||
Url = "https://test.com",
|
||||
Headers = new Dictionary<string, WireMockList<string>>
|
||||
{
|
||||
Url = "https://test.com",
|
||||
Headers = new Dictionary<string, WireMockList<string>>
|
||||
{
|
||||
{ "Single", new WireMockList<string>("x") },
|
||||
{ "Multi", new WireMockList<string>("a", "b") }
|
||||
},
|
||||
Method = "post",
|
||||
BodyData = new BodyData
|
||||
{
|
||||
BodyAsString = "b",
|
||||
DetectedBodyType = BodyType.String,
|
||||
DetectedBodyTypeFromContentType = BodyType.String
|
||||
}
|
||||
{ "Single", new WireMockList<string>("x") },
|
||||
{ "Multi", new WireMockList<string>("a", "b") }
|
||||
},
|
||||
Method = "post",
|
||||
BodyData = new BodyData
|
||||
{
|
||||
BodyAsString = "b",
|
||||
DetectedBodyType = BodyType.String,
|
||||
DetectedBodyTypeFromContentType = BodyType.String
|
||||
}
|
||||
}
|
||||
};
|
||||
var mapping = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Priority.Should().BeNull();
|
||||
|
||||
model.Response.BodyAsJsonIndented.Should().BeNull();
|
||||
model.Response.UseTransformer.Should().BeNull();
|
||||
model.Response.Headers.Should().BeNull();
|
||||
|
||||
model.Webhooks.Should().BeNull();
|
||||
|
||||
model.Webhook.Request.Method.Should().Be("post");
|
||||
model.Webhook.Request.Url.Should().Be("https://test.com");
|
||||
model.Webhook.Request.Headers.Should().HaveCount(2);
|
||||
model.Webhook.Request.Body.Should().Be("b");
|
||||
model.Webhook.Request.BodyAsJson.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_With_MultipleWebHooks()
|
||||
{
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
var response = Response.Create();
|
||||
var webhooks = new IWebhook[]
|
||||
{
|
||||
new Webhook
|
||||
{
|
||||
Request = new WebhookRequest
|
||||
{
|
||||
Url = "https://test1.com",
|
||||
Headers = new Dictionary<string, WireMockList<string>>
|
||||
{
|
||||
{ "One", new WireMockList<string>("x") }
|
||||
},
|
||||
Method = "post",
|
||||
BodyData = new BodyData
|
||||
{
|
||||
BodyAsString = "1",
|
||||
DetectedBodyType = BodyType.String,
|
||||
DetectedBodyTypeFromContentType = BodyType.String
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
new Webhook
|
||||
{
|
||||
Request = new WebhookRequest
|
||||
{
|
||||
Url = "https://test2.com",
|
||||
Headers = new Dictionary<string, WireMockList<string>>
|
||||
{
|
||||
{ "First", new WireMockList<string>("x") },
|
||||
{ "Second", new WireMockList<string>("a", "b") }
|
||||
},
|
||||
Method = "post",
|
||||
BodyData = new BodyData
|
||||
{
|
||||
BodyAsString = "2",
|
||||
DetectedBodyType = BodyType.String,
|
||||
DetectedBodyTypeFromContentType = BodyType.String
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
var mapping = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Priority.Should().BeNull();
|
||||
|
||||
model.Response.BodyAsJsonIndented.Should().BeNull();
|
||||
model.Response.UseTransformer.Should().BeNull();
|
||||
model.Response.Headers.Should().BeNull();
|
||||
|
||||
model.Webhook.Should().BeNull();
|
||||
|
||||
model.Webhooks[0].Request.Method.Should().Be("post");
|
||||
model.Webhooks[0].Request.Url.Should().Be("https://test1.com");
|
||||
model.Webhooks[0].Request.Headers.Should().HaveCount(1);
|
||||
model.Webhooks[0].Request.Body.Should().Be("1");
|
||||
|
||||
model.Webhooks[1].Request.Method.Should().Be("post");
|
||||
model.Webhooks[1].Request.Url.Should().Be("https://test2.com");
|
||||
model.Webhooks[1].Request.Headers.Should().HaveCount(2);
|
||||
model.Webhooks[1].Request.Body.Should().Be("2");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithTitle_And_Description_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
var title = "my-title";
|
||||
var description = "my-description";
|
||||
var request = Request.Create();
|
||||
var response = Response.Create();
|
||||
var mapping = new Mapping(Guid.NewGuid(), title, description, null, _settings, request, response, 0, null, null, null, null, null, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Title.Should().Be(title);
|
||||
model.Description.Should().Be(description);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithPriority_ReturnsPriority()
|
||||
{
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithBodyAsJson(new { x = "x" }).WithTransformer();
|
||||
var mapping = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Priority.Should().Be(42);
|
||||
model.Response.UseTransformer.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithTimeSettings_ReturnsCorrectTimeSettings()
|
||||
{
|
||||
// Assign
|
||||
var start = DateTime.Now;
|
||||
var ttl = 100;
|
||||
var end = start.AddSeconds(ttl);
|
||||
var request = Request.Create();
|
||||
var response = Response.Create();
|
||||
var timeSettings = new TimeSettings
|
||||
{
|
||||
Start = start,
|
||||
End = end,
|
||||
TTL = ttl
|
||||
};
|
||||
var mapping = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, timeSettings);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.TimeSettings.Should().NotBeNull();
|
||||
model.TimeSettings.Start.Should().Be(start);
|
||||
model.TimeSettings.End.Should().Be(end);
|
||||
model.TimeSettings.TTL.Should().Be(ttl);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
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(), string.Empty, string.Empty, string.Empty, _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);
|
||||
}
|
||||
}
|
||||
};
|
||||
var mapping = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, false, null);
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithDelayAsMilleSeconds_ReturnsCorrectModel()
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Priority.Should().BeNull();
|
||||
|
||||
model.Response.BodyAsJsonIndented.Should().BeNull();
|
||||
model.Response.UseTransformer.Should().BeNull();
|
||||
model.Response.Headers.Should().BeNull();
|
||||
model.UseWebhooksFireAndForget.Should().BeFalse();
|
||||
|
||||
model.Webhooks.Should().BeNull();
|
||||
|
||||
model.Webhook!.Request.Method.Should().Be("post");
|
||||
model.Webhook.Request.Url.Should().Be("https://test.com");
|
||||
model.Webhook.Request.Headers.Should().HaveCount(2);
|
||||
model.Webhook.Request.Body.Should().Be("b");
|
||||
model.Webhook.Request.BodyAsJson.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_With_MultipleWebHooks()
|
||||
{
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
var response = Response.Create();
|
||||
var webhooks = new IWebhook[]
|
||||
{
|
||||
new Webhook
|
||||
{
|
||||
Request = new WebhookRequest
|
||||
{
|
||||
Url = "https://test1.com",
|
||||
Headers = new Dictionary<string, WireMockList<string>>
|
||||
{
|
||||
{ "One", new WireMockList<string>("x") }
|
||||
},
|
||||
Method = "post",
|
||||
BodyData = new BodyData
|
||||
{
|
||||
BodyAsString = "1",
|
||||
DetectedBodyType = BodyType.String,
|
||||
DetectedBodyTypeFromContentType = BodyType.String
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
new Webhook
|
||||
{
|
||||
Request = new WebhookRequest
|
||||
{
|
||||
Url = "https://test2.com",
|
||||
Headers = new Dictionary<string, WireMockList<string>>
|
||||
{
|
||||
{ "First", new WireMockList<string>("x") },
|
||||
{ "Second", new WireMockList<string>("a", "b") }
|
||||
},
|
||||
Method = "post",
|
||||
BodyData = new BodyData
|
||||
{
|
||||
BodyAsString = "2",
|
||||
DetectedBodyType = BodyType.String,
|
||||
DetectedBodyTypeFromContentType = BodyType.String
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
var mapping = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, true, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Priority.Should().BeNull();
|
||||
|
||||
model.Response.BodyAsJsonIndented.Should().BeNull();
|
||||
model.Response.UseTransformer.Should().BeNull();
|
||||
model.Response.Headers.Should().BeNull();
|
||||
model.UseWebhooksFireAndForget.Should().BeTrue();
|
||||
|
||||
model.Webhook.Should().BeNull();
|
||||
|
||||
model.Webhooks![0].Request.Method.Should().Be("post");
|
||||
model.Webhooks[0].Request.Url.Should().Be("https://test1.com");
|
||||
model.Webhooks[0].Request.Headers.Should().HaveCount(1);
|
||||
model.Webhooks[0].Request.Body.Should().Be("1");
|
||||
|
||||
model.Webhooks[1].Request.Method.Should().Be("post");
|
||||
model.Webhooks[1].Request.Url.Should().Be("https://test2.com");
|
||||
model.Webhooks[1].Request.Headers.Should().HaveCount(2);
|
||||
model.Webhooks[1].Request.Body.Should().Be("2");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithTitle_And_Description_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
var title = "my-title";
|
||||
var description = "my-description";
|
||||
var request = Request.Create();
|
||||
var response = Response.Create();
|
||||
var mapping = new Mapping(Guid.NewGuid(), title, description, null, _settings, request, response, 0, null, null, null, null, null, false, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Title.Should().Be(title);
|
||||
model.Description.Should().Be(description);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithPriority_ReturnsPriority()
|
||||
{
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithBodyAsJson(new { x = "x" }).WithTransformer();
|
||||
var mapping = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Priority.Should().Be(42);
|
||||
model.Response.UseTransformer.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithTimeSettings_ReturnsCorrectTimeSettings()
|
||||
{
|
||||
// Assign
|
||||
var start = DateTime.Now;
|
||||
var ttl = 100;
|
||||
var end = start.AddSeconds(ttl);
|
||||
var request = Request.Create();
|
||||
var response = Response.Create();
|
||||
var timeSettings = new TimeSettings
|
||||
{
|
||||
Start = start,
|
||||
End = end,
|
||||
TTL = ttl
|
||||
};
|
||||
var mapping = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, timeSettings);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.TimeSettings!.Should().NotBeNull();
|
||||
model.TimeSettings!.Start.Should().Be(start);
|
||||
model.TimeSettings.End.Should().Be(end);
|
||||
model.TimeSettings.TTL.Should().Be(ttl);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
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)
|
||||
{
|
||||
// Assign
|
||||
var delay = 1000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithDelay(delay);
|
||||
var mapping = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, null);
|
||||
var response = Response.Create().WithDelay(test.Delay);
|
||||
var mapping = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, string.Empty, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Response.Delay.Should().Be(delay);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithRandomMinimumDelay_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
int minimumDelay = 1000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithRandomDelay(minimumDelay);
|
||||
var mapping = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, 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().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(), string.Empty, string.Empty, 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().BeNull();
|
||||
model.Response.MinimumRandomDelay.Should().Be(minimumDelay);
|
||||
model.Response.MaximumRandomDelay.Should().Be(maximumDelay);
|
||||
model.Response.Delay.Should().Be(test.Expected);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithDelayAsMilleSeconds_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
var delay = 1000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithDelay(delay);
|
||||
var mapping = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Response.Delay.Should().Be(delay);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithRandomMinimumDelay_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
int minimumDelay = 1000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithRandomDelay(minimumDelay);
|
||||
var mapping = new Mapping(Guid.NewGuid(), string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, 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(), string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, 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);
|
||||
}
|
||||
}
|
||||
@@ -1,101 +1,145 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Models;
|
||||
using WireMock.Serialization;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Serialization
|
||||
namespace WireMock.Net.Tests.Serialization;
|
||||
|
||||
public class WebhookMapperTests
|
||||
{
|
||||
public class WebhookMapperTests
|
||||
[Fact]
|
||||
public void WebhookMapper_Map_WebhookModel_BodyAsString_And_UseTransformerIsFalse()
|
||||
{
|
||||
[Fact]
|
||||
public void WebhookMapper_Map_Model_BodyAsString_And_UseTransformerIsFalse()
|
||||
// Assign
|
||||
var model = new WebhookModel
|
||||
{
|
||||
// Assign
|
||||
var model = new WebhookModel
|
||||
Request = new WebhookRequestModel
|
||||
{
|
||||
Request = new WebhookRequestModel
|
||||
Url = "https://localhost",
|
||||
Method = "get",
|
||||
Headers = new Dictionary<string, string>
|
||||
{
|
||||
Url = "https://localhost",
|
||||
Method = "get",
|
||||
Headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "x", "y" }
|
||||
},
|
||||
Body = "test",
|
||||
UseTransformer = false
|
||||
}
|
||||
};
|
||||
{ "x", "y" }
|
||||
},
|
||||
Body = "test",
|
||||
UseTransformer = false
|
||||
}
|
||||
};
|
||||
|
||||
var result = WebhookMapper.Map(model);
|
||||
var result = WebhookMapper.Map(model);
|
||||
|
||||
result.Request.Url.Should().Be("https://localhost");
|
||||
result.Request.Method.Should().Be("get");
|
||||
result.Request.Headers.Should().HaveCount(1);
|
||||
result.Request.BodyData.BodyAsJson.Should().BeNull();
|
||||
result.Request.BodyData.BodyAsString.Should().Be("test");
|
||||
result.Request.BodyData.DetectedBodyType.Should().Be(BodyType.String);
|
||||
result.Request.UseTransformer.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebhookMapper_Map_Model_BodyAsString_And_UseTransformerIsTrue()
|
||||
{
|
||||
// Assign
|
||||
var model = new WebhookModel
|
||||
{
|
||||
Request = new WebhookRequestModel
|
||||
{
|
||||
Url = "https://localhost",
|
||||
Method = "get",
|
||||
Headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "x", "y" }
|
||||
},
|
||||
Body = "test",
|
||||
UseTransformer = true
|
||||
}
|
||||
};
|
||||
|
||||
var result = WebhookMapper.Map(model);
|
||||
|
||||
result.Request.Url.Should().Be("https://localhost");
|
||||
result.Request.Method.Should().Be("get");
|
||||
result.Request.Headers.Should().HaveCount(1);
|
||||
result.Request.BodyData.BodyAsJson.Should().BeNull();
|
||||
result.Request.BodyData.BodyAsString.Should().Be("test");
|
||||
result.Request.BodyData.DetectedBodyType.Should().Be(BodyType.String);
|
||||
result.Request.UseTransformer.Should().BeTrue();
|
||||
result.Request.TransformerType.Should().Be(TransformerType.Handlebars);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebhookMapper_Map_Model_BodyAsJson()
|
||||
{
|
||||
// Assign
|
||||
var model = new WebhookModel
|
||||
{
|
||||
Request = new WebhookRequestModel
|
||||
{
|
||||
Url = "https://localhost",
|
||||
Method = "get",
|
||||
Headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "x", "y" }
|
||||
},
|
||||
BodyAsJson = new { n = 12345 }
|
||||
}
|
||||
};
|
||||
|
||||
var result = WebhookMapper.Map(model);
|
||||
|
||||
result.Request.Url.Should().Be("https://localhost");
|
||||
result.Request.Method.Should().Be("get");
|
||||
result.Request.Headers.Should().HaveCount(1);
|
||||
result.Request.BodyData.BodyAsString.Should().BeNull();
|
||||
result.Request.BodyData.BodyAsJson.Should().NotBeNull();
|
||||
result.Request.BodyData.DetectedBodyType.Should().Be(BodyType.Json);
|
||||
}
|
||||
result.Request.Url.Should().Be("https://localhost");
|
||||
result.Request.Method.Should().Be("get");
|
||||
result.Request.Headers.Should().HaveCount(1);
|
||||
result.Request.BodyData!.BodyAsJson.Should().BeNull();
|
||||
result.Request.BodyData.BodyAsString.Should().Be("test");
|
||||
result.Request.BodyData.DetectedBodyType.Should().Be(BodyType.String);
|
||||
result.Request.UseTransformer.Should().BeNull();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebhookMapper_Map_WebhookModel_BodyAsString_And_UseTransformerIsTrue()
|
||||
{
|
||||
// Assign
|
||||
var model = new WebhookModel
|
||||
{
|
||||
Request = new WebhookRequestModel
|
||||
{
|
||||
Url = "https://localhost",
|
||||
Method = "get",
|
||||
Headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "x", "y" }
|
||||
},
|
||||
Body = "test",
|
||||
UseTransformer = true
|
||||
}
|
||||
};
|
||||
|
||||
var result = WebhookMapper.Map(model);
|
||||
|
||||
result.Request.Url.Should().Be("https://localhost");
|
||||
result.Request.Method.Should().Be("get");
|
||||
result.Request.Headers.Should().HaveCount(1);
|
||||
result.Request.BodyData!.BodyAsJson.Should().BeNull();
|
||||
result.Request.BodyData.BodyAsString.Should().Be("test");
|
||||
result.Request.BodyData.DetectedBodyType.Should().Be(BodyType.String);
|
||||
result.Request.UseTransformer.Should().BeTrue();
|
||||
result.Request.TransformerType.Should().Be(TransformerType.Handlebars);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebhookMapper_Map_WebhookModel_BodyAsJson()
|
||||
{
|
||||
// Assign
|
||||
var model = new WebhookModel
|
||||
{
|
||||
Request = new WebhookRequestModel
|
||||
{
|
||||
Url = "https://localhost",
|
||||
Method = "get",
|
||||
Headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "x", "y" }
|
||||
},
|
||||
BodyAsJson = new { n = 12345 },
|
||||
Delay = 4,
|
||||
MinimumRandomDelay = 5,
|
||||
MaximumRandomDelay = 6
|
||||
},
|
||||
};
|
||||
|
||||
var result = WebhookMapper.Map(model);
|
||||
|
||||
result.Request.Url.Should().Be("https://localhost");
|
||||
result.Request.Method.Should().Be("get");
|
||||
result.Request.Headers.Should().HaveCount(1);
|
||||
result.Request.BodyData!.BodyAsString.Should().BeNull();
|
||||
result.Request.BodyData.BodyAsJson.Should().NotBeNull();
|
||||
result.Request.BodyData.DetectedBodyType.Should().Be(BodyType.Json);
|
||||
result.Request.Delay.Should().Be(4);
|
||||
result.Request.MinimumRandomDelay.Should().Be(5);
|
||||
result.Request.MaximumRandomDelay.Should().Be(6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebhookMapper_Map_Webhook_To_Model()
|
||||
{
|
||||
// Assign
|
||||
var webhook = new Webhook
|
||||
{
|
||||
Request = new WebhookRequest
|
||||
{
|
||||
Url = "https://localhost",
|
||||
Method = "get",
|
||||
Headers = new Dictionary<string, WireMockList<string>>
|
||||
{
|
||||
{ "x", new WireMockList<string>("y") }
|
||||
},
|
||||
BodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = new { n = 12345 },
|
||||
DetectedBodyType = BodyType.Json,
|
||||
DetectedBodyTypeFromContentType = BodyType.Json
|
||||
},
|
||||
Delay = 4,
|
||||
MinimumRandomDelay = 5,
|
||||
MaximumRandomDelay = 6
|
||||
}
|
||||
};
|
||||
|
||||
var result = WebhookMapper.Map(webhook);
|
||||
|
||||
result.Request.Url.Should().Be("https://localhost");
|
||||
result.Request.Method.Should().Be("get");
|
||||
result.Request.Headers.Should().HaveCount(1);
|
||||
result.Request.BodyAsJson.Should().NotBeNull();
|
||||
result.Request.Delay.Should().Be(4);
|
||||
result.Request.MinimumRandomDelay.Should().Be(5);
|
||||
result.Request.MaximumRandomDelay.Should().Be(6);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user