-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WireMock.Net.Testcontainers * . * logger? * . * . * WatchStaticMappings * linux * . * -- * ContainerInfo * . * 02 * . * fix * .
- Loading branch information
Showing
10 changed files
with
480 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Newtonsoft.Json; | ||
using Testcontainers.MsSql; | ||
using WireMock.Net.Testcontainers; | ||
|
||
namespace WireMock.Net.TestcontainersExample; | ||
|
||
internal class Program | ||
{ | ||
private static async Task Main(string[] args) | ||
{ | ||
var container = new WireMockContainerBuilder() | ||
.WithAdminUserNameAndPassword("x", "y") | ||
.WithMappings(@"C:\Dev\GitHub\WireMock.Net\examples\WireMock.Net.Console.NET6\__admin\mappings") | ||
.WithWatchStaticMappings(true) | ||
.WithAutoRemove(true) | ||
.WithCleanUp(true) | ||
.Build(); | ||
|
||
await container.StartAsync().ConfigureAwait(false); | ||
|
||
var logs = await container.GetLogsAsync(DateTime.Now.AddDays(-1)).ConfigureAwait(false); | ||
Console.WriteLine("logs = " + logs.Stdout); | ||
|
||
var restEaseApiClient = container.CreateWireMockAdminClient(); | ||
|
||
var settings = await restEaseApiClient.GetSettingsAsync(); | ||
Console.WriteLine("settings = " + JsonConvert.SerializeObject(settings, Formatting.Indented)); | ||
|
||
var mappings = await restEaseApiClient.GetMappingsAsync(); | ||
Console.WriteLine("mappings = " + JsonConvert.SerializeObject(mappings, Formatting.Indented)); | ||
|
||
var client = container.CreateClient(); | ||
var result = await client.GetStringAsync("/static/mapping"); | ||
Console.WriteLine("result = " + result); | ||
|
||
await container.StopAsync(); | ||
|
||
var sql = new MsSqlBuilder() | ||
.WithAutoRemove(true) | ||
.WithCleanUp(true) | ||
.Build(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
examples/WireMock.Net.TestcontainersExample/WireMock.Net.TestcontainersExample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Testcontainers.MsSql" Version="3.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\WireMock.Net.Testcontainers\WireMock.Net.Testcontainers.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="873d495f-940e-4b86-a1f4-4f0fc7be8b8b.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace WireMock.Net.Testcontainers.Models; | ||
|
||
internal record ContainerInfo | ||
( | ||
string Image, | ||
string MappingsPath | ||
); |
36 changes: 36 additions & 0 deletions
36
src/WireMock.Net.Testcontainers/WireMock.Net.Testcontainers.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description>A fluent testcontainer builder for the Docker version of WireMock.Net</Description> | ||
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<PackageTags>wiremock;docker;testcontainer;testcontainers</PackageTags> | ||
<ProjectGuid>{12B016A5-9D8B-4EFE-96C2-CA51BE43367D}</ProjectGuid> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<CodeAnalysisRuleSet>../WireMock.Net/WireMock.Net.ruleset</CodeAnalysisRuleSet> | ||
<SignAssembly>true</SignAssembly> | ||
<AssemblyOriginatorKeyFile>../WireMock.Net/WireMock.Net.snk</AssemblyOriginatorKeyFile> | ||
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<LangVersion>10</LangVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)' == 'Release'"> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="IsExternalInit" Version="1.0.3"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" /> | ||
<PackageReference Include="Stef.Validation" Version="0.1.1" /> | ||
<PackageReference Include="Testcontainers" Version="3.2.0" /> | ||
<PackageReference Include="JetBrains.Annotations" VersionOverride="2022.3.1" PrivateAssets="All" Version="2022.3.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\WireMock.Net.RestClient\WireMock.Net.RestClient.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using Docker.DotNet.Models; | ||
using DotNet.Testcontainers.Builders; | ||
using DotNet.Testcontainers.Configurations; | ||
using JetBrains.Annotations; | ||
|
||
namespace WireMock.Net.Testcontainers; | ||
|
||
/// <inheritdoc cref="ContainerConfiguration" /> | ||
[PublicAPI] | ||
public sealed class WireMockConfiguration : ContainerConfiguration | ||
{ | ||
#pragma warning disable CS1591 | ||
public string? Username { get; } | ||
|
||
public string? Password { get; } | ||
|
||
public bool HasBasicAuthentication => !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password); | ||
|
||
public WireMockConfiguration( | ||
string? username = null, | ||
string? password = null | ||
) | ||
{ | ||
Username = username; | ||
Password = password; | ||
} | ||
#pragma warning restore CS1591 | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="WireMockConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public WireMockConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) : base(resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="WireMockConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public WireMockConfiguration(IContainerConfiguration resourceConfiguration) : base(resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="WireMockConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
public WireMockConfiguration(WireMockConfiguration resourceConfiguration) : this(new WireMockConfiguration(), resourceConfiguration) | ||
{ | ||
// Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="WireMockConfiguration" /> class. | ||
/// </summary> | ||
/// <param name="oldValue">The old Docker resource configuration.</param> | ||
/// <param name="newValue">The new Docker resource configuration.</param> | ||
public WireMockConfiguration(WireMockConfiguration oldValue, WireMockConfiguration newValue) : base(oldValue, newValue) | ||
{ | ||
Username = BuildConfiguration.Combine(oldValue.Username, newValue.Username); | ||
Password = BuildConfiguration.Combine(oldValue.Password, newValue.Password); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using DotNet.Testcontainers.Containers; | ||
using JetBrains.Annotations; | ||
using Microsoft.Extensions.Logging; | ||
using RestEase; | ||
using Stef.Validation; | ||
using WireMock.Client; | ||
|
||
namespace WireMock.Net.Testcontainers; | ||
|
||
/// <summary> | ||
/// A container for running WireMock in a docker environment. | ||
/// </summary> | ||
public sealed class WireMockContainer : DockerContainer | ||
{ | ||
internal const int ContainerPort = 80; | ||
|
||
private readonly WireMockConfiguration _configuration; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="WireMockContainer" /> class. | ||
/// </summary> | ||
/// <param name="configuration">The container configuration.</param> | ||
/// <param name="logger">The logger.</param> | ||
public WireMockContainer(WireMockConfiguration configuration, ILogger logger) : base(configuration, logger) | ||
{ | ||
_configuration = Guard.NotNull(configuration); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the public Url. | ||
/// </summary> | ||
[PublicAPI] | ||
public string GetPublicUrl() => GetPublicUri().ToString(); | ||
|
||
/// <summary> | ||
/// Create a RestEase Admin client which can be used to call the admin REST endpoint. | ||
/// </summary> | ||
/// <returns>A <see cref="IWireMockAdminApi"/></returns> | ||
[PublicAPI] | ||
public IWireMockAdminApi CreateWireMockAdminClient() | ||
{ | ||
ValidateIfRunning(); | ||
|
||
var api = RestClient.For<IWireMockAdminApi>(GetPublicUri()); | ||
|
||
if (_configuration.HasBasicAuthentication) | ||
{ | ||
api.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_configuration.Username}:{_configuration.Password}"))); | ||
} | ||
|
||
return api; | ||
} | ||
|
||
/// <summary> | ||
/// Create a <see cref="HttpClient"/> which can be used to call this instance. | ||
/// <param name="handlers"> | ||
/// An ordered list of System.Net.Http.DelegatingHandler instances to be invoked | ||
/// as an System.Net.Http.HttpRequestMessage travels from the System.Net.Http.HttpClient | ||
/// to the network and an System.Net.Http.HttpResponseMessage travels from the network | ||
/// back to System.Net.Http.HttpClient. The handlers are invoked in a top-down fashion. | ||
/// That is, the first entry is invoked first for an outbound request message but | ||
/// last for an inbound response message. | ||
/// </param> | ||
/// </summary> | ||
[PublicAPI] | ||
public HttpClient CreateClient(params DelegatingHandler[] handlers) | ||
{ | ||
ValidateIfRunning(); | ||
|
||
var client = HttpClientFactory.Create(handlers); | ||
client.BaseAddress = GetPublicUri(); | ||
return client; | ||
} | ||
|
||
/// <summary> | ||
/// Create a <see cref="HttpClient"/> (one for each URL) which can be used to call this instance. | ||
/// <param name="innerHandler">The inner handler represents the destination of the HTTP message channel.</param> | ||
/// <param name="handlers"> | ||
/// An ordered list of System.Net.Http.DelegatingHandler instances to be invoked | ||
/// as an System.Net.Http.HttpRequestMessage travels from the System.Net.Http.HttpClient | ||
/// to the network and an System.Net.Http.HttpResponseMessage travels from the network | ||
/// back to System.Net.Http.HttpClient. The handlers are invoked in a top-down fashion. | ||
/// That is, the first entry is invoked first for an outbound request message but | ||
/// last for an inbound response message. | ||
/// </param> | ||
/// </summary> | ||
[PublicAPI] | ||
public HttpClient CreateClient(HttpMessageHandler innerHandler, params DelegatingHandler[] handlers) | ||
{ | ||
ValidateIfRunning(); | ||
|
||
var client = HttpClientFactory.Create(innerHandler, handlers); | ||
client.BaseAddress = GetPublicUri(); | ||
return client; | ||
} | ||
|
||
private void ValidateIfRunning() | ||
{ | ||
if (State != TestcontainersStates.Running) | ||
{ | ||
throw new InvalidOperationException("Unable to create HttpClient because the WireMock.Net is not yet running."); | ||
} | ||
} | ||
|
||
private Uri GetPublicUri() => new UriBuilder(Uri.UriSchemeHttp, Hostname, GetMappedPublicPort(ContainerPort)).Uri; | ||
} |
Oops, something went wrong.