How to mock HttpClient.GetFromJsonAsync? - c#

I've got some code that calls HttpClient's GetFromJsonAsync however I'm struggling to mock the method call and was wondering how can I do this?
C# code:
public class Client : IClient
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly HttpClient _httpClient;
public Client(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
_httpClient = _httpClientFactory.CreateClient();
}
public async Task<List<ApiResponse>> GetData()
{
try
{
return await _httpClient.GetFromJsonAsync<ApiResponse>("endpointUrl"); // How to mock?
}
catch (Exception e)
{
throw;
}
return null;
}
}
I've seen previous posts that suggest I should mock HttpMessageHandler but how do I mock the response back from the GetFromJsonAsync method call?
As per one of the suggested answers, I've done the following:
var httpClientMock = new Mock<HttpClient>();
httpClientMock.Setup(x => x.GetFromJsonAsync<ApiResponse>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ApiResponse());
_httpClientFactoryMock = new Mock<IHttpClientFactory>();
_httpClientFactoryMock.Setup(x => x.CreateClient(It.IsAny<string>())).Returns(httpClientMock.Object);
However I receive the following error:
Message "Unsupported expression: x => x.GetFromJsonAsync<DataLookupResponse>(It.IsAny<string>(), It.IsAny<CancellationToken>())\nExtension methods (here: HttpClientJsonExtensions.GetFromJsonAsync) may not be used in setup / verification expressions."

If you create a mock of HttpClient you can then return this when calling _httpClientFactory.CreateClient();.
Something like this (haven't tested this code in my IDE so be aware of any typo's)
var httpClientMock = new Mock<HttpClient>();
httpClientMock.Setup(x => x.GetFromJsonAsync<ApiResponse>("endpointurl").Returns(...); httpClientFactoryMock.Setup(x => x.CreateClient()).Returns(httpClientMock.Object);

Recently I've been unit testing my HttpClients and I had to solve the same problem as you.
Modify where needed. I'm using the IConfiguration to retrieve some application settings. The code to mock this has also been included in the code you can find below.
The call in the test is a mocked call. You don't need an internet connection for this call to succeed. You can specify any endpoint and call it with any configured response.
This means can return anything you want in your mocked call and use fake endpoint in order to not expose any sensitive data in your code.
Install the following NuGet Packages in your test project in order for my solution to work:
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="RichardSzalay.MockHttp" Version="6.0.0" />
My HttpClient:
public class MyHttpClient : IMyHttpClient
{
private readonly IConfiguration _configuration;
private readonly IHttpClientFactory _httpClientFactory;
public MyHttpClient(IConfiguration configuration, IHttpClientFactory httpClientFactory)
{
_configuration = configuration;
_httpClientFactory = httpClientFactory;
}
public async Task<SomeType> GetSomeInformationAsync()
{
var token = await FetchAccessToken();
var client = CreateHttpClient(token);
var endpoint = _configuration.GetValue<string>("Endpoints:SomeEndpoint");
var response = client.GetAsync(endpoint);
var content = await response.Result.Content.ReadAsStringAsync();
return content;
}
private HttpClient CreateHttpClient(string accessToken)
{
var client = _httpClientFactory.CreateClient(nameof(MyHttpClient));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
return client;
}
}
My TestClass:
public class MyHttpClientTests
{
private readonly MyHttpClient _sut;
private readonly Mock<IConfiguration> _configurationMock = new();
private readonly Mock<IHttpClientFactory> _httpClientFactoryMock = new();
private readonly MockHttpMessageHandler _httpMessageHandlerMock = new();
public MyHttpClientTests()
{
_sut = new MyHttpClient(_configurationMock.Object, _httpClientFactoryMock.Object);
}
[Fact]
public async void GetSomeInformationTest_ShouldReturnSomething()
{
// Since you are mocking you don't need the real endpoint.
var endpoint = "/someNotExistingEndpoint/";
var getSomeInformationValue = new Mock<IConfigurationSection>();
getSomeInformationValue.Setup(x => x.Value).Returns(endpoint);
// When I retrieve my configuration in my mocked HttpClient from 'Endpoints:SomeEndpoint' it will return the value '/someNotExistingEndpoint/'
_configurationMock.Setup(x => x.GetSection(It.Is<string>(x => x == "Endpoints:SomeEndpoint"))).Returns(getSomeInformationValue.Object);
// When the above endpoint is called I can respond with anything I want. In this case an StatusCode of OK and some JsonContent (application/json)).
_httpMessageHandlerMock.When(endpoint).Respond(HttpStatusCode.OK, JsonContent.Create(new { Message = "thisIsSomeJsonResponse" }));
_httpClientFactoryMock.Setup(x => x.CreateClient(nameof(MyHttpClient)))
.Returns(new HttpClient(_httpMessageHandlerMock)
{
BaseAddress = new Uri("someBaseAdress")
});
var result = await _sut.GetSomeInformationAsync();
// You can put your assertions here
}
}

Related

Injection of default headers to httpclient failing?

My webapp relies on two strings for its api calls. I have tested that the api call works as expected when hard-coded. I have also verified that user-secrets holds the expected key-value pairs; my previous attempt using singleton ran into the issue of not being able to differentiate between the various strings coming from user-secrets.
My current attempt at implementation is to set the necessary headers in ConfigureServices:
services.AddHttpClient("OxfordDictionaryClient", _ =>
{
_.DefaultRequestHeaders.Add("app_id", Configuration["app_id"]);
_.DefaultRequestHeaders.Add("app_key", Configuration["app_key"]);
_.DefaultRequestHeaders.Add("Accept", "application/json");
});
However, it seems none of this is being carried into the class where the method is called:
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private HttpClient _client;
public IndexModel(ILogger<IndexModel> logger, HttpClient client)
{
_logger = logger;
_client = client;
}
[BindProperty]
public WordsInput Delivery { get; set; }
public string synonyms { get; set; }
public async Task<IActionResult> OnPostAsync()
{
string word = "anger";
var URL = $"https://od-api.oxforddictionaries.com/api/v2/thesaurus/en/{word}?fields=synonyms&strictMatch=false";
//_client.DefaultRequestHeaders.Add("app_id", ABC);
//_client.DefaultRequestHeaders.Add("app_key", XYZ);
//_client.DefaultRequestHeaders.Add("Accept", "application/json");
var response = await _client.GetAsync(URL);
var result = await response.Content.ReadAsStringAsync();
JObject obj = JObject.Parse(result);
synonyms = obj.ToString();
return Page();
}
}
I can see _client has none of my expected headers with a breakpoint on the line where the URL is set. Setting the headers directly in this class (the commented out code) works as expected, so the issue seems to be in the AddHttpClient not holding onto the headers added in ConfigureServices.
For the best practice of setting headers in ConfigureServices using httpclient, you can refer to the following code:
Startup.cs:
services.AddHttpClient("OxfordDictionaryClient", c =>
{
c.DefaultRequestHeaders.Add("app_id", "testId");
c.DefaultRequestHeaders.Add("app_key", "testKey");
c.DefaultRequestHeaders.Add("Accept", "application/json");
});
PageModel:
public class PrivacyModel : PageModel
{
private readonly ILogger<PrivacyModel> _logger;
private readonly IHttpClientFactory _clientFactory;
public PrivacyModel(ILogger<PrivacyModel> logger, IHttpClientFactory clientFactory)
{
_logger = logger;
_clientFactory = clientFactory;
}
public async Task OnGet()
{
string word = "anger";
var URL = $"https://od-api.oxforddictionaries.com/api/v2/thesaurus/en/{word}?fields=synonyms&strictMatch=false";
var request = new HttpRequestMessage(HttpMethod.Get,
URL);
//Consistent with the name in ConfigureServices
var client = _clientFactory.CreateClient("OxfordDictionaryClient");
Console.WriteLine(client.DefaultRequestHeaders);
var response = await client.SendAsync(request);
var result = await response.Content.ReadAsStringAsync();
}
}
Test Result:
For more details, please refer to this link.

.net services.AddHttpClient Automatic Access Token Handling

I am trying to write a Blazor app that uses client secret credentials to get an access token for the API. I wanted to encapsulate it in such a way that it handles the token fetching and refreshing behind the scenes. To achieve this, I created the following inherited class which uses IdentityModel Nuget package:
public class MPSHttpClient : HttpClient
{
private readonly IConfiguration Configuration;
private readonly TokenProvider Tokens;
private readonly ILogger Logger;
public MPSHttpClient(IConfiguration configuration, TokenProvider tokens, ILogger logger)
{
Configuration = configuration;
Tokens = tokens;
Logger = logger;
}
public async Task<bool> RefreshTokens()
{
if (Tokens.RefreshToken == null)
return false;
var client = new HttpClient();
var disco = await client.GetDiscoveryDocumentAsync(Configuration["Settings:Authority"]);
if (disco.IsError) throw new Exception(disco.Error);
var result = await client.RequestRefreshTokenAsync(new RefreshTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = Configuration["Settings:ClientID"],
RefreshToken = Tokens.RefreshToken
});
Logger.LogInformation("Refresh Token Result {0}", result.IsError);
if (result.IsError)
{
Logger.LogError("Error: {0)", result.ErrorDescription);
return false;
}
Tokens.RefreshToken = result.RefreshToken;
Tokens.AccessToken = result.AccessToken;
Logger.LogInformation("Access Token: {0}", result.AccessToken);
Logger.LogInformation("Refresh Token: {0}" , result.RefreshToken);
return true;
}
public async Task<bool> CheckTokens()
{
if (await RefreshTokens())
return true;
var client = new HttpClient();
var disco = await client.GetDiscoveryDocumentAsync(Configuration["Settings:Authority"]);
if (disco.IsError) throw new Exception(disco.Error);
var result = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = Configuration["Settings:ClientID"],
ClientSecret = Configuration["Settings:ClientSecret"]
});
if (result.IsError)
{
//Log("Error: " + result.Error);
return false;
}
Tokens.AccessToken = result.AccessToken;
Tokens.RefreshToken = result.RefreshToken;
return true;
}
public new async Task<HttpResponseMessage> GetAsync(string requestUri)
{
DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Tokens.AccessToken);
var response = await base.GetAsync(requestUri);
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
if (await CheckTokens())
{
DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Tokens.AccessToken);
response = await base.GetAsync(requestUri);
}
}
return response;
}
}
The idea is to keep from having to write a bunch of redundant code to try the API, then request/refresh the token if you are unauthorized. I tried it at first using extension methods to HttpClient, but there was no good way to inject the Configuration into a static class.
So my Service code is written as this:
public interface IEngineListService
{
Task<IEnumerable<EngineList>> GetEngineList();
}
public class EngineListService : IEngineListService
{
private readonly MPSHttpClient _httpClient;
public EngineListService(MPSHttpClient httpClient)
{
_httpClient = httpClient;
}
async Task<IEnumerable<EngineList>> IEngineListService.GetEngineList()
{
return await JsonSerializer.DeserializeAsync<IEnumerable<EngineList>>
(await _httpClient.GetStreamAsync($"api/EngineLists"), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
}
}
Everything compiles great. In my Startup, I have the following code:
services.AddScoped<TokenProvider>();
services.AddHttpClient<IEngineListService, EngineListService>(client =>
{
client.BaseAddress = new Uri(Configuration["Settings:ApiAddress"]);
});
Just to be complete, Token Provider looks like this:
public class TokenProvider
{
public string AccessToken { get; set; }
public string RefreshToken { get; set; }
}
When I run the App, it complains that it can't find a suitable constructor for EngineListService in the call to services.AddHttpClient. Is there a way to pass AddHttpClient an actual instance of the IEngineListService. Any other way I might be able to achieve this?
Thanks,
Jim
I think that EngineListService should not be registered as a HttpClient in services and instead you should register MPSHttpClient.
This follows the "Typed Client" example in the documentation and uses IHttpClientFactory behind the scenes.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests#typed-clients
When you use services.AddHttpClient the constructor needs a HttpClient parameter. That is how the HttpClientFactory initializes the HttpClient and then passes it into your service ready to go.
You can change your MPSHttpClient to not inherit HttpClient and instead add a HttpClient parameter to the constructor. You could also have it implement an interface like IMPSHttpClient
public class MPSHttpClient
{
public MPSHttpClient(HttpClient httpClient, IConfiguration configuration, TokenProvider tokens, ILogger logger)
{
HttpClient = httpClient;
Configuration = configuration;
Tokens = tokens;
Logger = logger;
}
}
You must remove these lines from MPSHttpClient and use the injected client.
// remove this
var client = new HttpClient();
In Startup add
services.AddHttpClient<MPSHttpClient>(client =>
{
// add any configuration
client.BaseAddress = new Uri(Configuration["Settings:ApiAddress"]);
});
Change EngineListService to a normal service registration as it is not a HttpClient
services.AddScoped<IEngineListService, EngineListService>()
Special thanks to #pinkfloydx33 for helping me solve this. This link that he shared https://blog.joaograssi.com/typed-httpclient-with-messagehandler-getting-accesstokens-from-identityserver/ was everything I needed. The trick was that there exists a class called DelegatingHandler that you can inherit and override the OnSendAsync method and do all of your token-checking there before sending it to the final HttpHandler. So my new MPSHttpClient class is as so:
public class MPSHttpClient : DelegatingHandler
{
private readonly IConfiguration Configuration;
private readonly TokenProvider Tokens;
private readonly ILogger<MPSHttpClient> Logger;
private readonly HttpClient client;
public MPSHttpClient(HttpClient httpClient, IConfiguration configuration, TokenProvider tokens, ILogger<MPSHttpClient> logger)
{
Configuration = configuration;
Tokens = tokens;
Logger = logger;
client = httpClient;
}
public async Task<bool> CheckTokens()
{
var disco = await client.GetDiscoveryDocumentAsync(Configuration["Settings:Authority"]);
if (disco.IsError) throw new Exception(disco.Error);
var result = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = Configuration["Settings:ClientID"],
ClientSecret = Configuration["Settings:ClientSecret"]
});
if (result.IsError)
{
//Log("Error: " + result.Error);
return false;
}
Tokens.AccessToken = result.AccessToken;
Tokens.RefreshToken = result.RefreshToken;
return true;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.SetBearerToken(Tokens.AccessToken);
var response = await base.SendAsync(request, cancellationToken);
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
if (await CheckTokens())
{
request.SetBearerToken(Tokens.AccessToken);
response = await base.SendAsync(request, cancellationToken);
}
}
return response;
}
}
The big changes here are the inheritance and I used DI to obtain the HttpClient much like #Rosco mentioned. I had tried to override OnGetAsync in my original version. When inheriting from DelegatingHandler, all you have to override is OnSendAsync. This will handle all of your get, put, post, and deletes from your HttpContext all in one method.
My EngineList Service is written as if there were no tokens to be considered, which was my original goal:
public interface IEngineListService
{
Task<IEnumerable<EngineList>> GetEngineList();
}
public class EngineListService : IEngineListService
{
private readonly HttpClient _httpClient;
public EngineListService(HttpClient httpClient)
{
_httpClient = httpClient;
}
async Task<IEnumerable<EngineList>> IEngineListService.GetEngineList()
{
return await JsonSerializer.DeserializeAsync<IEnumerable<EngineList>>
(await _httpClient.GetStreamAsync($"api/EngineLists"), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
}
}
The Token Provider stayed the same. I plan to add expirations and such to it, but it works as is:
public class TokenProvider
{
public string AccessToken { get; set; }
public string RefreshToken { get; set; }
}
The ConfigureServices code changed just a bit:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddScoped<TokenProvider>();
services.AddTransient<MPSHttpClient>();
services.AddHttpClient<IEngineListService, EngineListService>(client =>
{
client.BaseAddress = new Uri(Configuration["Settings:ApiAddress"]);
}).AddHttpMessageHandler<MPSHttpClient>();
...
}
You instantiate MPSHttpClient as Transient, then reference it with the AddHttpMessageHandler call attached to the AddHttpClient call. I know this is different than how others implement HttpClients, but I learned this method of creating client services from a Pluralsight video and have been using it for everything. I create a separate Service for each entity in the database. If say I wanted to do tires, I would add the following to ConfigureServices:
services.AddHttpClient<ITireListService, TireListService>(client =>
{
client.BaseAddress = new Uri(Configuration["Settings:ApiAddress"]);
}).AddHttpMessageHandler<MPSHttpClient>();
It will use the same DelegatingHandler so I can just keep adding services for each entity type while no longer worrying about tokens. Thanks to everyone that responded.
Thanks,
Jim

Get name HttpClient from IHttpClientFactory injected with DI

In Blazor I have setup two HttpClients. One for my API and one for MS Graph API.
The Graph API is new, and have forced me to find a way to inject a named httpclient in to my services.
This is all the code in Main
public class Program
{
public static async Task Main(string[] args)
{
var b = WebAssemblyHostBuilder.CreateDefault(args);
b.RootComponents.Add<App>("app");
var samsonApiUrl = new Uri(b.HostEnvironment.BaseAddress + "api/");
b.Services.AddHttpClient("SamsonApi",client =>
{
client.BaseAddress = samsonApiUrl;
// add jwt token to header
// add user agent to header
}).AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
b.Services.AddTransient<GraphCustomAuthorizationMessageHandler>();
b.Services.AddHttpClient<GraphHttpClientService>("GraphAPI",
client => client.BaseAddress = new Uri("https://graph.microsoft.com/"))
.AddHttpMessageHandler<GraphCustomAuthorizationMessageHandler>();
b.Services.AddScoped(provider => provider.GetService<IHttpClientFactory>().CreateClient("SamsonApi"));
b.Services.AddScoped(provider => provider.GetService<IHttpClientFactory>().CreateClient("GraphAPI"));
b.Services.AddMsalAuthentication<RemoteAuthenticationState, CustomUserAccount>(options =>
{
b.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
options.ProviderOptions.DefaultAccessTokenScopes.Add("1c8d4e31-97dd-4a54-8c2b-0d81e4356bf9/API.Access");
options.UserOptions.RoleClaim = "role";
}).AddAccountClaimsPrincipalFactory<RemoteAuthenticationState, CustomUserAccount, CustomUserFactory>();
// add Radzen services
b.Services.AddScoped<DialogService>();
b.Services.AddScoped<NotificationService>();
b.Services.AddScoped<TooltipService>();
// add samson component services
b.Services.AddSingleton<FormTitleState>();
// Add Http Services
b.Services.Scan(scan =>
{
scan.FromAssemblyOf<ICustomerService>()
.AddClasses(classes => classes.Where(type => type.Name.EndsWith("Service")))
.AsMatchingInterface()
.WithScopedLifetime();
});
await b.Build().RunAsync();
}
}
This is the code that has to change.
It's scan all my service and get a HttpClient injected.
And since I now have two I get a random client injected.
How can I inject a named client into all of my services? I can handle the graph API service as a special case.
b.Services.Scan(scan =>
{
scan.FromAssemblyOf<ICustomerService>()
.AddClasses(classes => classes.Where(type => type.Name.EndsWith("Service")))
.AsMatchingInterface()
.WithScopedLifetime();
});
Example of a service calling my API
public class ActiveAgreementService : IActiveAgreementService
{
private readonly HttpClient _client;
public ActiveAgreementService(HttpClient client)
{
_client = client;
}
public async Task<List<ActiveAgreementDto>> GetActiveAgreements()
{
var lst = await _client.GetFromJsonAsync<ActiveAgreementDto[]>("ActiveAgreement");
return lst.ToList();
}
}
Okay ended up with replacing HttpClient with IHttpClientFactory in all my services
public UserService(IHttpClientFactory clientFactory)
{
_client = clientFactory.CreateClient("SamsonApi");
}
I assume you're using ASP.NET Core, although it's not clear which dependency injection framework you're using.
In that case, you could have your classes depend on IHttpClientFactory and then setup the configuration with named clients:
// Named client like you're currently doing
b.Services.AddHttpClient("SamsonApi", client =>
{
client.BaseAddress = samsonApiUrl;
// add jwt token to header
// add user agent to header
}).AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
//...
b.Services.AddHttpClient("GraphAPI", client =>
client.BaseAddress = new Uri("https://graph.microsoft.com/"))
.AddHttpMessageHandler<GraphCustomAuthorizationMessageHandler>();
// And in your dependent class
public class ActiveAgreementService : IActiveAgreementService
{
private readonly HttpClient _client;
public ActiveAgreementService(IHttpClientFactory clientFac)
{
// Whichever one you need:
_client = clientFac.CreateClient("SamsonApi");
_client = clientFac.CreateClient("GraphAPI");
}
public async Task<List<ActiveAgreementDto>> GetActiveAgreements()
{
var lst = await _client.GetFromJsonAsync<ActiveAgreementDto[]>("ActiveAgreement");
return lst.ToList();
}
}
... or with typed clients you specify the instance for each class that depends on it:
// This HttpClient is only injected into ActiveAgreementService
b.Services.AddHttpClient<ActiveAgreementService>(client =>
{
client.BaseAddress = samsonApiUrl;
// add jwt token to header
// add user agent to header
}).AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
//...
// This HttpClient is only injected into GraphHttpClientService
b.Services.AddHttpClient<GraphHttpClientService>(client =>
client.BaseAddress = new Uri("https://graph.microsoft.com/"))
.AddHttpMessageHandler<GraphCustomAuthorizationMessageHandler>();
// And in your dependent class
public class ActiveAgreementService : IActiveAgreementService
{
private readonly HttpClient _client;
public ActiveAgreementService(HttpClient client)
{
_client = client;
}
public async Task<List<ActiveAgreementDto>> GetActiveAgreements()
{
var lst = await _client.GetFromJsonAsync<ActiveAgreementDto[]>("ActiveAgreement");
return lst.ToList();
}
}

Dependency injection problem - Initializing remote service connection

In my .Net Core 3.0 app I want to use the Microsoft Graph Nuget library. I have created a connection class that authenticates my application using [MSAL][1] and then creates the connection and returns this. My idea was to inject this connection object in the constructor using Dependency Injection. However, since the method that creates the connection is async, I seem to have a problem how to use it in the constructor.
My Connect Class
public class AuthorizeGraphApi: IAuthorizeGraphApi
{
private readonly IConfiguration _config;
public AuthorizeGraphApi(IConfiguration config)
{
_config = config;
}
public async Task<GraphServiceClient> ConnectToAAD()
{
string accessToken = await GetAccessTokenFromAuthorityAsync();
var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => {
requestMessage
.Headers
.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
return Task.FromResult(0);
}));
return graphServiceClient;
}
private async Task<string> GetAccessTokenFromAuthorityAsync()
{
// clientid, authUri, etc removed for this example.
IConfidentialClientApplication _conn;
_conn = ConfidentialClientApplicationBuilder.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority(new Uri(authUri))
.Build();
string[] scopes = new string[] { $"api://{clientId}/.default" };
AuthenticationResult result = null;
// AcquireTokenForClient only has async method.
result = await _conn.AcquireTokenForClient(scopes)
.ExecuteAsync();
return result.AccessToken;
}
}
My Graph Service to send requests
public class AzureIntuneService
{
private readonly IAuthorizeGraphApi _graphClient;
public AzureIntuneService(IAuthorizeGraphApi client)
{
//Gives: cannot implicitely convert to Threading.Tasks.Task.... error
_graphClient = client.ConnectToAAD();
}
public async Task<IList<string>> GetAADInformationAsync()
{
// then here, use the graphClient object for the request...
var payload = await _graphClient.Groups.Request().GetAsync();
return payload
}
}
I register the above classess in my startup as follows:
services.AddScoped<IAuthorizeGraphApi, AuthorizeGraphApi>();
The idea was that this way, I don't need to call the _graphClient in each method. How can I inject the connection object in a correct way? Or what are the best practices regarding this (injecting connection objects)?
One way would be to store a reference to the Task and make sure any public methods that use the connection are async:
public class AzureIntuneService
{
private readonly Task<GraphServiceClient> _graphClientTask;
public AzureIntuneService(IAuthorizeGraphApi client)
{
_graphClientTask = client.ConnectToAAD();
}
public async Task<IList<string>> GetAADInformationAsync()
{
var client = await _graphClientTask; // Get the client when connected
var payload = await client.Groups.Request().GetAsync();
return payload;
}
}
Constructors aren't async and should never be used to initialize anything async. The only way to workaround it is to do sync-over-async by doing a .Result which is always a problem.
In your case, the GraphServiceClient that takes in DelegateAuthenticationProvider, accepts an AuthenticateRequestAsyncDelegate. This allows you to have an async delegate to construct the client.
So now you can do
new DelegateAuthenticationProvider(async requestMessage =>
{
string accessToken = await GetAccessTokenFromAuthorityAsync();
//rest of code here
}
)
and this allows you to change your ConnectToAAD signature to just return a GraphServiceClient and not a Task<GraphServiceClient>.
When you need async data you have to look away from the regular constructor and create a factory method (private static function). Something like below:
public sealed class MyClass
{
private MyData asyncData;
private MyClass() { ... }
private async Task<MyClass> InitializeAsync()
{
asyncData = await GetDataAsync();
return this;
}
public static Task<MyClass> CreateAsync()
{
var ret = new MyClass();
return ret.InitializeAsync();
}
}
public static async Task UseMyClassAsync()
{
MyClass instance = await MyClass.CreateAsync();
...
}
More here: https://blog.stephencleary.com/2013/01/async-oop-2-constructors.html

Tests - .net core 3

I'm creating a Rest API in .net core 3 (my first one). In that API I did a dll that I call from some API methods.
I want to write some tests on that dll but I have some issues with some dependency injection and getting values set in API ConfigureServices. My main problem is to get an HttpClient by name with a IHttpClientFactory.
My architecture is :
Project WebApi
Project dllApi
Project Tests
Here is my ConfigureServices :
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddHttpClient("csms", c =>
{
c.BaseAddress = Configuration.GetValue<Uri>("ExternalAPI:CSMS:Url");
});
services.AddSingleton(typeof(IdllClass), typeof(dllClass));
}
My class in dll
public class dllClass
{
private readonly IHttpClientFactory ClientFactory;
public dllClass(IHttpClientFactory clientFactory)
{
ClientFactory = clientFactory;
}
public async Task<Credentials> GetCredentials()
{
var request = new HttpRequestMessage(HttpMethod.Get, $"Security/GetCredentials");
using (var client = ClientFactory.CreateClient("csms"))
{
var response = await client.SendAsync(request);
}
return new Credentials();
}
}
I tried different method (moq, Substitute, ...) and the closest I got from my goal was this one below but it doesn't find the HttpClient by name :
public void GetCredentials()
{
var httpClientFactoryMock = Substitute.For<IHttpClientFactory>();
var service = new dllClass(httpClientFactoryMock);
var result = service.GetCredentials().Result;
}
How should I write that test ?
Thank you for your help
As the Comment states. You haven't mocked the CreateClient method. It should look something like the following:
public void GetCredentials()
{
var httpClientFactoryMock = Substitute.For<IHttpClientFactory>();
var csmsTestClient = new HttpClient();
httpClientFactoryMock.CreateClient("csms").Returns(csmsTestClient)
var service = new dllClass(httpClientFactoryMock);
var result = service.GetCredentials().Result;
}
Then you need to setup your HttpClient to point at whatever url you want to test.

Categories