Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Meir017 opened this issue Jan 14, 2025 · 1 comment

Comments

@Meir017
Copy link

Meir017 commented Jan 14, 2025

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.

@jmprieur
Copy link
Collaborator

@bgavrilMS: any thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants