Skip to content
This repository was archived by the owner on Feb 11, 2025. It is now read-only.

Commit 0b0367a

Browse files
authored
Merge pull request #592 from 0xced/BetterArgumentExceptionMessage
Improve ArgumentException error messages
2 parents 1d870ed + 769608e commit 0b0367a

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/Client/Extensions/HttpClientDiscoveryExtensions.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public static class HttpClientDiscoveryExtensions
2323
/// <returns></returns>
2424
public static async Task<DiscoveryDocumentResponse> GetDiscoveryDocumentAsync(this HttpClient client, string? address = null, CancellationToken cancellationToken = default)
2525
{
26+
if (address == null && client.BaseAddress == null)
27+
throw new ArgumentException("Either the address parameter or the HttpClient BaseAddress must not be null.");
2628
return await client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest { Address = address }, cancellationToken).ConfigureAwait();
2729
}
2830

@@ -40,13 +42,13 @@ public static async Task<DiscoveryDocumentResponse> GetDiscoveryDocumentAsync(th
4042
{
4143
address = request.Address!;
4244
}
43-
else if (client is HttpClient httpClient)
45+
else if (client is HttpClient httpClient && httpClient.BaseAddress != null)
4446
{
4547
address = httpClient.BaseAddress!.GetLeftPart(UriPartial.Authority);
4648
}
4749
else
4850
{
49-
throw new ArgumentException("An address is required.");
51+
throw new ArgumentException("Either the DiscoveryDocumentRequest Address or the HttpClient BaseAddress must not be null.");
5052
}
5153

5254
var parsed = DiscoveryEndpoint.ParseUrl(address, request.Policy.DiscoveryDocumentPath);

test/UnitTests/HttpClientExtensions/DiscoveryExtensionsTests.cs

+20
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,26 @@ public async Task Base_address_should_work(string baseAddress)
9090
disco.IsError.Should().BeFalse();
9191
}
9292

93+
[Fact]
94+
public async Task Null_client_base_address_should_throw()
95+
{
96+
var client = new HttpClient(_successHandler) { BaseAddress = null };
97+
98+
Func<Task> act = () => client.GetDiscoveryDocumentAsync();
99+
100+
await act.Should().ThrowAsync<ArgumentException>().WithMessage("Either the address parameter or the HttpClient BaseAddress must not be null.");
101+
}
102+
103+
[Fact]
104+
public async Task Null_discovery_document_address_should_throw()
105+
{
106+
var client = new HttpClient(_successHandler) { BaseAddress = null };
107+
108+
Func<Task> act = () => client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest());
109+
110+
await act.Should().ThrowAsync<ArgumentException>().WithMessage("Either the DiscoveryDocumentRequest Address or the HttpClient BaseAddress must not be null.");
111+
}
112+
93113
[Fact]
94114
public async Task Explicit_address_should_work()
95115
{

0 commit comments

Comments
 (0)