This commit is contained in:
Stef Heyenrath
2025-12-07 10:52:17 +01:00
36 changed files with 886 additions and 76 deletions

View File

@@ -13,7 +13,7 @@ public class WireMockServerArgumentsTests
var args = new WireMockServerArguments();
// Assert
args.HttpPort.Should().BeNull();
args.HttpPorts.Should().BeEmpty();
args.AdminUsername.Should().BeNull();
args.AdminPassword.Should().BeNull();
args.ReadStaticMappings.Should().BeFalse();

View File

@@ -3,6 +3,7 @@
using System.Net.Sockets;
using AwesomeAssertions;
using Moq;
using WireMock.Util;
namespace WireMock.Net.Aspire.Tests;
@@ -40,7 +41,21 @@ public class WireMockServerBuilderExtensionsTests
}
[Fact]
public void AddWireMock()
public void AddWireMock_WithInvalidAdditionalUrls_ShouldThrowArgumentException()
{
// Arrange
string[] invalidUrls = { "err" };
var builder = Mock.Of<IDistributedApplicationBuilder>();
// Act
Action act = () => builder.AddWireMock("ValidName", invalidUrls);
// Assert
act.Should().Throw<ArgumentException>().WithMessage("The URL 'err' is not valid.");
}
[Fact]
public void AddWireMockWithPort()
{
// Arrange
var name = $"apiservice{Guid.NewGuid()}";
@@ -65,7 +80,7 @@ public class WireMockServerBuilderExtensionsTests
ReadStaticMappings = true,
WatchStaticMappings = false,
MappingsPath = null,
HttpPort = port
HttpPorts = [port]
});
wiremock.Resource.Annotations.Should().HaveCount(6);
@@ -90,9 +105,90 @@ public class WireMockServerBuilderExtensionsTests
));
wiremock.Resource.Annotations.OfType<EnvironmentCallbackAnnotation>().FirstOrDefault().Should().NotBeNull();
wiremock.Resource.Annotations.OfType<CommandLineArgsCallbackAnnotation>().FirstOrDefault().Should().NotBeNull();
wiremock.Resource.Annotations.OfType<ResourceCommandAnnotation>().FirstOrDefault().Should().NotBeNull();
}
[Fact]
public void AddWireMockWithAdditionalUrls()
{
// Arrange
var name = $"apiservice{Guid.NewGuid()}";
var freePorts = PortUtils.FindFreeTcpPorts(2).ToList();
string[] additionalUrls = { $"http://*:{freePorts[0]}", $"grpc://*:{freePorts[1]}" };
const string username = "admin";
const string password = "test";
var builder = DistributedApplication.CreateBuilder();
// Act
var wiremock = builder
.AddWireMock(name, additionalUrls)
.WithAdminUserNameAndPassword(username, password)
.WithReadStaticMappings();
// Assert
wiremock.Resource.Should().NotBeNull();
wiremock.Resource.Name.Should().Be(name);
wiremock.Resource.Arguments.Should().BeEquivalentTo(new WireMockServerArguments
{
AdminPassword = password,
AdminUsername = username,
ReadStaticMappings = true,
WatchStaticMappings = false,
MappingsPath = null,
HttpPorts = freePorts,
AdditionalUrls = additionalUrls.ToList()
});
wiremock.Resource.Annotations.Should().HaveCount(9);
var containerImageAnnotation = wiremock.Resource.Annotations.OfType<ContainerImageAnnotation>().FirstOrDefault();
containerImageAnnotation.Should().BeEquivalentTo(new ContainerImageAnnotation
{
Image = "sheyenrath/wiremock.net-alpine",
Registry = null,
Tag = "latest"
});
var endpointAnnotations = wiremock.Resource.Annotations.OfType<EndpointAnnotation>().ToArray();
endpointAnnotations.Should().HaveCount(3);
var endpointAnnotationForHttp80 = endpointAnnotations[0];
endpointAnnotationForHttp80.Should().BeEquivalentTo(new EndpointAnnotation(
protocol: ProtocolType.Tcp,
uriScheme: "http",
transport: null,
name: null,
port: null,
targetPort: 80,
isExternal: null,
isProxied: true
));
var endpointAnnotationForHttpFreePort = endpointAnnotations[1];
endpointAnnotationForHttpFreePort.Should().BeEquivalentTo(new EndpointAnnotation(
protocol: ProtocolType.Tcp,
uriScheme: "http",
transport: null,
name: $"http-{freePorts[0]}",
port: freePorts[0],
targetPort: freePorts[0],
isExternal: null,
isProxied: true
));
var endpointAnnotationForGrpcFreePort = endpointAnnotations[2];
endpointAnnotationForGrpcFreePort.Should().BeEquivalentTo(new EndpointAnnotation(
protocol: ProtocolType.Tcp,
uriScheme: "grpc",
transport: null,
name: $"grpc-{freePorts[1]}",
port: freePorts[1],
targetPort: freePorts[1],
isExternal: null,
isProxied: true
));
wiremock.Resource.Annotations.OfType<EnvironmentCallbackAnnotation>().FirstOrDefault().Should().NotBeNull();
wiremock.Resource.Annotations.OfType<CommandLineArgsCallbackAnnotation>().FirstOrDefault().Should().NotBeNull();
wiremock.Resource.Annotations.OfType<ResourceCommandAnnotation>().FirstOrDefault().Should().NotBeNull();
}
}

View File

@@ -190,5 +190,92 @@
BodyDestination: SameAsSource,
Body: Buy milk
}
},
{
Guid: 98fae52e-76df-47d9-876f-2ee32e931006,
UpdatedAt: 2023-01-14 15:16:17,
Request: {
Path: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: /delay,
IgnoreCase: false
}
]
},
Methods: [
GET
]
},
Response: {
Delay: 1000
}
},
{
Guid: 98fae52e-76df-47d9-876f-2ee32e931007,
UpdatedAt: 2023-01-14 15:16:17,
Request: {
Path: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: /random-delay,
IgnoreCase: false
}
]
},
Methods: [
GET
]
},
Response: {
MinimumRandomDelay: 1234,
MaximumRandomDelay: 60000
}
},
{
Guid: 98fae52e-76df-47d9-876f-2ee32e931008,
UpdatedAt: 2023-01-14 15:16:17,
Request: {
Path: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: /prob,
IgnoreCase: false
}
]
},
Methods: [
GET
]
},
Response: {
StatusCode: 300
},
Probability: 0.1
},
{
Guid: 98fae52e-76df-47d9-876f-2ee32e931009,
UpdatedAt: 2023-01-14 15:16:17,
Request: {
Path: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: /prob,
IgnoreCase: false
}
]
},
Methods: [
GET
]
},
Response: {
StatusCode: 201
},
Probability: 0.9
}
]

View File

@@ -78,3 +78,45 @@ builder
.WithBody("Buy milk")
);
builder
.Given(Request.Create()
.UsingMethod("GET")
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/delay", false, WireMock.Matchers.MatchOperator.Or))
)
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931006")
.RespondWith(Response.Create()
.WithDelay(1000)
);
builder
.Given(Request.Create()
.UsingMethod("GET")
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/random-delay", false, WireMock.Matchers.MatchOperator.Or))
)
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931007")
.RespondWith(Response.Create()
.WithRandomDelay(1234, 60000)
);
builder
.Given(Request.Create()
.UsingMethod("GET")
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/prob", false, WireMock.Matchers.MatchOperator.Or))
)
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931008")
.WithProbability(0.1)
.RespondWith(Response.Create()
.WithStatusCode(300)
);
builder
.Given(Request.Create()
.UsingMethod("GET")
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/prob", false, WireMock.Matchers.MatchOperator.Or))
)
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931009")
.WithProbability(0.9)
.RespondWith(Response.Create()
.WithStatusCode(201)
);

View File

@@ -78,3 +78,45 @@ server
.WithBody("Buy milk")
);
server
.Given(Request.Create()
.UsingMethod("GET")
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/delay", false, WireMock.Matchers.MatchOperator.Or))
)
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931006")
.RespondWith(Response.Create()
.WithDelay(1000)
);
server
.Given(Request.Create()
.UsingMethod("GET")
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/random-delay", false, WireMock.Matchers.MatchOperator.Or))
)
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931007")
.RespondWith(Response.Create()
.WithRandomDelay(1234, 60000)
);
server
.Given(Request.Create()
.UsingMethod("GET")
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/prob", false, WireMock.Matchers.MatchOperator.Or))
)
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931008")
.WithProbability(0.1)
.RespondWith(Response.Create()
.WithStatusCode(300)
);
server
.Given(Request.Create()
.UsingMethod("GET")
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/prob", false, WireMock.Matchers.MatchOperator.Or))
)
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931009")
.WithProbability(0.9)
.RespondWith(Response.Create()
.WithStatusCode(201)
);

View File

@@ -186,5 +186,92 @@
BodyDestination: SameAsSource,
Body: Buy milk
}
},
{
Guid: 98fae52e-76df-47d9-876f-2ee32e931006,
UpdatedAt: 2023-01-14T15:16:17,
Request: {
Path: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: /delay,
IgnoreCase: false
}
]
},
Methods: [
GET
]
},
Response: {
Delay: 1000
}
},
{
Guid: 98fae52e-76df-47d9-876f-2ee32e931007,
UpdatedAt: 2023-01-14T15:16:17,
Request: {
Path: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: /random-delay,
IgnoreCase: false
}
]
},
Methods: [
GET
]
},
Response: {
MinimumRandomDelay: 1234,
MaximumRandomDelay: 60000
}
},
{
Guid: 98fae52e-76df-47d9-876f-2ee32e931008,
UpdatedAt: 2023-01-14T15:16:17,
Request: {
Path: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: /prob,
IgnoreCase: false
}
]
},
Methods: [
GET
]
},
Response: {
StatusCode: 300
},
Probability: 0.1
},
{
Guid: 98fae52e-76df-47d9-876f-2ee32e931009,
UpdatedAt: 2023-01-14T15:16:17,
Request: {
Path: {
Matchers: [
{
Name: WildcardMatcher,
Pattern: /prob,
IgnoreCase: false
}
]
},
Methods: [
GET
]
},
Response: {
StatusCode: 201
},
Probability: 0.9
}
]

View File

@@ -2,6 +2,7 @@
#if !(NET452 || NET461 || NETCOREAPP3_1)
using System;
using System.Net;
using System.Threading.Tasks;
using Moq;
using VerifyTests;
@@ -118,6 +119,35 @@ public class MappingBuilderTests
.RespondWith(Response.Create()
.WithBody("Buy milk"));
_sut.Given(Request.Create()
.WithPath("/delay")
.UsingGet()
).RespondWith(Response.Create()
.WithDelay(1000)
);
_sut.Given(Request.Create()
.WithPath("/random-delay")
.UsingGet()
).RespondWith(Response.Create()
.WithRandomDelay(1234)
);
_sut.Given(Request.Create()
.WithPath("/prob")
.UsingGet()
).WithProbability(0.1)
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.Ambiguous)
);
_sut.Given(Request.Create()
.WithPath("/prob")
.UsingGet()
).WithProbability(0.9)
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.Created)
);
_numMappings = _sut.GetMappings().Length;
}

View File

@@ -56,7 +56,7 @@ public partial class TestcontainersTests
var grpcPort = wireMockContainer.GetMappedPublicPort(9090);
grpcPort.Should().BeGreaterThan(0);
var grpcUrl = wireMockContainer.GetMappedPublicUrl(80);
var grpcUrl = wireMockContainer.GetMappedPublicUrl(9090);
grpcUrl.Should().StartWith("http://");
var adminClient = wireMockContainer.CreateWireMockAdminClient();
@@ -149,6 +149,18 @@ public partial class TestcontainersTests
await StopAsync(wireMockContainer);
}
[Fact]
public async Task WireMockContainer_Build_Grpc_ProtoDefinitionAtServerLevel_UsingGrpcGeneratedClient_AndWithWatchStaticMappings()
{
var wireMockContainer = await Given_WireMockContainerWithProtoDefinitionAtServerLevelWithWatchStaticMappingsIsStartedForHttpAndGrpcAsync();
var reply = await When_GrpcClient_Calls_SayHelloAsync(wireMockContainer);
Then_ReplyMessage_Should_BeCorrect(reply);
await StopAsync(wireMockContainer);
}
private static async Task<WireMockContainer> Given_WireMockContainerIsStartedForHttpAndGrpcAsync()
{
var wireMockContainer = new WireMockContainerBuilder()
@@ -172,6 +184,19 @@ public partial class TestcontainersTests
return wireMockContainer;
}
private static async Task<WireMockContainer> Given_WireMockContainerWithProtoDefinitionAtServerLevelWithWatchStaticMappingsIsStartedForHttpAndGrpcAsync()
{
var wireMockContainer = new WireMockContainerBuilder()
.AddUrl("grpc://*:9090")
.AddProtoDefinition("my-greeter", ReadFile("greet.proto"))
.WithMappings(Path.Combine(Directory.GetCurrentDirectory(), "__admin", "mappings"))
.Build();
await wireMockContainer.StartAsync();
return wireMockContainer;
}
private static async Task Given_ProtoBufMappingIsAddedViaAdminInterfaceAsync(WireMockContainer wireMockContainer, string filename)
{
var mappingsJson = ReadFile(filename);

View File

@@ -15,13 +15,13 @@ public class PortUtilsTests
var url = "test";
// Act
var result = PortUtils.TryExtract(url, out var isHttps, out var isGrpc, out var proto, out var host, out var port);
var result = PortUtils.TryExtract(url, out var isHttps, out var isGrpc, out var scheme, out var host, out var port);
// Assert
result.Should().BeFalse();
isHttps.Should().BeFalse();
isGrpc.Should().BeFalse();
proto.Should().BeNull();
scheme.Should().BeNull();
host.Should().BeNull();
port.Should().Be(default(int));
}
@@ -33,13 +33,13 @@ public class PortUtilsTests
var url = "http://0.0.0.0";
// Act
var result = PortUtils.TryExtract(url, out var isHttps, out var isGrpc, out var proto, out var host, out var port);
var result = PortUtils.TryExtract(url, out var isHttps, out var isGrpc, out var scheme, out var host, out var port);
// Assert
result.Should().BeFalse();
isHttps.Should().BeFalse();
isGrpc.Should().BeFalse();
proto.Should().BeNull();
scheme.Should().BeNull();
host.Should().BeNull();
port.Should().Be(default(int));
}
@@ -51,13 +51,13 @@ public class PortUtilsTests
var url = "http://wiremock.net:1234";
// Act
var result = PortUtils.TryExtract(url, out var isHttps, out var isGrpc, out var proto, out var host, out var port);
var result = PortUtils.TryExtract(url, out var isHttps, out var isGrpc, out var scheme, out var host, out var port);
// Assert
result.Should().BeTrue();
isHttps.Should().BeFalse();
isGrpc.Should().BeFalse();
proto.Should().Be("http");
scheme.Should().Be("http");
host.Should().Be("wiremock.net");
port.Should().Be(1234);
}
@@ -69,13 +69,13 @@ public class PortUtilsTests
var url = "https://wiremock.net:5000";
// Act
var result = PortUtils.TryExtract(url, out var isHttps, out var isGrpc, out var proto, out var host, out var port);
var result = PortUtils.TryExtract(url, out var isHttps, out var isGrpc, out var scheme, out var host, out var port);
// Assert
result.Should().BeTrue();
isHttps.Should().BeTrue();
isGrpc.Should().BeFalse();
proto.Should().Be("https");
scheme.Should().Be("https");
host.Should().Be("wiremock.net");
port.Should().Be(5000);
}
@@ -87,13 +87,13 @@ public class PortUtilsTests
var url = "grpc://wiremock.net:1234";
// Act
var result = PortUtils.TryExtract(url, out var isHttps, out var isGrpc, out var proto, out var host, out var port);
var result = PortUtils.TryExtract(url, out var isHttps, out var isGrpc, out var scheme, out var host, out var port);
// Assert
result.Should().BeTrue();
isHttps.Should().BeFalse();
isGrpc.Should().BeTrue();
proto.Should().Be("grpc");
scheme.Should().Be("grpc");
host.Should().Be("wiremock.net");
port.Should().Be(1234);
}
@@ -105,13 +105,13 @@ public class PortUtilsTests
var url = "https://0.0.0.0:5000";
// Act
var result = PortUtils.TryExtract(url, out var isHttps, out var isGrpc, out var proto, out var host, out var port);
var result = PortUtils.TryExtract(url, out var isHttps, out var isGrpc, out var scheme, out var host, out var port);
// Assert
result.Should().BeTrue();
isHttps.Should().BeTrue();
isGrpc.Should().BeFalse();
proto.Should().Be("https");
scheme.Should().Be("https");
host.Should().Be("0.0.0.0");
port.Should().Be(5000);
}