mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-13 22:03:35 +01:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4538f6cd27 | ||
|
|
43746631e1 | ||
|
|
8eda46ffc7 | ||
|
|
17f5ab5145 |
@@ -1,3 +1,11 @@
|
||||
# 1.5.57 (04 June 2024)
|
||||
- [#1113](https://github.com/WireMock-Net/WireMock.Net/pull/1113) - Add some Extension methods to IWireMockAdminApi [feature] contributed by [StefH](https://github.com/StefH)
|
||||
|
||||
# 1.5.56 (03 June 2024)
|
||||
- [#1111](https://github.com/WireMock-Net/WireMock.Net/pull/1111) - Fix Request.Create().WithBodyAsJson(...) [bug] contributed by [StefH](https://github.com/StefH)
|
||||
- [#1112](https://github.com/WireMock-Net/WireMock.Net/pull/1112) - Add "/__admin/health" endpoint [feature] contributed by [StefH](https://github.com/StefH)
|
||||
- [#1110](https://github.com/WireMock-Net/WireMock.Net/issues/1110) - Connection prematurely closed BEFORE response [bug]
|
||||
|
||||
# 1.5.55 (22 May 2024)
|
||||
- [#1107](https://github.com/WireMock-Net/WireMock.Net/pull/1107) - When only Port is provided, bind to * (Fixes #1100) [bug] contributed by [StefH](https://github.com/StefH)
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<VersionPrefix>1.5.55</VersionPrefix>
|
||||
<VersionPrefix>1.5.57</VersionPrefix>
|
||||
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
|
||||
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>
|
||||
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
rem https://github.com/StefH/GitHubReleaseNotes
|
||||
|
||||
SET version=1.5.55
|
||||
SET version=1.5.57
|
||||
|
||||
GitHubReleaseNotes --output CHANGELOG.md --skip-empty-releases --exclude-labels question invalid doc duplicate example --version %version% --token %GH_TOKEN%
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# 1.5.55 (22 May 2024)
|
||||
- #1107 When only Port is provided, bind to * (Fixes #1100) [bug]
|
||||
# 1.5.57 (04 June 2024)
|
||||
- #1113 Add some Extension methods to IWireMockAdminApi [feature]
|
||||
|
||||
The full release notes can be found here: https://github.com/WireMock-Net/WireMock.Net/blob/master/CHANGELOG.md
|
||||
@@ -1,3 +1,9 @@
|
||||
using System;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Stef.Validation;
|
||||
using WireMock.Client.Builders;
|
||||
|
||||
namespace WireMock.Client.Extensions;
|
||||
@@ -7,13 +13,77 @@ namespace WireMock.Client.Extensions;
|
||||
/// </summary>
|
||||
public static class WireMockAdminApiExtensions
|
||||
{
|
||||
private const int MaxRetries = 5;
|
||||
private const int InitialWaitingTimeInMilliSeconds = 500;
|
||||
private const string HealthStatusHealthy = "Healthy";
|
||||
|
||||
/// <summary>
|
||||
/// Get a new <see cref="AdminApiMappingBuilder"/> for the <see cref="IWireMockAdminApi"/>.
|
||||
/// </summary>
|
||||
/// <param name="api">See <see cref="IWireMockAdminApi"/>.</param>
|
||||
/// <param name="adminApi">See <see cref="IWireMockAdminApi"/>.</param>
|
||||
/// <returns></returns>
|
||||
public static AdminApiMappingBuilder GetMappingBuilder(this IWireMockAdminApi api)
|
||||
public static AdminApiMappingBuilder GetMappingBuilder(this IWireMockAdminApi adminApi)
|
||||
{
|
||||
return new AdminApiMappingBuilder(api);
|
||||
return new AdminApiMappingBuilder(adminApi);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set basic authentication to access the <see cref="IWireMockAdminApi"/>.
|
||||
/// </summary>
|
||||
/// <param name="adminApi">See <see cref="IWireMockAdminApi"/>.</param>
|
||||
/// <param name="username">The admin username.</param>
|
||||
/// <param name="password">The admin password.</param>
|
||||
/// <returns><see cref="IWireMockAdminApi"/></returns>
|
||||
public static IWireMockAdminApi WithAuthorization(this IWireMockAdminApi adminApi, string username, string password)
|
||||
{
|
||||
Guard.NotNull(adminApi);
|
||||
Guard.NotNullOrEmpty(username);
|
||||
Guard.NotNullOrEmpty(password);
|
||||
|
||||
adminApi.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")));
|
||||
return adminApi;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wait for the WireMock.Net server to be healthy. (The "/__admin/health" returns "Healthy").
|
||||
/// </summary>
|
||||
/// <param name="adminApi">See <see cref="IWireMockAdminApi"/>.</param>
|
||||
/// <param name="maxRetries">The maximum number of retries. Default is <c>5</c>.</param>
|
||||
/// <param name="cancellationToken">The optional <see cref="CancellationToken"/>.</param>
|
||||
/// <returns>A completed Task in case the health endpoint is available, else throws a <see cref="InvalidOperationException"/>.</returns>
|
||||
public static async Task WaitForHealthAsync(this IWireMockAdminApi adminApi, int maxRetries = MaxRetries, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Guard.NotNull(adminApi);
|
||||
|
||||
var retries = 0;
|
||||
var waitTime = InitialWaitingTimeInMilliSeconds;
|
||||
var totalWaitTime = waitTime;
|
||||
var isHealthy = await IsHealthyAsync(adminApi, cancellationToken);
|
||||
while (!isHealthy && retries < MaxRetries && !cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
waitTime = (int)(InitialWaitingTimeInMilliSeconds * Math.Pow(2, retries));
|
||||
await Task.Delay(waitTime, cancellationToken);
|
||||
isHealthy = await IsHealthyAsync(adminApi, cancellationToken);
|
||||
retries++;
|
||||
totalWaitTime += waitTime;
|
||||
}
|
||||
|
||||
if (retries >= MaxRetries)
|
||||
{
|
||||
throw new InvalidOperationException($"The /__admin/health endpoint did not return 'Healthy' after {MaxRetries} retries and {totalWaitTime / 1000.0:0.0} seconds.");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<bool> IsHealthyAsync(IWireMockAdminApi adminApi, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var status = await adminApi.GetHealthAsync(cancellationToken);
|
||||
return string.Equals(status, HealthStatusHealthy, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,12 +24,24 @@ public interface IWireMockAdminApi
|
||||
[Header("Authorization")]
|
||||
AuthenticationHeaderValue Authorization { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Get health status.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The optional cancellationToken.</param>
|
||||
/// <returns>
|
||||
/// Returns HttpStatusCode <c>200</c> with a value <c>Healthy</c> to indicate that WireMock.Net is healthy.
|
||||
/// Else it returns HttpStatusCode <c>404</c>.
|
||||
/// </returns>
|
||||
[Get("health")]
|
||||
[AllowAnyStatusCode]
|
||||
Task<string> GetHealthAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Get the settings.
|
||||
/// </summary>
|
||||
/// <returns>SettingsModel</returns>
|
||||
[Get("settings")]
|
||||
Task<SettingsModel> GetSettingsAsync();
|
||||
Task<SettingsModel> GetSettingsAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Update the settings.
|
||||
|
||||
@@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging;
|
||||
using RestEase;
|
||||
using Stef.Validation;
|
||||
using WireMock.Client;
|
||||
using WireMock.Client.Extensions;
|
||||
using WireMock.Http;
|
||||
|
||||
namespace WireMock.Net.Testcontainers;
|
||||
@@ -47,13 +48,7 @@ public sealed class WireMockContainer : DockerContainer
|
||||
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;
|
||||
return _configuration.HasBasicAuthentication ? api.WithAuthorization(_configuration.Username!, _configuration.Password!) : api;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -31,6 +31,7 @@ public partial class WireMockServer
|
||||
{
|
||||
private const int EnhancedFileSystemWatcherTimeoutMs = 1000;
|
||||
private const string AdminFiles = "/__admin/files";
|
||||
private const string AdminHealth = "/__admin/health";
|
||||
private const string AdminMappings = "/__admin/mappings";
|
||||
private const string AdminMappingsCode = "/__admin/mappings/code";
|
||||
private const string AdminMappingsWireMockOrg = "/__admin/mappings/wiremock.org";
|
||||
@@ -54,6 +55,9 @@ public partial class WireMockServer
|
||||
#region InitAdmin
|
||||
private void InitAdmin()
|
||||
{
|
||||
// __admin/health
|
||||
Given(Request.Create().WithPath(AdminHealth).UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(HealthGet));
|
||||
|
||||
// __admin/settings
|
||||
Given(Request.Create().WithPath(AdminSettings).UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(SettingsGet));
|
||||
Given(Request.Create().WithPath(AdminSettings).UsingMethod("PUT", "POST").WithHeader(HttpKnownHeaderNames.ContentType, AdminRequestContentTypeJson)).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(SettingsUpdate));
|
||||
@@ -218,6 +222,22 @@ public partial class WireMockServer
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Health
|
||||
private static IResponseMessage HealthGet(IRequestMessage requestMessage)
|
||||
{
|
||||
return new ResponseMessage
|
||||
{
|
||||
BodyData = new BodyData
|
||||
{
|
||||
DetectedBodyType = BodyType.String,
|
||||
BodyAsString = "Healthy"
|
||||
},
|
||||
StatusCode = (int)HttpStatusCode.OK,
|
||||
Headers = new Dictionary<string, WireMockList<string>> { { HttpKnownHeaderNames.ContentType, new WireMockList<string>(WireMockConstants.ContentTypeTextPlain) } }
|
||||
};
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Settings
|
||||
private IResponseMessage SettingsGet(IRequestMessage requestMessage)
|
||||
{
|
||||
@@ -830,4 +850,4 @@ public partial class WireMockServer
|
||||
var singleResult = ((JObject)value).ToObject<T>();
|
||||
return new[] { singleResult! };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ using VerifyXunit;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Admin.Settings;
|
||||
using WireMock.Client;
|
||||
using WireMock.Client.Extensions;
|
||||
using WireMock.Handlers;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers;
|
||||
@@ -41,6 +42,47 @@ public partial class WireMockAdminApiTests
|
||||
VerifyNewtonsoftJson.Enable(VerifySettings);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IWireMockAdminApi_WaitForHealthAsync_AndCall_GetHealthAsync_OK()
|
||||
{
|
||||
// Arrange
|
||||
var adminUsername = $"username_{Guid.NewGuid()}";
|
||||
var adminPassword = $"password_{Guid.NewGuid()}";
|
||||
var server = WireMockServer.Start(w =>
|
||||
{
|
||||
w.StartAdminInterface = true;
|
||||
w.AdminUsername = adminUsername;
|
||||
w.AdminPassword = adminPassword;
|
||||
});
|
||||
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0])
|
||||
.WithAuthorization(adminUsername, adminPassword);
|
||||
|
||||
// Act 1
|
||||
await api.WaitForHealthAsync().ConfigureAwait(false);
|
||||
|
||||
// Act 2
|
||||
var status = await api.GetHealthAsync().ConfigureAwait(false);
|
||||
status.Should().Be("Healthy");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IWireMockAdminApi_WaitForHealthAsync_AndCall_GetHealthAsync_ThrowsException()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.Start(w =>
|
||||
{
|
||||
w.StartAdminInterface = true;
|
||||
w.AdminUsername = $"username_{Guid.NewGuid()}";
|
||||
w.AdminPassword = $"password_{Guid.NewGuid()}";
|
||||
});
|
||||
|
||||
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
|
||||
|
||||
// Act
|
||||
Func<Task> act = () => api.WaitForHealthAsync(maxRetries: 3);
|
||||
await act.Should().ThrowAsync<InvalidOperationException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IWireMockAdminApi_GetSettingsAsync()
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#if NET6_0_OR_GREATER
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using FluentAssertions.Execution;
|
||||
@@ -13,9 +14,12 @@ public class TestcontainersTests
|
||||
public async Task WireMockContainer_Build_and_StartAsync_and_StopAsync()
|
||||
{
|
||||
// Act
|
||||
var adminUsername = $"username_{Guid.NewGuid()}";
|
||||
var adminPassword = $"password_{Guid.NewGuid()}";
|
||||
var wireMockContainer = new WireMockContainerBuilder()
|
||||
.WithAutoRemove(true)
|
||||
.WithCleanUp(true)
|
||||
.WithAdminUserNameAndPassword(adminUsername, adminPassword)
|
||||
.Build();
|
||||
|
||||
try
|
||||
|
||||
@@ -117,7 +117,7 @@ public class WireMockServerProxyTests
|
||||
}
|
||||
|
||||
// Assert
|
||||
server.Mappings.Should().HaveCount(35);
|
||||
server.Mappings.Should().HaveCount(36);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -81,7 +81,7 @@ public class WireMockServerSettingsTests
|
||||
|
||||
// Assert
|
||||
server.Mappings.Should().NotBeNull();
|
||||
server.Mappings.Should().HaveCount(33);
|
||||
server.Mappings.Should().HaveCount(34);
|
||||
server.Mappings.All(m => m.Priority == WireMockConstants.AdminPriority).Should().BeTrue();
|
||||
}
|
||||
|
||||
@@ -100,9 +100,9 @@ public class WireMockServerSettingsTests
|
||||
|
||||
// Assert
|
||||
server.Mappings.Should().NotBeNull();
|
||||
server.Mappings.Should().HaveCount(34);
|
||||
server.Mappings.Should().HaveCount(35);
|
||||
|
||||
server.Mappings.Count(m => m.Priority == WireMockConstants.AdminPriority).Should().Be(33);
|
||||
server.Mappings.Count(m => m.Priority == WireMockConstants.AdminPriority).Should().Be(34);
|
||||
server.Mappings.Count(m => m.Priority == WireMockConstants.ProxyPriority).Should().Be(1);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user