Skip to content

Commit 3afea3c

Browse files
committed
...
1 parent 705b252 commit 3afea3c

10 files changed

+73
-42
lines changed

src/WireMock.Net.Aspire/DistributedApplicationExtensions.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
21
using System.Globalization;
3-
using System.Linq;
42
using System.Net.Http.Headers;
53
using System.Text;
64
using Aspire.Hosting.ApplicationModel;

src/WireMock.Net.Aspire/WireMock.Net.Aspire.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<Version>0.0.1-preview-01</Version>
4+
<Version>0.0.1-preview-02</Version>
5+
<ImplicitUsings>enable</ImplicitUsings>
56
<Description>Aspire extension to start a WireMock.Net server to stub an api.</Description>
67
<AssemblyTitle>WireMock.Net.Aspire</AssemblyTitle>
78
<Authors>Stef Heyenrath</Authors>

src/WireMock.Net.Aspire/WireMockServerArguments.cs

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
51
using WireMock.Client.Builders;
62

73
// ReSharper disable once CheckNamespace

src/WireMock.Net.Aspire/WireMockServerBuilderExtensions.cs

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
using System.Threading.Tasks;
31
using Aspire.Hosting.ApplicationModel;
42
using Aspire.Hosting.Lifecycle;
53
using Stef.Validation;
@@ -15,7 +13,7 @@ namespace Aspire.Hosting;
1513
public static class WireMockServerBuilderExtensions
1614
{
1715
// Linux only (https://github.com/dotnet/aspire/issues/854)
18-
private const string DefaultLinuxImage = "sheyenrath/wiremock.net";
16+
private const string DefaultLinuxImage = "sheyenrath/wiremock.net-alpine";
1917
private const string DefaultLinuxMappingsPath = "/app/__admin/mappings";
2018

2119
/// <summary>
@@ -64,13 +62,6 @@ public static IResourceBuilder<WireMockServerResource> AddWireMock(this IDistrib
6462

6563
resourceBuilder = resourceBuilder.WithArgs(ctx =>
6664
{
67-
//var urls = new List<string>
68-
//{
69-
// $"http://*:{HttpContainerPort}"
70-
//};
71-
72-
//ctx.Args.Add($"--URLS http://*:{HttpContainerPort}, https://*:{HttpsContainerPort}");
73-
7465
foreach (var arg in arguments.GetArgs())
7566
{
7667
ctx.Args.Add(arg);

src/WireMock.Net.Aspire/WireMockServerMappingBuilderHook.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
using System;
2-
using System.Linq;
3-
using System.Threading;
4-
using System.Threading.Tasks;
51
using Aspire.Hosting.ApplicationModel;
62
using Aspire.Hosting.Lifecycle;
73
using RestEase;
@@ -25,9 +21,11 @@ public Task AfterResourcesCreatedAsync(DistributedApplicationModel appModel, Can
2521

2622
foreach (var wireMockInstance in wireMockInstances)
2723
{
28-
if (wireMockInstance.PrimaryEndpoint.IsAllocated)
24+
var endpoint = wireMockInstance.GetEndpoint("http");
25+
if (endpoint.IsAllocated)
2926
{
30-
var adminApi = RestClient.For<IWireMockAdminApi>(new Uri(wireMockInstance.PrimaryEndpoint.Url, UriKind.Absolute));
27+
var adminApi = RestClient.For<IWireMockAdminApi>(endpoint.Url);
28+
3129
var mappingBuilder = adminApi.GetMappingBuilder();
3230
wireMockInstance.Arguments.ApiMappingBuilder?.Invoke(mappingBuilder);
3331
}

src/WireMock.Net.Aspire/WireMockServerResource.cs

-8
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,8 @@ namespace Aspire.Hosting.ApplicationModel;
88
/// </summary>
99
public class WireMockServerResource : ContainerResource, IResourceWithServiceDiscovery
1010
{
11-
private const string PrimaryEndpointName = "http";
12-
private EndpointReference? _primaryEndpoint;
13-
1411
internal WireMockServerArguments Arguments { get; }
1512

16-
/// <summary>
17-
/// Gets the primary endpoint for the server.
18-
/// </summary>
19-
public EndpointReference PrimaryEndpoint => _primaryEndpoint ??= new(this, PrimaryEndpointName);
20-
2113
/// <summary>
2214
/// Initializes a new instance of the <see cref="WireMockServerResource"/> class.
2315
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
using WireMock.Net.Aspire.TestAppHost;
2+
13
var builder = DistributedApplication.CreateBuilder(args);
24

35
var mappingsPath = Path.Combine(Directory.GetCurrentDirectory(), "WireMockMappings");
46

57
builder
68
.AddWireMock("wiremock-service")
79
.WithMappingsPath(mappingsPath)
8-
.WithWatchStaticMappings();
10+
.WithWatchStaticMappings()
11+
.WithApiMappingBuilder(WeatherForecastApiMock.BuildAsync);
912

10-
await builder.Build().RunAsync();
13+
await builder
14+
.Build()
15+
.RunAsync();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using WireMock.Client.Builders;
2+
3+
namespace WireMock.Net.Aspire.TestAppHost;
4+
5+
internal class WeatherForecastApiMock
6+
{
7+
public static async Task BuildAsync(AdminApiMappingBuilder builder)
8+
{
9+
var summaries = new[]
10+
{
11+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
12+
};
13+
14+
builder.Given(b => b
15+
.WithRequest(request => request
16+
.UsingGet()
17+
.WithPath("/weatherforecast2")
18+
)
19+
.WithResponse(response => response
20+
.WithHeaders(h => h.Add("Content-Type", "application/json"))
21+
.WithBodyAsJson(() => Enumerable.Range(1, 5).Select(index =>
22+
new WeatherForecast
23+
(
24+
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
25+
Random.Shared.Next(-20, 55),
26+
summaries[Random.Shared.Next(summaries.Length)]
27+
))
28+
.ToArray())
29+
)
30+
);
31+
32+
await builder.BuildAndPostAsync();
33+
}
34+
}
35+
36+
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary);

test/WireMock.Net.Aspire.Tests/IntegrationTests.cs

+20-6
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,23 @@ public async Task StartAppHostWithWireMockAndCreateHttpClientToCallTheMockedWeat
2323
await using var app = await appHost.BuildAsync();
2424
await app.StartAsync();
2525

26-
// Act
2726
using var httpClient = app.CreateHttpClient("wiremock-service");
28-
var weatherForecasts = await httpClient.GetFromJsonAsync<WeatherForecast[]>("/weatherforecast");
2927

30-
// Assert
31-
weatherForecasts.Should().BeEquivalentTo(new[]
28+
// Act 1
29+
var weatherForecasts1 = await httpClient.GetFromJsonAsync<WeatherForecast[]>("/weatherforecast");
30+
31+
// Assert 1
32+
weatherForecasts1.Should().BeEquivalentTo(new[]
3233
{
3334
new WeatherForecast(new DateOnly(2024, 5, 24), -10, "Freezing"),
3435
new WeatherForecast(new DateOnly(2024, 5, 25), +33, "Hot")
3536
});
37+
38+
// Act 2
39+
var weatherForecasts2 = await httpClient.GetFromJsonAsync<WeatherForecast[]>("/weatherforecast2");
40+
41+
// Assert 2
42+
weatherForecasts2.Should().HaveCount(5);
3643
}
3744

3845
[Fact]
@@ -49,11 +56,18 @@ public async Task StartAppHostWithWireMockAndCreateWireMockAdminClientToCallTheA
4956
await using var app = await appHost.BuildAsync();
5057
await app.StartAsync();
5158

52-
// Act
5359
var adminClient = app.CreateWireMockAdminClient("wiremock-service");
60+
61+
// Act 1
5462
var settings = await adminClient.GetSettingsAsync();
5563

56-
// Assert
64+
// Assert 1
5765
settings.Should().NotBeNull();
66+
67+
// Act 2
68+
var mappings = await adminClient.GetMappingsAsync();
69+
70+
// Assert 2
71+
mappings.Should().HaveCount(2);
5872
}
5973
}

test/WireMock.Net.Tests/WireMockServer.Proxy.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ public async Task WireMockServer_ProxyAndRecordSettings_SameRequest_ShouldProxyA
929929
//Arrange
930930
var wireMockServerSettings = new WireMockServerSettings
931931
{
932-
Urls = new[] { "http://localhost:9091" },
932+
Urls = new[] { "http://localhost:19091" },
933933
ProxyAndRecordSettings = new ProxyAndRecordSettings
934934
{
935935
Url = "http://postman-echo.com",
@@ -947,13 +947,13 @@ public async Task WireMockServer_ProxyAndRecordSettings_SameRequest_ShouldProxyA
947947
var request = new HttpRequestMessage
948948
{
949949
Method = HttpMethod.Post,
950-
RequestUri = new Uri("http://localhost:9091/post"),
950+
RequestUri = new Uri("http://localhost:19091/post"),
951951
Content = new StringContent(requestBody)
952952
};
953953
var request2 = new HttpRequestMessage
954954
{
955955
Method = HttpMethod.Post,
956-
RequestUri = new Uri("http://localhost:9091/post"),
956+
RequestUri = new Uri("http://localhost:19091/post"),
957957
Content = new StringContent(requestBody)
958958
};
959959
server.ResetMappings();

0 commit comments

Comments
 (0)