Skip to content

Commit 2d83d28

Browse files
authored
Add default BaseAddress when calling CreateClient. (#263)
Fixes #261
1 parent 97d8a84 commit 2d83d28

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ All notable changes to TestableHttpClient will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and
55
this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [0.11] - 2024-06-14
7+
## [0.11] - 2024-06-15
88
### Removed
99
- .NET 7.0 target, since it is no longer supported
1010
- `ShouldHaveMadeRequestsTo(this TestableHttpMessageHandler, string, bool)` and `ShouldHaveMadeRequestsTo(this TestableHttpMessageHandler, string, bool, int)` have been removed. CaseInsensitivity is controlled by the `UriPatternMatchingOptions` that can be set on the `TestableHttpMessageHandler`.
@@ -16,6 +16,7 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1616

1717
### Added
1818
- Support for .NET 8
19+
- `CreateClient` now sets `https://localhost` as default BaseAddress on the created `HttpClient`.
1920

2021
## [0.10] - 2022-12-03
2122
### Deprecated

src/TestableHttpClient/TestableHttpMessageHandlerExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
public static class TestableHttpMessageHandlerExtensions
44
{
5+
private const string DefaultBaseAddress = "https://localhost";
6+
57
/// <summary>
68
/// Create an <seealso cref="HttpClient"/> configured with the TestableHttpMessageHandler.
79
/// </summary>
@@ -37,7 +39,10 @@ public static HttpClient CreateClient(this TestableHttpMessageHandler handler, A
3739
throw new ArgumentNullException(nameof(httpMessageHandlers));
3840
}
3941

40-
var httpClient = new HttpClient(CreateHandlerChain(handler, httpMessageHandlers));
42+
HttpClient httpClient = new(CreateHandlerChain(handler, httpMessageHandlers))
43+
{
44+
BaseAddress = new Uri(DefaultBaseAddress)
45+
};
4146
configureClient(httpClient);
4247

4348
return httpClient;

test/TestableHttpClient.Tests/TestableHttpMessageHandlerExtensionsTests/CreateClient.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public void CreateClient_CorrectTestableHttpMessageHandler_AddsHandlerToHttpClie
2525
Assert.Same(sut, handler);
2626
}
2727

28+
[Fact]
29+
public void CreateClient_ByDefault_SetsBaseAddress()
30+
{
31+
using TestableHttpMessageHandler sut = new();
32+
33+
using var client = sut.CreateClient();
34+
35+
Assert.Equal(new Uri("https://localhost"), client.BaseAddress);
36+
}
37+
2838
[Fact]
2939
public void CreateClient_NullDelegateHandler_ThrowsArgumentNullException()
3040
{

test/TestableHttpClient.Tests/TestableHttpMessageHandlerExtensionsTests/CreateClientWithConfigurer.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ static void configureClient(HttpClient client) { }
3838
Assert.Same(sut, handler);
3939
}
4040

41+
[Fact]
42+
public void CreateClientWithConfigurer_ByDefault_SetsDefaultBaseAddress()
43+
{
44+
using TestableHttpMessageHandler sut = new();
45+
static void configureClient(HttpClient client) { }
46+
47+
using var client = sut.CreateClient(configureClient);
48+
49+
Assert.Equal(new Uri("https://localhost"), client.BaseAddress);
50+
}
51+
52+
[Fact]
53+
public void CreateClientWithConfigurer_WhenConfiguringBaseAddress_DoesNotOverrideWithDefault()
54+
{
55+
using TestableHttpMessageHandler sut = new();
56+
static void configureClient(HttpClient client) { client.BaseAddress = new Uri("https://example"); }
57+
58+
using var client = sut.CreateClient(configureClient);
59+
60+
Assert.Equal(new Uri("https://example"), client.BaseAddress);
61+
}
62+
4163
[Fact]
4264
public void CreateClientWithConfigurer_CallsConfigureClientWithClientToReturn()
4365
{

test/TestableHttpClient.Tests/TestableHttpMessageHandlerExtensionsTests/CreateClientWithConfigurerAndHttpMessageHandlers.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public void CreateClientWithConfigurerAndHttpMessageHandlers_WhenHttpMessageHand
5656
{
5757
using TestableHttpMessageHandler sut = new();
5858
static void configureClient(HttpClient _) { }
59-
IEnumerable<DelegatingHandler> handlers = new DelegatingHandler[]
60-
{
59+
IEnumerable<DelegatingHandler> handlers =
60+
[
6161
Substitute.For<DelegatingHandler>(),
6262
Substitute.For<DelegatingHandler>()
63-
};
63+
];
6464

6565
using var client = sut.CreateClient(configureClient, handlers);
6666

@@ -71,5 +71,37 @@ static void configureClient(HttpClient _) { }
7171
Assert.Same(handlers.Last(), delegatingHandler2);
7272
Assert.Same(sut, delegatingHandler2.InnerHandler);
7373
}
74+
75+
[Fact]
76+
public void CreateClientWithConfigurerAndHttpMessageHandlers_ByDefault_SetsDefaultBaseAddress()
77+
{
78+
using TestableHttpMessageHandler sut = new();
79+
static void configureClient(HttpClient client) { }
80+
IEnumerable<DelegatingHandler> handlers =
81+
[
82+
Substitute.For<DelegatingHandler>(),
83+
Substitute.For<DelegatingHandler>()
84+
];
85+
86+
using var client = sut.CreateClient(configureClient, handlers);
87+
88+
Assert.Equal(new Uri("https://localhost"), client.BaseAddress);
89+
}
90+
91+
[Fact]
92+
public void CreateClientWithConfigurerAndHttpMessageHandlers_WhenConfiguringBaseAddress_DoesNotOverrideWithDefault()
93+
{
94+
using TestableHttpMessageHandler sut = new();
95+
static void configureClient(HttpClient client) { client.BaseAddress = new Uri("https://example"); }
96+
IEnumerable<DelegatingHandler> handlers =
97+
[
98+
Substitute.For<DelegatingHandler>(),
99+
Substitute.For<DelegatingHandler>()
100+
];
101+
102+
using var client = sut.CreateClient(configureClient, handlers);
103+
104+
Assert.Equal(new Uri("https://example"), client.BaseAddress);
105+
}
74106
}
75107

0 commit comments

Comments
 (0)