Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions src/WireMock.Net.Minimal/Http/WireMockHttpClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright © WireMock.Net
#if NET5_0_OR_GREATER
using System;
using System.Net.Http;
using WireMock.Server;

namespace WireMock.Http;

internal class WireMockHttpClientFactory(WireMockServer server, params DelegatingHandler[] handlers) : IHttpClientFactory
{
private readonly Lazy<HttpClient> _lazyHttpClient = new(() => server.CreateClient());

public HttpClient CreateClient(string name)
{
return handlers.Length > 0 ? server.CreateClient(handlers) : _lazyHttpClient.Value;
}
}
#endif
34 changes: 32 additions & 2 deletions src/WireMock.Net.Minimal/Server/WireMockServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ public partial class WireMockServer : IWireMockServer

/// <inheritdoc />
[PublicAPI]
public int Port => Ports?.FirstOrDefault() ?? default;
public int Port => Ports.FirstOrDefault();

/// <inheritdoc />
[PublicAPI]
public string[] Urls { get; }

/// <inheritdoc />
[PublicAPI]
public string? Url => Urls?.FirstOrDefault();
public string? Url => Urls.FirstOrDefault();

/// <inheritdoc />
[PublicAPI]
Expand Down Expand Up @@ -120,6 +120,32 @@ protected virtual void Dispose(bool disposing)
#endregion

#region HttpClient
#if NET5_0_OR_GREATER
private readonly Lazy<IHttpClientFactory> _lazyHttpClientFactory;

/// <summary>
/// Create a <see cref="IHttpClientFactory"/> which can be used to generate a HttpClient 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 IHttpClientFactory CreateHttpClientFactory(params DelegatingHandler[] handlers)
{
if (!IsStarted)
{
throw new InvalidOperationException("Unable to create IHttpClientFactory because the service is not started.");
}

return handlers.Length > 0 ? new WireMockHttpClientFactory(this, handlers) : _lazyHttpClientFactory.Value;
}
#endif

/// <summary>
/// Create a <see cref="HttpClient"/> which can be used to call this instance.
/// <param name="handlers">
Expand Down Expand Up @@ -427,6 +453,10 @@ protected WireMockServer(WireMockServerSettings settings)
}

InitSettings(settings);

#if NET5_0_OR_GREATER
_lazyHttpClientFactory = new Lazy<IHttpClientFactory>(() => new WireMockHttpClientFactory(this));
#endif
}

/// <inheritdoc cref="IWireMockServer.Stop" />
Expand Down
24 changes: 24 additions & 0 deletions test/WireMock.Net.Tests/WireMockServer.Admin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,30 @@ public async Task WireMockServer_Admin_DeleteMappings()
Check.That(await response.Content.ReadAsStringAsync().ConfigureAwait(false)).Equals($"{{\"Status\":\"Mappings deleted. Affected GUIDs: [{guid1}, {guid2}]\"}}");
}

#if NET5_0_OR_GREATER
[Fact]
public async Task WireMockServer_CreateHttpClientFactory_And_CallEndpoint()
{
// Arrange
var server = WireMockServer.Start();
var factory = server.CreateHttpClientFactory();
var client = factory.CreateClient("any name");

// Act
await client.GetAsync($"{server.Url}/foo").ConfigureAwait(false);

// Assert
Check.That(server.LogEntries).HasSize(1);
var requestLogged = server.LogEntries.First();
Check.That(requestLogged.RequestMessage.Method).IsEqualTo("GET");
Check.That(requestLogged.RequestMessage.BodyData).IsNull();

// Cleanup
server.Stop();
server.Dispose();
}
#endif

[Fact]
public async Task WireMockServer_CreateClient_And_CallEndpoint()
{
Expand Down
Loading