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

Improve ArgumentException error messages #592

Merged
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
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
Loading