public async Task RecommendedPassiveMocking() { // Given m_TeePeeBuilder.ForRequest("https://some.api/path/resource", HttpMethod.Get) .ContainingQueryParam("filter", "those") .Responds() .WithStatus(HttpStatusCode.OK) .WithBody(new { Things = new[] { new { Value = 10 } } }); var controller = new HttpClientFactoryBasicUsageController(m_TeePeeBuilder.Build().Manual().CreateHttpClientFactory()); // When var result = await controller.FireAndAct(); // Then Assert.NotNull(result); var okResult = Assert.IsType <OkObjectResult>(result); var resultValue = Assert.IsType <int>(okResult.Value); Assert.Equal(10, resultValue); }
public static T WithTypedClients <T, TClient1, TClient2>(TeePeeBuilder <TClient1> teePeeBuilder1, TeePeeBuilder <TClient2> teePeeBuilder2, Action <IServiceCollection> setup = null) where T : class where TClient1 : class where TClient2 : class { var serviceCollection = new ServiceCollection(); var teePeeMessageHandler1 = teePeeBuilder1.Build().HttpHandler; var teePeeMessageHandler2 = teePeeBuilder2.Build().HttpHandler; if (setup != null) { // Use your own Typed Client setup here setup(serviceCollection); serviceCollection.AddTrackingForTypedClient <TClient1>(teePeeMessageHandler1); serviceCollection.AddTrackingForTypedClient <TClient2>(teePeeMessageHandler2); } else { serviceCollection.AddHttpClient <TClient1>(_ => { }) .AddHttpMessageHandler(_ => teePeeMessageHandler1); serviceCollection.AddHttpClient <TClient2>(_ => { }) .AddHttpMessageHandler(_ => teePeeMessageHandler2); } serviceCollection.AddTransient <T>(); return(serviceCollection.BuildServiceProvider().GetService <T>()); }
public static T WithDefaultClient <T>(TeePeeBuilder teePeeBuilder) where T : class { var teePeeMessageHandler = teePeeBuilder.Build().HttpHandler; var serviceCollection = new ServiceCollection(); serviceCollection.AddHttpClient(Options.DefaultName) .AddHttpMessageHandler(_ => teePeeMessageHandler); serviceCollection.AddTransient <T>(); return(serviceCollection.BuildServiceProvider().GetService <T>()); }
public static T WithTypedClient <T, TClient>(TeePeeBuilder teePeeBuilder, Action <IServiceCollection> configureServices = null) where T : class where TClient : class { var serviceCollection = new ServiceCollection(); var teePeeMessageHandler = teePeeBuilder.Build().HttpHandler; if (configureServices != null) { // Use your own Typed Client setup here configureServices(serviceCollection); serviceCollection.AddTrackingForTypedClient <TClient>(teePeeMessageHandler); } else { // Otherwise, register the Typed Client here serviceCollection.AddHttpClient <TClient>(_ => { }) .AddHttpMessageHandler(_ => teePeeMessageHandler); } serviceCollection.AddTransient <T>(); return(serviceCollection.BuildServiceProvider().GetService <T>()); }
public async Task ManualInjection_RecommendedPassiveMocking() { // Given m_TeePeeBuilderOne.ForRequest("https://first.api/pathone/resourceone", HttpMethod.Get) .ContainingQueryParam("filter", "those") .Responds() .WithStatus(HttpStatusCode.OK) .WithBody(new { Things = new[] { new { Value = 10 } } }); m_TeePeeBuilderTwo.ForRequest("https://second.api/pathtwo/resourcetwo", HttpMethod.Get) .ContainingQueryParam("filter", "those") .Responds() .WithStatus(HttpStatusCode.OK) .WithBody(new { Things = new[] { new { Value = 30 } } }); var controller = new HttpClientFactoryMultipleNamedUsageController(new[] { m_TeePeeBuilderOne.Build().Manual("https://first.api"), m_TeePeeBuilderTwo.Build().Manual("https://second.api") } .ToHttpClientFactory()); // When var result = await controller.FireAndAct(); // Then Assert.NotNull(result); var okResult = Assert.IsType <OkObjectResult>(result); var resultValue = Assert.IsType <int>(okResult.Value); Assert.Equal(40, resultValue); }
private async Task <HttpResponseMessage> SendRequest(HttpRequestMessage httpRequestMessage) => await m_TrackingBuilder.Build().Manual().CreateClient().SendAsync(httpRequestMessage);