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 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);
        }
        public async Task OnRequest_IncrementsHistogramCountAndSum()
        {
            var registry = Metrics.NewCustomRegistry();

            var options = new HttpClientRequestDurationOptions
            {
                Registry = registry
            };

            var handler = new HttpClientRequestDurationHandler(options, HttpClientIdentity.Default);

            // As we are not using the HttpClientProvider for constructing our pipeline, we need to do this manually.
            handler.InnerHandler = new HttpClientHandler();

            var client = new HttpClient(handler);
            await client.GetAsync(ConnectivityCheck.Url);

            Assert.AreEqual(1, handler._metric.WithLabels("GET", ConnectivityCheck.Host, HttpClientIdentity.Default.Name, ConnectivityCheck.ExpectedResponseCode).Count);
            Assert.IsTrue(handler._metric.WithLabels("GET", ConnectivityCheck.Host, HttpClientIdentity.Default.Name, ConnectivityCheck.ExpectedResponseCode).Sum > 0);
        }
        public async Task OnRequest_IncrementsHistogramCountAndSum()
        {
            var registry = Metrics.NewCustomRegistry();

            var options = new HttpClientRequestDurationOptions
            {
                Registry = registry
            };

            var handler = new HttpClientRequestDurationHandler(options);

            // As we are not using the HttpClientProvider for constructing our pipeline, we need to do this manually.
            handler.InnerHandler = new HttpClientHandler();

            var client = new HttpClient(handler);
            await client.GetAsync("http://www.google.com");

            Assert.AreEqual(1, handler._metric.WithLabels("GET", "www.google.com").Count);
            Assert.IsTrue(handler._metric.WithLabels("GET", "www.google.com").Sum > 0);
        }