Skip to content

[Feature Request] introduce a base class for IDownstreamApi to simplify mocking in tests #3199

@Meir017

Description

@Meir017

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...].

mocking the IDownstreamApi class in tests isn't trivial, the interface contains 36 methods (by creating a test class the implements this interface).

Describe the solution you'd like
To have a base abstract class DownstreamApiBase that implements the interface and exposes just a single method

protected async Task<HttpResponseMessage> CallApiInternalAsync(
  HttpRequestMessage httpRequestMessage,
  DownstreamApiOptions effectiveOptions,
  bool appToken,
  HttpContent? content = null,
  ClaimsPrincipal? user = null,
  CancellationToken cancellationToken = default);

this would mean that the current https://github.com/AzureAD/microsoft-identity-web/blob/master/src/Microsoft.Identity.Web.DownstreamApi/DownstreamApi.cs#L455 API would be slightly different

internal /* for tests */ async Task<HttpResponseMessage> CallApiInternalAsync(
            string? serviceName,
            DownstreamApiOptions effectiveOptions,
            bool appToken,
            HttpContent? content = null,
            ClaimsPrincipal? user = null,
            CancellationToken cancellationToken = default)
        {
            // Downstream API URI
            string apiUrl = effectiveOptions.GetApiUrl();

            // Create an HTTP request message
            using HttpRequestMessage httpRequestMessage = new(
                new HttpMethod(effectiveOptions.HttpMethod),
                apiUrl);
            if (content != null)
            {
                httpRequestMessage.Content = content;
            }

            // call the new protected API
            return await CallApiInternalAsync(message, effectiveOptions, appToken, user, cancellationToken);

then the current DownstreamApi class would extend this abstract-class and override the CallApiInternalAsync method.
This would allow any HttpClient mocking library to easily be leveraged here since the new API would be close to the HttpClient.SendAsync API.

Describe alternatives you've considered
using the Moq library and mocking the interface IDownstreamApi - mocking the correct method is a bit of a nuisance since there are many similar API's.

Additional context
Add any other context or screenshots about the feature request here.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions