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

Commit

Permalink
Improve ArgumentException error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Oct 7, 2024
1 parent 1d870ed commit 769608e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Client/Extensions/HttpClientDiscoveryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public static class HttpClientDiscoveryExtensions
/// <returns></returns>
public static async Task<DiscoveryDocumentResponse> GetDiscoveryDocumentAsync(this HttpClient client, string? address = null, CancellationToken cancellationToken = default)
{
if (address == null && client.BaseAddress == null)
throw new ArgumentException("Either the address parameter or the HttpClient BaseAddress must not be null.");
return await client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest { Address = address }, cancellationToken).ConfigureAwait();
}

Expand All @@ -40,13 +42,13 @@ public static async Task<DiscoveryDocumentResponse> GetDiscoveryDocumentAsync(th
{
address = request.Address!;
}
else if (client is HttpClient httpClient)
else if (client is HttpClient httpClient && httpClient.BaseAddress != null)
{
address = httpClient.BaseAddress!.GetLeftPart(UriPartial.Authority);
}
else
{
throw new ArgumentException("An address is required.");
throw new ArgumentException("Either the DiscoveryDocumentRequest Address or the HttpClient BaseAddress must not be null.");
}

var parsed = DiscoveryEndpoint.ParseUrl(address, request.Policy.DiscoveryDocumentPath);
Expand Down
20 changes: 20 additions & 0 deletions test/UnitTests/HttpClientExtensions/DiscoveryExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ public async Task Base_address_should_work(string baseAddress)
disco.IsError.Should().BeFalse();
}

[Fact]
public async Task Null_client_base_address_should_throw()
{
var client = new HttpClient(_successHandler) { BaseAddress = null };

Func<Task> act = () => client.GetDiscoveryDocumentAsync();

await act.Should().ThrowAsync<ArgumentException>().WithMessage("Either the address parameter or the HttpClient BaseAddress must not be null.");
}

[Fact]
public async Task Null_discovery_document_address_should_throw()
{
var client = new HttpClient(_successHandler) { BaseAddress = null };

Func<Task> act = () => client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest());

await act.Should().ThrowAsync<ArgumentException>().WithMessage("Either the DiscoveryDocumentRequest Address or the HttpClient BaseAddress must not be null.");
}

[Fact]
public async Task Explicit_address_should_work()
{
Expand Down

0 comments on commit 769608e

Please sign in to comment.