public void OnRequest_AwaitsRequestAndRecordsDuration() { var registry = Metrics.NewCustomRegistry(); var options = new HttpClientRequestDurationOptions { Registry = registry }; var handler = new HttpClientRequestDurationHandler(options, HttpClientIdentity.Default); // Use a mock client handler so we can control when the task completes var mockHttpClientHandler = new MockHttpClientHandler(); handler.InnerHandler = mockHttpClientHandler; var client = new HttpClient(handler); client.GetAsync(ConnectivityCheck.Url); // There should be no duration metric recorded unless the task is completed Assert.AreEqual(0, handler._metric.WithLabels("GET", ConnectivityCheck.Host, HttpClientIdentity.Default.Name, ConnectivityCheck.ExpectedResponseCode).Count); mockHttpClientHandler.Complete(); Assert.AreEqual(1, handler._metric.WithLabels("GET", ConnectivityCheck.Host, HttpClientIdentity.Default.Name, ConnectivityCheck.ExpectedResponseCode).Count); }
public async Task OnRequest_AwaitsResponseDisposal_ThenRecordsDuration() { var registry = Metrics.NewCustomRegistry(); var options = new HttpClientResponseDurationOptions { Registry = registry }; var handler = new HttpClientResponseDurationHandler(options, HttpClientIdentity.Default); // Use a mock client handler so we can control when the task completes var mockHttpClientHandler = new MockHttpClientHandler(); handler.InnerHandler = mockHttpClientHandler; var client = new HttpClient(handler); var requestTask = client.GetAsync(ConnectivityCheck.Url, HttpCompletionOption.ResponseHeadersRead); // There should be no duration metric recorded unless the task is completed. Assert.AreEqual(0, handler._metric.WithLabels("GET", ConnectivityCheck.Host, HttpClientIdentity.Default.Name, ConnectivityCheck.ExpectedResponseCode).Count); mockHttpClientHandler.Complete(); // There should be no duration metric recorded unless the response is actually read or disposed. Assert.AreEqual(0, handler._metric.WithLabels("GET", ConnectivityCheck.Host, HttpClientIdentity.Default.Name, ConnectivityCheck.ExpectedResponseCode).Count); var response = await requestTask; response.Dispose(); // Now that we have disposed it, it should show up. Assert.AreEqual(1, handler._metric.WithLabels("GET", ConnectivityCheck.Host, HttpClientIdentity.Default.Name, ConnectivityCheck.ExpectedResponseCode).Count); }
public void OnRequest_AwaitsRequestAndRecordsDuration() { var registry = Metrics.NewCustomRegistry(); var options = new HttpClientRequestDurationOptions { Registry = registry }; var handler = new HttpClientRequestDurationHandler(options); // Use a mock client handler so we can control when the task completes var mockHttpClientHandler = new MockHttpClientHandler(); handler.InnerHandler = mockHttpClientHandler; var client = new HttpClient(handler); client.GetAsync("http://www.google.com"); // There should be no duration metric recorded unless the task is completed Assert.AreEqual(0, handler._metric.WithLabels("GET", "www.google.com").Count); mockHttpClientHandler.Complete(); Assert.AreEqual(1, handler._metric.WithLabels("GET", "www.google.com").Count); }
protected void SetupHttpClientHandlerSendAsync(HttpResponseMessage response, params object[] arguments) { MockHttpClientHandler .Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", arguments) .ReturnsAsync(response); }
public void TestGetUSDGBPOnNewYearsDay2018() { ILoggerFactory loggerFactory = new LoggerFactory(); var environmentServiceMock = new Mock <IEnvironmentService>(); string fakeKey = "abcd"; environmentServiceMock .Setup(es => es.GetFixerKey()) .Returns(fakeKey); string date = "2018-01-01"; decimal GbpPerEur = 0.889131m; decimal UsdPerEur = 1.201496m; decimal UsdPerGbp = UsdPerEur / GbpPerEur; var mockedResponsesByRequestUri = new Dictionary <string, HttpResponseMessage>() { { $"http://data.fixer.io/{date}?access_key={fakeKey}", new HttpResponseMessage { StatusCode = System.Net.HttpStatusCode.OK, Content = new StringContent ( $"{{ base: \"EUR\", date: \"{date}\", rates: {{ GBP: {GbpPerEur}, USD: {UsdPerEur} }} }}" ) } } }; HttpClientHandler httpClientHandler = new MockHttpClientHandler(mockedResponsesByRequestUri); IHttpClientFactory httpClientFactory = new MockHttpClientFactory(httpClientHandler); var service = new FixerIOCurrencyExchangeService( loggerFactory, environmentServiceMock.Object, httpClientFactory ); decimal gbpUsdRate = service.GetExchangeRate("GBP", "USD", new DateTime(2018, 1, 1)); Assert.AreEqual(UsdPerGbp, gbpUsdRate); }
public void Setup() { var httpHandler = new MockHttpClientHandler(); httpHandler .AddRequest(HttpMethod.Get, root) .ContentsJson("{\"Version\": \"1\", \"About\": \"Materials\"}"); httpHandler .AddRequest(HttpMethod.Get, root + "Materials") .ContentsJson("[{\"MaterialId\" : \"Id1\"}, {\"MaterialId\" : \"Id2\"}]"); httpHandler .AddRequest(HttpMethod.Post, root + "Materials"); httpHandler .AddRequest(HttpMethod.Get, root + "Materials/Id1") .ContentsJson("{\"MaterialId\" : \"Id1\"}"); httpHandler .AddRequest(HttpMethod.Put, root + "Materials/Id1"); httpHandler .AddRequest(HttpMethod.Delete, root + "Materials/Id1"); this.handler = new(httpHandler); Assert.IsNotNull(this.handler); }
protected void VerifyHttpClientHandlerSendAsync(Times times, params object[] args) { MockHttpClientHandler .Protected() .Verify <Task <HttpResponseMessage> >("SendAsync", times, args); }