Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/WireMock.Net.ProtoBuf/Matchers/ProtoBufMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ public string GetCSharpCodeArguments()
}

var protoDefinitions = ProtoDefinition().Texts;

var resolver = new WireMockProtoFileResolver(protoDefinitions);
var request = new ConvertToObjectRequest(protoDefinitions[0], MessageType, input)
.WithProtoFileResolver(resolver);

try
{
return await ProtoBufToJsonConverter.ConvertAsync(request, cancellationToken).ConfigureAwait(false);
return await ProtoBufToJsonConverter.ConvertAsync(request, cancellationToken);
}
catch
{
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net.ProtoBuf/WireMock.Net.ProtoBuf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ProtoBufJsonConverter" Version="0.10.0" />
<PackageReference Include="ProtoBufJsonConverter" Version="0.11.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright © WireMock.Net

using WireMock.Net.Testcontainers;

namespace DotNet.Testcontainers.Configurations;

internal static class HttpWaitStrategyExtensions
{
internal static HttpWaitStrategy WithBasicAuthentication(this HttpWaitStrategy strategy, WireMockConfiguration configuration)
{
if (configuration.HasBasicAuthentication)
{
return strategy.WithBasicAuthentication(configuration.Username, configuration.Password);
}

return strategy;
}
}
20 changes: 20 additions & 0 deletions src/WireMock.Net.Testcontainers/Utils/CombineUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright © WireMock.Net

using System.Collections.Generic;
using System.Linq;

namespace WireMock.Net.Testcontainers.Utils;

internal static class CombineUtils
{
internal static List<T> Combine<T>(List<T> oldValue, List<T> newValue)
{
return oldValue.Union(newValue).ToList();
}

internal static Dictionary<TKey, TValue> Combine<TKey, TValue>(Dictionary<TKey, TValue> oldValue, Dictionary<TKey, TValue> newValue)
where TKey : notnull
{
return oldValue.Union(newValue).ToDictionary(item => item.Key, item => item.Value);
}
}
18 changes: 3 additions & 15 deletions src/WireMock.Net.Testcontainers/WireMockConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright © WireMock.Net

using System.Collections.Generic;
using System.Linq;
using Docker.DotNet.Models;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using JetBrains.Annotations;
using Stef.Validation;
using WireMock.Net.Testcontainers.Utils;

namespace WireMock.Net.Testcontainers;

Expand Down Expand Up @@ -77,8 +77,8 @@ public WireMockConfiguration(WireMockConfiguration oldValue, WireMockConfigurati
StaticMappingsPath = BuildConfiguration.Combine(oldValue.StaticMappingsPath, newValue.StaticMappingsPath);
WatchStaticMappings = BuildConfiguration.Combine(oldValue.WatchStaticMappings, newValue.WatchStaticMappings);
WatchStaticMappingsInSubdirectories = BuildConfiguration.Combine(oldValue.WatchStaticMappingsInSubdirectories, newValue.WatchStaticMappingsInSubdirectories);
AdditionalUrls = Combine(oldValue.AdditionalUrls, newValue.AdditionalUrls);
ProtoDefinitions = Combine(oldValue.ProtoDefinitions, newValue.ProtoDefinitions);
AdditionalUrls = CombineUtils.Combine(oldValue.AdditionalUrls, newValue.AdditionalUrls);
ProtoDefinitions = CombineUtils.Combine(oldValue.ProtoDefinitions, newValue.ProtoDefinitions);
}

/// <summary>
Expand Down Expand Up @@ -130,16 +130,4 @@ public WireMockConfiguration AddProtoDefinition(string id, params string[] proto

return this;
}

private static List<T> Combine<T>(List<T> oldValue, List<T> newValue)
{
return oldValue.Concat(newValue).ToList();
}

private static Dictionary<TKey, TValue> Combine<TKey, TValue>(Dictionary<TKey, TValue> oldValue, Dictionary<TKey, TValue> newValue)
{
return newValue
.Concat(oldValue.Where(item => !newValue.Keys.Contains(item.Key)))
.ToDictionary(item => item.Key, item => item.Value);
}
}
1 change: 1 addition & 0 deletions src/WireMock.Net.Testcontainers/WireMockContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ private async Task CallAdditionalActionsAfterStartedAsync()
foreach (var kvp in _configuration.ProtoDefinitions)
{
Logger.LogInformation("Adding ProtoDefinition {Id}", kvp.Key);

foreach (var protoDefinition in kvp.Value)
{
try
Expand Down
23 changes: 22 additions & 1 deletion src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using Docker.DotNet.Models;
using DotNet.Testcontainers.Builders;
Expand Down Expand Up @@ -250,6 +252,23 @@ public override WireMockContainer Build()

builder.Validate();

var waitForContainerOS = _imageOS == OSPlatform.Windows ? Wait.ForWindowsContainer() : Wait.ForUnixContainer();
builder
.WithWaitStrategy(waitForContainerOS
.UntilHttpRequestIsSucceeded(httpWaitStrategy => httpWaitStrategy
.ForPort(WireMockContainer.ContainerPort)
.WithMethod(HttpMethod.Get)
.WithBasicAuthentication(DockerResourceConfiguration)
.ForPath("/__admin/health")
.ForStatusCode(HttpStatusCode.OK)
.ForResponseMessageMatching(async httpResponseMessage =>
{
var content = await httpResponseMessage.Content.ReadAsStringAsync();
return content?.Contains("Healthy") == true;
})
)
);

return new WireMockContainer(builder.DockerResourceConfiguration);
}

Expand All @@ -262,7 +281,9 @@ protected override WireMockContainerBuilder Init()
return builder
.WithPortBinding(WireMockContainer.ContainerPort, true)
.WithCommand($"--WireMockLogger {DefaultLogger}")
.WithWaitStrategy(waitForContainerOS.UntilMessageIsLogged("WireMock.Net server running"));
.WithWaitStrategy(waitForContainerOS
.UntilMessageIsLogged("WireMock.Net server running", waitStrategy => waitStrategy.WithTimeout(TimeSpan.FromSeconds(30)))
);
}

/// <inheritdoc />
Expand Down
161 changes: 161 additions & 0 deletions test/WireMock.Net.Tests/Testcontainers/CombineUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright © WireMock.Net

using System.Collections.Generic;
using FluentAssertions;
using WireMock.Net.Testcontainers.Utils;
using Xunit;

namespace WireMock.Net.Tests.Testcontainers;

public class CombineUtilsTests
{
[Fact]
public void Combine_Lists_WithBothEmpty_ReturnsEmptyList()
{
// Arrange
var oldValue = new List<string>();
var newValue = new List<string>();

// Act
var result = CombineUtils.Combine(oldValue, newValue);

// Assert
result.Should().BeEmpty();
}

[Fact]
public void Combine_Lists_WithEmptyOldValue_ReturnsNewValue()
{
// Arrange
var oldValue = new List<string>();
var newValue = new List<string> { "item1", "item2" };

// Act
var result = CombineUtils.Combine(oldValue, newValue);

// Assert
result.Should().Equal("item1", "item2");
}

[Fact]
public void Combine_Lists_WithEmptyNewValue_ReturnsOldValue()
{
// Arrange
var oldValue = new List<string> { "item1", "item2" };
var newValue = new List<string>();

// Act
var result = CombineUtils.Combine(oldValue, newValue);

// Assert
result.Should().Equal("item1", "item2");
}

[Fact]
public void Combine_Lists_WithBothPopulated_ReturnsConcatenatedList()
{
// Arrange
var oldValue = new List<int> { 1, 2, 3 };
var newValue = new List<int> { 4, 5, 6 };

// Act
var result = CombineUtils.Combine(oldValue, newValue);

// Assert
result.Should().Equal(1, 2, 3, 4, 5, 6);
}

[Fact]
public void Combine_Lists_WithDuplicates_RemovesDuplicates()
{
// Arrange
var oldValue = new List<string> { "a", "b", "c" };
var newValue = new List<string> { "b", "c", "d" };

// Act
var result = CombineUtils.Combine(oldValue, newValue);

// Assert
result.Should().Equal("a", "b", "c", "d");
}

[Fact]
public void Combine_Dictionaries_WithBothEmpty_ReturnsEmptyDictionary()
{
// Arrange
var oldValue = new Dictionary<string, int>();
var newValue = new Dictionary<string, int>();

// Act
var result = CombineUtils.Combine(oldValue, newValue);

// Assert
result.Should().BeEmpty();
}

[Fact]
public void Combine_Dictionaries_WithEmptyOldValue_ReturnsNewValue()
{
// Arrange
var oldValue = new Dictionary<string, int>();
var newValue = new Dictionary<string, int>
{
{ "key1", 1 },
{ "key2", 2 }
};

// Act
var result = CombineUtils.Combine(oldValue, newValue);

// Assert
result.Should().HaveCount(2);
result["key1"].Should().Be(1);
result["key2"].Should().Be(2);
}

[Fact]
public void Combine_Dictionaries_WithEmptyNewValue_ReturnsOldValue()
{
// Arrange
var oldValue = new Dictionary<string, int>
{
{ "key1", 1 },
{ "key2", 2 }
};
var newValue = new Dictionary<string, int>();

// Act
var result = CombineUtils.Combine(oldValue, newValue);

// Assert
result.Should().HaveCount(2);
result["key1"].Should().Be(1);
result["key2"].Should().Be(2);
}

[Fact]
public void Combine_Dictionaries_WithNoOverlappingKeys_ReturnsMergedDictionary()
{
// Arrange
var oldValue = new Dictionary<string, string>
{
{ "key1", "value1" },
{ "key2", "value2" }
};
var newValue = new Dictionary<string, string>
{
{ "key3", "value3" },
{ "key4", "value4" }
};

// Act
var result = CombineUtils.Combine(oldValue, newValue);

// Assert
result.Should().HaveCount(4);
result["key1"].Should().Be("value1");
result["key2"].Should().Be("value2");
result["key3"].Should().Be("value3");
result["key4"].Should().Be("value4");
}
}
Loading
Loading