mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-28 12:22:14 +01:00
Generate C# code from Mapping (#842)
* 1 * . * v * . * . * - * b * res b * Fix UT * . * Verify * v * ... * . * . * dir * m
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
{
|
||||
Request: {
|
||||
ClientIP: ::1,
|
||||
Path: /,
|
||||
AbsolutePath: /,
|
||||
Url: http://localhost/,
|
||||
AbsoluteUrl: http://localhost/,
|
||||
Method: post,
|
||||
BodyAsBytes: AA==,
|
||||
DetectedBodyType: Bytes
|
||||
},
|
||||
Response: {
|
||||
BodyAsBytes: AA==,
|
||||
DetectedBodyType: Bytes
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
Request: {
|
||||
ClientIP: ::1,
|
||||
Path: /,
|
||||
AbsolutePath: /,
|
||||
Url: http://localhost/,
|
||||
AbsoluteUrl: http://localhost/,
|
||||
Method: get
|
||||
},
|
||||
Response: {
|
||||
BodyAsFile: test,
|
||||
DetectedBodyType: File
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
Request: {
|
||||
ClientIP: ::1,
|
||||
Path: /,
|
||||
AbsolutePath: /,
|
||||
Url: http://localhost/,
|
||||
AbsoluteUrl: http://localhost/,
|
||||
Method: post,
|
||||
BodyAsBytes: AA==,
|
||||
DetectedBodyType: Bytes
|
||||
},
|
||||
Response: {
|
||||
Body: Func<IRequestMessage, string>,
|
||||
DetectedBodyType: String
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
Request: {
|
||||
ClientIP: ::1,
|
||||
Path: /,
|
||||
AbsolutePath: /,
|
||||
Url: http://localhost/,
|
||||
AbsoluteUrl: http://localhost/,
|
||||
Method: get
|
||||
},
|
||||
Response: {
|
||||
BodyAsFile: test,
|
||||
DetectedBodyType: File,
|
||||
FaultType: EMPTY_RESPONSE,
|
||||
FaultPercentage: 0.5
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
using FluentAssertions;
|
||||
using NFluent;
|
||||
#if !(NET452 || NET461)
|
||||
using System.Threading.Tasks;
|
||||
using VerifyTests;
|
||||
using VerifyXunit;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Models;
|
||||
using WireMock.Net.Tests.VerifyExtensions;
|
||||
using WireMock.Owin;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Serialization;
|
||||
@@ -11,8 +14,15 @@ using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Serialization;
|
||||
|
||||
[UsesVerify]
|
||||
public class LogEntryMapperTests
|
||||
{
|
||||
private static readonly VerifySettings VerifySettings = new();
|
||||
static LogEntryMapperTests()
|
||||
{
|
||||
VerifySettings.Init<LogEntryMapperTests>();
|
||||
}
|
||||
|
||||
private readonly IWireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
|
||||
|
||||
private readonly LogEntryMapper _sut;
|
||||
@@ -23,7 +33,7 @@ public class LogEntryMapperTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LogEntryMapper_Map_LogEntry_Check_BodyTypeBytes()
|
||||
public Task LogEntryMapper_Map_LogEntry_Check_BodyTypeBytes()
|
||||
{
|
||||
// Assign
|
||||
var logEntry = new LogEntry
|
||||
@@ -51,23 +61,12 @@ public class LogEntryMapperTests
|
||||
// Act
|
||||
var result = _sut.Map(logEntry);
|
||||
|
||||
// Assert
|
||||
Check.That(result.Request.DetectedBodyType).IsEqualTo("Bytes");
|
||||
Check.That(result.Request.DetectedBodyTypeFromContentType).IsNull();
|
||||
Check.That(result.Request.BodyAsBytes).ContainsExactly(new byte[] { 0 });
|
||||
Check.That(result.Request.Body).IsNull();
|
||||
Check.That(result.Request.BodyAsJson).IsNull();
|
||||
|
||||
Check.That(result.Response.DetectedBodyType).IsEqualTo(BodyType.Bytes);
|
||||
Check.That(result.Response.DetectedBodyTypeFromContentType).IsNull();
|
||||
Check.That(result.Response.BodyAsBytes).ContainsExactly(new byte[] { 0 });
|
||||
Check.That(result.Response.Body).IsNull();
|
||||
Check.That(result.Response.BodyAsJson).IsNull();
|
||||
Check.That(result.Response.BodyAsFile).IsNull();
|
||||
// Verify
|
||||
return Verifier.Verify(result, VerifySettings);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LogEntryMapper_Map_LogEntry_Check_ResponseBodyTypeFile()
|
||||
public Task LogEntryMapper_Map_LogEntry_Check_ResponseBodyTypeFile()
|
||||
{
|
||||
// Assign
|
||||
var logEntry = new LogEntry
|
||||
@@ -86,23 +85,12 @@ public class LogEntryMapperTests
|
||||
// Act
|
||||
var result = _sut.Map(logEntry);
|
||||
|
||||
// Assert
|
||||
Check.That(result.Request.DetectedBodyType).IsNull();
|
||||
Check.That(result.Request.DetectedBodyTypeFromContentType).IsNull();
|
||||
Check.That(result.Request.BodyAsBytes).IsNull();
|
||||
Check.That(result.Request.Body).IsNull();
|
||||
Check.That(result.Request.BodyAsJson).IsNull();
|
||||
|
||||
Check.That(result.Response.DetectedBodyType).IsEqualTo(BodyType.File);
|
||||
Check.That(result.Response.DetectedBodyTypeFromContentType).IsNull();
|
||||
Check.That(result.Request.BodyAsBytes).IsNull();
|
||||
Check.That(result.Response.Body).IsNull();
|
||||
Check.That(result.Response.BodyAsJson).IsNull();
|
||||
Check.That(result.Response.BodyAsFile).IsEqualTo("test");
|
||||
// Verify
|
||||
return Verifier.Verify(result, VerifySettings);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LogEntryMapper_Map_LogEntry_WithFault()
|
||||
public Task LogEntryMapper_Map_LogEntry_WithFault()
|
||||
{
|
||||
// Assign
|
||||
var logEntry = new LogEntry
|
||||
@@ -123,13 +111,12 @@ public class LogEntryMapperTests
|
||||
// Act
|
||||
var result = _sut.Map(logEntry);
|
||||
|
||||
// Assert
|
||||
result.Response.FaultType.Should().Be("EMPTY_RESPONSE");
|
||||
result.Response.FaultPercentage.Should().Be(0.5);
|
||||
// Verify
|
||||
return Verifier.Verify(result, VerifySettings);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LogEntryMapper_Map_LogEntry_WhenFuncIsUsed_And_DoNotSaveDynamicResponseInLogEntry_Is_True_Should_NotSave_StringResponse()
|
||||
public Task LogEntryMapper_Map_LogEntry_WhenFuncIsUsed_And_DoNotSaveDynamicResponseInLogEntry_Is_True_Should_NotSave_StringResponse()
|
||||
{
|
||||
// Assign
|
||||
var options = new WireMockMiddlewareOptions
|
||||
@@ -163,7 +150,8 @@ public class LogEntryMapperTests
|
||||
// Act
|
||||
var result = new LogEntryMapper(options).Map(logEntry);
|
||||
|
||||
// Assert
|
||||
result.Response.Body.Should().Be(isFuncUsed);
|
||||
// Verify
|
||||
return Verifier.Verify(result, VerifySettings);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,127 @@
|
||||
#if !(NET452 || NET461 || NETCOREAPP3_1)
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using VerifyTests;
|
||||
using VerifyXunit;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Serialization;
|
||||
using WireMock.Types;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Serialization;
|
||||
|
||||
[UsesVerify]
|
||||
public partial class MappingConverterTests
|
||||
{
|
||||
|
||||
[ModuleInitializer]
|
||||
public static void ModuleInitializer()
|
||||
{
|
||||
VerifierSettings.DontScrubGuids();
|
||||
VerifierSettings.DontScrubDateTimes();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ToCSharpCode_With_Builder_And_AddStartIsTrue()
|
||||
{
|
||||
// Assign
|
||||
var mapping = CreateMapping();
|
||||
|
||||
// Act
|
||||
var code = _sut.ToCSharpCode(mapping, new MappingConverterSettings
|
||||
{
|
||||
AddStart = true,
|
||||
ConverterType = MappingConverterType.Builder
|
||||
});
|
||||
|
||||
// Assert
|
||||
code.Should().NotBeEmpty();
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ToCSharpCode_With_Builder_And_AddStartIsFalse()
|
||||
{
|
||||
// Assign
|
||||
var mapping = CreateMapping();
|
||||
|
||||
// Act
|
||||
var code = _sut.ToCSharpCode(mapping, new MappingConverterSettings
|
||||
{
|
||||
AddStart = false,
|
||||
ConverterType = MappingConverterType.Builder
|
||||
});
|
||||
|
||||
// Assert
|
||||
code.Should().NotBeEmpty();
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ToCSharpCode_With_Server_And_AddStartIsTrue()
|
||||
{
|
||||
// Assign
|
||||
var mapping = CreateMapping();
|
||||
|
||||
// Act
|
||||
var code = _sut.ToCSharpCode(mapping, new MappingConverterSettings
|
||||
{
|
||||
AddStart = true,
|
||||
ConverterType = MappingConverterType.Server
|
||||
});
|
||||
|
||||
// Assert
|
||||
code.Should().NotBeEmpty();
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(code);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ToCSharpCode_With_Server_And_AddStartIsFalse()
|
||||
{
|
||||
// Assign
|
||||
var mapping = CreateMapping();
|
||||
|
||||
// Act
|
||||
var code = _sut.ToCSharpCode(mapping, new MappingConverterSettings
|
||||
{
|
||||
AddStart = false,
|
||||
ConverterType = MappingConverterType.Server
|
||||
});
|
||||
|
||||
// Assert
|
||||
code.Should().NotBeEmpty();
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(code);
|
||||
}
|
||||
|
||||
private Mapping CreateMapping()
|
||||
{
|
||||
var guid = new Guid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc");
|
||||
var request = Request.Create()
|
||||
.UsingGet()
|
||||
.WithPath("test_path")
|
||||
.WithParam("q", "42")
|
||||
.WithClientIP("112.123.100.99")
|
||||
.WithHeader("h-key", "h-value")
|
||||
.WithCookie("c-key", "c-value")
|
||||
.WithBody("b");
|
||||
var response = Response.Create()
|
||||
.WithHeader("Keep-Alive", "test")
|
||||
.WithBody("bbb")
|
||||
.WithDelay(12345)
|
||||
.WithTransformer();
|
||||
|
||||
return new Mapping(guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
builder
|
||||
.Given(Request.Create()
|
||||
.UsingMethod("GET")
|
||||
.WithPath("test_path")
|
||||
.WithParam("q", "42")
|
||||
.WithClientIP("112.123.100.99")
|
||||
.WithHeader("h-key", "h-value", true)
|
||||
.WithCookie("c-key", "c-value", true)
|
||||
.WithBody("b")
|
||||
)
|
||||
.WithGuid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc")
|
||||
.RespondWith(Response.Create()
|
||||
.WithHeader("Keep-Alive)", "test")
|
||||
.WithBody("bbb")
|
||||
.WithDelay(12345)
|
||||
.WithTransformer()
|
||||
);
|
||||
@@ -0,0 +1,18 @@
|
||||
var builder = new MappingBuilder();
|
||||
builder
|
||||
.Given(Request.Create()
|
||||
.UsingMethod("GET")
|
||||
.WithPath("test_path")
|
||||
.WithParam("q", "42")
|
||||
.WithClientIP("112.123.100.99")
|
||||
.WithHeader("h-key", "h-value", true)
|
||||
.WithCookie("c-key", "c-value", true)
|
||||
.WithBody("b")
|
||||
)
|
||||
.WithGuid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc")
|
||||
.RespondWith(Response.Create()
|
||||
.WithHeader("Keep-Alive)", "test")
|
||||
.WithBody("bbb")
|
||||
.WithDelay(12345)
|
||||
.WithTransformer()
|
||||
);
|
||||
@@ -0,0 +1,17 @@
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.UsingMethod("GET")
|
||||
.WithPath("test_path")
|
||||
.WithParam("q", "42")
|
||||
.WithClientIP("112.123.100.99")
|
||||
.WithHeader("h-key", "h-value", true)
|
||||
.WithCookie("c-key", "c-value", true)
|
||||
.WithBody("b")
|
||||
)
|
||||
.WithGuid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc")
|
||||
.RespondWith(Response.Create()
|
||||
.WithHeader("Keep-Alive)", "test")
|
||||
.WithBody("bbb")
|
||||
.WithDelay(12345)
|
||||
.WithTransformer()
|
||||
);
|
||||
@@ -0,0 +1,18 @@
|
||||
var server = WireMockServer.Start();
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.UsingMethod("GET")
|
||||
.WithPath("test_path")
|
||||
.WithParam("q", "42")
|
||||
.WithClientIP("112.123.100.99")
|
||||
.WithHeader("h-key", "h-value", true)
|
||||
.WithCookie("c-key", "c-value", true)
|
||||
.WithBody("b")
|
||||
)
|
||||
.WithGuid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc")
|
||||
.RespondWith(Response.Create()
|
||||
.WithHeader("Keep-Alive)", "test")
|
||||
.WithBody("bbb")
|
||||
.WithDelay(12345)
|
||||
.WithTransformer()
|
||||
);
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
Guid: c8eeaf99-d5c4-4341-8543-4597c3fd40d9,
|
||||
UpdatedAt: 2022-12-04 11:12:13,
|
||||
Title: ,
|
||||
Description: ,
|
||||
Priority: 42,
|
||||
Request: {},
|
||||
Response: {
|
||||
Delay: 1000
|
||||
},
|
||||
UseWebhooksFireAndForget: false
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
Guid: c8eeaf99-d5c4-4341-8543-4597c3fd40d9,
|
||||
UpdatedAt: 2022-12-04 11:12:13,
|
||||
Title: ,
|
||||
Description: ,
|
||||
Priority: 42,
|
||||
Request: {},
|
||||
Response: {
|
||||
BodyAsJson: {
|
||||
x: x
|
||||
},
|
||||
UseTransformer: true,
|
||||
TransformerType: Handlebars,
|
||||
TransformerReplaceNodeOptions: Evaluate
|
||||
},
|
||||
UseWebhooksFireAndForget: false
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
Guid: c8eeaf99-d5c4-4341-8543-4597c3fd40d9,
|
||||
UpdatedAt: 2022-12-04 11:12:13,
|
||||
Title: ,
|
||||
Description: ,
|
||||
Priority: 42,
|
||||
Request: {},
|
||||
Response: {
|
||||
MinimumRandomDelay: 1000,
|
||||
MaximumRandomDelay: 2000
|
||||
},
|
||||
UseWebhooksFireAndForget: false
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
Guid: c8eeaf99-d5c4-4341-8543-4597c3fd40d9,
|
||||
UpdatedAt: 2022-12-04 11:12:13,
|
||||
Title: ,
|
||||
Description: ,
|
||||
Priority: 42,
|
||||
Request: {},
|
||||
Response: {
|
||||
MinimumRandomDelay: 1000,
|
||||
MaximumRandomDelay: 60000
|
||||
},
|
||||
UseWebhooksFireAndForget: false
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
Guid: c8eeaf99-d5c4-4341-8543-4597c3fd40d9,
|
||||
UpdatedAt: 2022-12-04 11:12:13,
|
||||
TimeSettings: {
|
||||
Start: 2023-01-14 15:16:17,
|
||||
End: 2023-01-14 15:17:57,
|
||||
TTL: 100
|
||||
},
|
||||
Title: ,
|
||||
Description: ,
|
||||
Priority: 42,
|
||||
Request: {},
|
||||
Response: {},
|
||||
UseWebhooksFireAndForget: false
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
Guid: c8eeaf99-d5c4-4341-8543-4597c3fd40d9,
|
||||
UpdatedAt: 2022-12-04 11:12:13,
|
||||
Title: my-title,
|
||||
Description: my-description,
|
||||
Request: {},
|
||||
Response: {},
|
||||
UseWebhooksFireAndForget: false
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
{
|
||||
Guid: c8eeaf99-d5c4-4341-8543-4597c3fd40d9,
|
||||
UpdatedAt: 2022-12-04 11:12:13,
|
||||
Title: ,
|
||||
Description: ,
|
||||
Request: {},
|
||||
Response: {},
|
||||
Webhooks: [
|
||||
{
|
||||
Request: {
|
||||
Url: https://test1.com,
|
||||
Method: post,
|
||||
Headers: {
|
||||
One: x
|
||||
},
|
||||
Body: 1,
|
||||
TransformerReplaceNodeOptions: EvaluateAndTryToConvert
|
||||
}
|
||||
},
|
||||
{
|
||||
Request: {
|
||||
Url: https://test2.com,
|
||||
Method: post,
|
||||
Headers: {
|
||||
First: x,
|
||||
Second: a, b
|
||||
},
|
||||
Body: 2,
|
||||
TransformerReplaceNodeOptions: EvaluateAndTryToConvert
|
||||
}
|
||||
}
|
||||
],
|
||||
UseWebhooksFireAndForget: true
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
Guid: c8eeaf99-d5c4-4341-8543-4597c3fd40d9,
|
||||
UpdatedAt: 2022-12-04 11:12:13,
|
||||
Title: ,
|
||||
Description: ,
|
||||
Request: {},
|
||||
Response: {},
|
||||
Webhook: {
|
||||
Request: {
|
||||
Url: https://test.com,
|
||||
Method: post,
|
||||
Headers: {
|
||||
Multi: a, b,
|
||||
Single: x
|
||||
},
|
||||
Body: b,
|
||||
TransformerReplaceNodeOptions: EvaluateAndTryToConvert
|
||||
}
|
||||
},
|
||||
UseWebhooksFireAndForget: false
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
#if !(NET452 || NET461 || NETCOREAPP3_1)
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using VerifyTests;
|
||||
using VerifyXunit;
|
||||
using WireMock.Models;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
@@ -13,9 +18,10 @@ using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Serialization;
|
||||
|
||||
public class MappingConverterTests
|
||||
public partial class MappingConverterTests
|
||||
{
|
||||
private readonly DateTime _updatedAt = new(2022, 12, 4);
|
||||
private readonly Guid _guid = new("c8eeaf99-d5c4-4341-8543-4597c3fd40d9");
|
||||
private readonly DateTime _updatedAt = new(2022, 12, 4, 11, 12, 13);
|
||||
private readonly WireMockServerSettings _settings = new();
|
||||
|
||||
private readonly MappingConverter _sut;
|
||||
@@ -26,7 +32,7 @@ public class MappingConverterTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_With_SingleWebHook()
|
||||
public Task ToMappingModel_With_SingleWebHook()
|
||||
{
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
@@ -53,7 +59,7 @@ public class MappingConverterTests
|
||||
}
|
||||
}
|
||||
};
|
||||
var mapping = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, false, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, false, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -74,10 +80,13 @@ public class MappingConverterTests
|
||||
model.Webhook.Request.Headers.Should().HaveCount(2);
|
||||
model.Webhook.Request.Body.Should().Be("b");
|
||||
model.Webhook.Request.BodyAsJson.Should().BeNull();
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_With_MultipleWebHooks()
|
||||
public Task ToMappingModel_With_MultipleWebHooks()
|
||||
{
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
@@ -123,7 +132,7 @@ public class MappingConverterTests
|
||||
}
|
||||
}
|
||||
};
|
||||
var mapping = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, true, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, true, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -148,17 +157,20 @@ public class MappingConverterTests
|
||||
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");
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithTitle_And_Description_ReturnsCorrectModel()
|
||||
public Task 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(), _updatedAt, title, description, null, _settings, request, response, 0, null, null, null, null, null, false, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, title, description, null, _settings, request, response, 0, null, null, null, null, null, false, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -167,15 +179,18 @@ public class MappingConverterTests
|
||||
model.Should().NotBeNull();
|
||||
model.Title.Should().Be(title);
|
||||
model.Description.Should().Be(description);
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithPriority_ReturnsPriority()
|
||||
public Task ToMappingModel_WithPriority_ReturnsPriority()
|
||||
{
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithBodyAsJson(new { x = "x" }).WithTransformer();
|
||||
var mapping = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -184,13 +199,16 @@ public class MappingConverterTests
|
||||
model.Should().NotBeNull();
|
||||
model.Priority.Should().Be(42);
|
||||
model.Response.UseTransformer.Should().BeTrue();
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithTimeSettings_ReturnsCorrectTimeSettings()
|
||||
public Task ToMappingModel_WithTimeSettings_ReturnsCorrectTimeSettings()
|
||||
{
|
||||
// Assign
|
||||
var start = DateTime.Now;
|
||||
var start = new DateTime(2023, 1, 14, 15, 16, 17);
|
||||
var ttl = 100;
|
||||
var end = start.AddSeconds(ttl);
|
||||
var request = Request.Create();
|
||||
@@ -201,7 +219,7 @@ public class MappingConverterTests
|
||||
End = end,
|
||||
TTL = ttl
|
||||
};
|
||||
var mapping = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, timeSettings);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, timeSettings);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -212,6 +230,9 @@ public class MappingConverterTests
|
||||
model.TimeSettings!.Start.Should().Be(start);
|
||||
model.TimeSettings.End.Should().Be(end);
|
||||
model.TimeSettings.TTL.Should().Be(ttl);
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -241,13 +262,13 @@ public class MappingConverterTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithDelayAsMilleSeconds_ReturnsCorrectModel()
|
||||
public Task ToMappingModel_WithDelayAsMilleSeconds_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
var delay = 1000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithDelay(delay);
|
||||
var mapping = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -255,16 +276,19 @@ public class MappingConverterTests
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
model.Response.Delay.Should().Be(delay);
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithRandomMinimumDelay_ReturnsCorrectModel()
|
||||
public Task ToMappingModel_WithRandomMinimumDelay_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
int minimumDelay = 1000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithRandomDelay(minimumDelay);
|
||||
var mapping = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -274,17 +298,20 @@ public class MappingConverterTests
|
||||
model.Response.Delay.Should().BeNull();
|
||||
model.Response.MinimumRandomDelay.Should().Be(minimumDelay);
|
||||
model.Response.MaximumRandomDelay.Should().Be(60_000);
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMappingModel_WithRandomDelay_ReturnsCorrectModel()
|
||||
public Task 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(), _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -294,5 +321,9 @@ public class MappingConverterTests
|
||||
model.Response.Delay.Should().BeNull();
|
||||
model.Response.MinimumRandomDelay.Should().Be(minimumDelay);
|
||||
model.Response.MaximumRandomDelay.Should().Be(maximumDelay);
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,75 @@
|
||||
{
|
||||
Guid: ff55ac0a-fea9-4d7b-be74-5e483a2c1305,
|
||||
UpdatedAt: 2022-12-04,
|
||||
Title: my title,
|
||||
Description: my description,
|
||||
Priority: -2000000,
|
||||
Request: {
|
||||
Path: {
|
||||
Matchers: [
|
||||
{
|
||||
Name: WildcardMatcher,
|
||||
Pattern: x,
|
||||
IgnoreCase: false
|
||||
}
|
||||
]
|
||||
},
|
||||
Methods: [
|
||||
POST
|
||||
],
|
||||
Headers: [
|
||||
{
|
||||
Name: Content-Type,
|
||||
Matchers: [
|
||||
{
|
||||
Name: ContentTypeMatcher,
|
||||
Pattern: text/plain,
|
||||
IgnoreCase: false
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
Cookies: [
|
||||
{
|
||||
Name: c,
|
||||
Matchers: [
|
||||
{
|
||||
Name: WildcardMatcher,
|
||||
Pattern: x,
|
||||
IgnoreCase: true
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
Params: [
|
||||
{
|
||||
Name: p1,
|
||||
Matchers: [
|
||||
{
|
||||
Name: ExactMatcher,
|
||||
Pattern: p1-v,
|
||||
IgnoreCase: false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
Name: p2,
|
||||
Matchers: [
|
||||
{
|
||||
Name: ExactMatcher,
|
||||
Pattern: p2-v,
|
||||
IgnoreCase: false
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
Body: {
|
||||
Matcher: {
|
||||
Name: RegexMatcher,
|
||||
Pattern: <RequestType>Auth</RequestType>,
|
||||
IgnoreCase: false
|
||||
}
|
||||
}
|
||||
},
|
||||
Response: {}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
#if !(NET452 || NET461 || NETCOREAPP3_1)
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using Moq;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using FluentAssertions;
|
||||
using VerifyTests;
|
||||
using VerifyXunit;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.Serialization;
|
||||
@@ -12,6 +14,7 @@ using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Serialization;
|
||||
|
||||
[UsesVerify]
|
||||
public class ProxyMappingConverterTests
|
||||
{
|
||||
private readonly WireMockServerSettings _settings = new();
|
||||
@@ -33,8 +36,15 @@ public class ProxyMappingConverterTests
|
||||
_sut = new ProxyMappingConverter(_settings, guidUtilsMock.Object, dateTimeUtilsMock.Object);
|
||||
}
|
||||
|
||||
[ModuleInitializer]
|
||||
public static void ModuleInitializer()
|
||||
{
|
||||
VerifierSettings.DontScrubGuids();
|
||||
VerifierSettings.DontScrubDateTimes();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToMapping_UseDefinedRequestMatchers_True()
|
||||
public Task ToMapping_UseDefinedRequestMatchers_True()
|
||||
{
|
||||
// Arrange
|
||||
var proxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
@@ -49,7 +59,7 @@ public class ProxyMappingConverterTests
|
||||
.WithParam("p2", "p2-v")
|
||||
.WithHeader("Content-Type", new ContentTypeMatcher("text/plain"))
|
||||
.WithCookie("c", "x")
|
||||
.WithBody(new RegexMatcher("<RequestType>Auth</RequestType"));
|
||||
.WithBody(new RegexMatcher("<RequestType>Auth</RequestType>"));
|
||||
|
||||
var mappingMock = new Mock<IMapping>();
|
||||
mappingMock.SetupGet(m => m.RequestMatcher).Returns(request);
|
||||
@@ -65,9 +75,9 @@ public class ProxyMappingConverterTests
|
||||
|
||||
// Assert
|
||||
var model = _mappingConverter.ToMappingModel(proxyMapping);
|
||||
var json = JsonConvert.SerializeObject(model, JsonSerializationConstants.JsonSerializerSettingsDefault);
|
||||
var expected = File.ReadAllText(Path.Combine("../../../", "Serialization", "files", "proxy.json"));
|
||||
|
||||
json.Should().Be(expected);
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
Request: {
|
||||
Url: https://localhost,
|
||||
Method: get,
|
||||
Headers: {
|
||||
x: [
|
||||
y
|
||||
]
|
||||
},
|
||||
BodyData: {
|
||||
BodyAsJson: {
|
||||
n: 12345
|
||||
},
|
||||
DetectedBodyType: Json,
|
||||
DetectedBodyTypeFromContentType: Bytes
|
||||
},
|
||||
Delay: 4,
|
||||
MinimumRandomDelay: 5,
|
||||
MaximumRandomDelay: 6
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
Request: {
|
||||
Url: https://localhost,
|
||||
Method: get,
|
||||
Headers: {
|
||||
x: [
|
||||
y
|
||||
]
|
||||
},
|
||||
BodyData: {
|
||||
BodyAsString: test,
|
||||
DetectedBodyType: String,
|
||||
DetectedBodyTypeFromContentType: Bytes
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
Request: {
|
||||
Url: https://localhost,
|
||||
Method: get,
|
||||
Headers: {
|
||||
x: [
|
||||
y
|
||||
]
|
||||
},
|
||||
BodyData: {
|
||||
BodyAsString: test,
|
||||
DetectedBodyType: String,
|
||||
DetectedBodyTypeFromContentType: Bytes
|
||||
},
|
||||
UseTransformer: true,
|
||||
TransformerReplaceNodeOptions: Evaluate
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
Request: {
|
||||
Url: https://localhost,
|
||||
Method: get,
|
||||
Headers: {
|
||||
x: y
|
||||
},
|
||||
BodyAsJson: {
|
||||
n: 12345
|
||||
},
|
||||
TransformerReplaceNodeOptions: EvaluateAndTryToConvert,
|
||||
Delay: 4,
|
||||
MinimumRandomDelay: 5,
|
||||
MaximumRandomDelay: 6
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
#if !(NET452 || NET461 || NETCOREAPP3_1)
|
||||
using System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using VerifyTests;
|
||||
using VerifyXunit;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Models;
|
||||
using WireMock.Serialization;
|
||||
@@ -9,10 +13,18 @@ using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Serialization;
|
||||
|
||||
[UsesVerify]
|
||||
public class WebhookMapperTests
|
||||
{
|
||||
[ModuleInitializer]
|
||||
public static void ModuleInitializer()
|
||||
{
|
||||
VerifierSettings.DontScrubGuids();
|
||||
VerifierSettings.DontScrubDateTimes();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebhookMapper_Map_WebhookModel_BodyAsString_And_UseTransformerIsFalse()
|
||||
public Task WebhookMapper_Map_WebhookModel_BodyAsString_And_UseTransformerIsFalse()
|
||||
{
|
||||
// Assign
|
||||
var model = new WebhookModel
|
||||
@@ -32,17 +44,12 @@ public class WebhookMapperTests
|
||||
|
||||
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();
|
||||
// Verify
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebhookMapper_Map_WebhookModel_BodyAsString_And_UseTransformerIsTrue()
|
||||
public Task WebhookMapper_Map_WebhookModel_BodyAsString_And_UseTransformerIsTrue()
|
||||
{
|
||||
// Assign
|
||||
var model = new WebhookModel
|
||||
@@ -62,18 +69,12 @@ public class WebhookMapperTests
|
||||
|
||||
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);
|
||||
// Verify
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebhookMapper_Map_WebhookModel_BodyAsJson()
|
||||
public Task WebhookMapper_Map_WebhookModel_BodyAsJson()
|
||||
{
|
||||
// Assign
|
||||
var model = new WebhookModel
|
||||
@@ -95,19 +96,12 @@ public class WebhookMapperTests
|
||||
|
||||
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);
|
||||
// Verify
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WebhookMapper_Map_Webhook_To_Model()
|
||||
public Task WebhookMapper_Map_Webhook_To_Model()
|
||||
{
|
||||
// Assign
|
||||
var webhook = new Webhook
|
||||
@@ -134,12 +128,8 @@ public class WebhookMapperTests
|
||||
|
||||
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);
|
||||
// Verify
|
||||
return Verifier.Verify(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,75 +0,0 @@
|
||||
{
|
||||
"Guid": "ff55ac0a-fea9-4d7b-be74-5e483a2c1305",
|
||||
"UpdatedAt": "2022-12-04T00:00:00",
|
||||
"Title": "my title",
|
||||
"Description": "my description",
|
||||
"Priority": -2000000,
|
||||
"Request": {
|
||||
"Path": {
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "WildcardMatcher",
|
||||
"Pattern": "x",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
]
|
||||
},
|
||||
"Methods": [
|
||||
"POST"
|
||||
],
|
||||
"Headers": [
|
||||
{
|
||||
"Name": "Content-Type",
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "ContentTypeMatcher",
|
||||
"Pattern": "text/plain",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"Cookies": [
|
||||
{
|
||||
"Name": "c",
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "WildcardMatcher",
|
||||
"Pattern": "x",
|
||||
"IgnoreCase": true
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"Params": [
|
||||
{
|
||||
"Name": "p1",
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "ExactMatcher",
|
||||
"Pattern": "p1-v",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"Name": "p2",
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "ExactMatcher",
|
||||
"Pattern": "p2-v",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"Body": {
|
||||
"Matcher": {
|
||||
"Name": "RegexMatcher",
|
||||
"Pattern": "<RequestType>Auth</RequestType",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"Response": {}
|
||||
}
|
||||
Reference in New Issue
Block a user