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
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;

Check warning on line 8 in src/WireMock.Net.Minimal/Http/WireMockHttpClientFactory.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net.Minimal/Http/WireMockHttpClientFactory.cs#L8

Added line #L8 was not covered by tests
internal class WireMockHttpClientFactory(WireMockServer server, params DelegatingHandler[] handlers) : IHttpClientFactory
{
private readonly Lazy<HttpClient> _lazyHttpClient = new(() => server.CreateClient());

public HttpClient CreateClient(string name)

Check warning on line 13 in src/WireMock.Net.Minimal/Http/WireMockHttpClientFactory.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net.Minimal/Http/WireMockHttpClientFactory.cs#L13

Added line #L13 was not covered by tests
{
return handlers.Length > 0 ? server.CreateClient(handlers) : _lazyHttpClient.Value;
}
}
#endif

Check warning on line 18 in src/WireMock.Net.Minimal/Http/WireMockHttpClientFactory.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net.Minimal/Http/WireMockHttpClientFactory.cs#L18

Added line #L18 was not covered by tests
30 changes: 30 additions & 0 deletions src/WireMock.Net.Minimal/Server/WireMockServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,32 @@
#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">

Check warning on line 128 in src/WireMock.Net.Minimal/Server/WireMockServer.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net.Minimal/Server/WireMockServer.cs#L123-L128

Added lines #L123 - L128 were not covered by tests
/// 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

Check warning on line 131 in src/WireMock.Net.Minimal/Server/WireMockServer.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net.Minimal/Server/WireMockServer.cs#L131

Added line #L131 was not covered by tests
/// 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)

Check warning on line 138 in src/WireMock.Net.Minimal/Server/WireMockServer.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net.Minimal/Server/WireMockServer.cs#L133-L138

Added lines #L133 - L138 were not covered by tests
{
if (!IsStarted)
{

Check warning on line 141 in src/WireMock.Net.Minimal/Server/WireMockServer.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net.Minimal/Server/WireMockServer.cs#L141

Added line #L141 was not covered by tests
throw new InvalidOperationException("Unable to create IHttpClientFactory because the service is not started.");
}

Check warning on line 143 in src/WireMock.Net.Minimal/Server/WireMockServer.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net.Minimal/Server/WireMockServer.cs#L143

Added line #L143 was not covered by tests

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 @@
}

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