Exemple #1
0
        public async Task AddsCorrelationIdToRequestWhenAvailable()
        {
            // Arrange
            var correlatingHttpClientHandler = new CorrelatingHttpClientHandler();

            correlatingHttpClientHandler.InnerHandler = new HttpClientHandler();

            var inspectingHandler = new RequestInspectingHandler();

            inspectingHandler.InnerHandler = correlatingHttpClientHandler;

            var correlationId = Guid.NewGuid();

            CallContext.LogicalSetData(WebApiCorrelationHandler.CallContextKey, correlationId);

            // Act
            using (var client = new HttpClient(inspectingHandler))
            {
                await client.GetAsync("https://www.nuget.org");
            }

            // Assert
            var request = inspectingHandler.Requests.FirstOrDefault();

            Assert.NotNull(request);
            Assert.True(request.Headers.Contains(WebApiCorrelationHandler.CorrelationIdHttpHeaderName));
            Assert.Equal(correlationId.ToString(), request.Headers.GetValues(WebApiCorrelationHandler.CorrelationIdHttpHeaderName).FirstOrDefault());
        }
Exemple #2
0
        public async Task LoadBalancesBetweenValidUrisForGetStringAsync()
        {
            var inspectingHandler = new RequestInspectingHandler();
            var client            = CreateWrapperClient(inspectingHandler);

            bool hasHitUri1 = false;
            bool hasHitUri2 = false;

            int numRequests = 0;

            while ((!hasHitUri1 || !hasHitUri2) && numRequests < 25)
            {
                numRequests++;
                var result = await client.GetStringAsync(new[] { ValidUri1, ValidUri2 });

                Assert.NotNull(result);
                if (!hasHitUri1)
                {
                    hasHitUri1 = inspectingHandler.Requests.Any(r => r.RequestUri == ValidUri1);
                }
                if (!hasHitUri2)
                {
                    hasHitUri2 = inspectingHandler.Requests.Any(r => r.RequestUri == ValidUri2);
                }
            }

            Assert.True(hasHitUri1, "The first valid Uri has not been hit within the limit of " + numRequests + " requests.");
            Assert.True(hasHitUri2, "The second valid Uri has not been hit within the limit of " + numRequests + " requests.");
        }
Exemple #3
0
        public async Task ReturnsStringForCollectionContainingValidUri()
        {
            var inspectingHandler = new RequestInspectingHandler();
            var client            = CreateWrapperClient(inspectingHandler);

            var result = await client.GetStringAsync(new[] { InvalidUri1, InvalidUri2, ValidUri1, InvalidUri3 });

            Assert.NotNull(result);
        }
        public async Task ReturnsStringForCollectionContainingValidUri()
        {
            var inspectingHandler = new RequestInspectingHandler();
            var client = CreateWrapperClient(inspectingHandler);

            var result = await client.GetStringAsync(new[] { InvalidUri1, InvalidUri2, ValidUri1, InvalidUri3 });

            Assert.NotNull(result);
        }
Exemple #5
0
        public async Task ReturnsSuccessResponseForCollectionContainingValidUri()
        {
            var inspectingHandler = new RequestInspectingHandler();
            var client            = CreateWrapperClient(inspectingHandler);

            var result = await client.GetAsync(new[] { InvalidUri1, InvalidUri2, ValidUri1, InvalidUri3 });

            Assert.True(result.IsSuccessStatusCode);
            Assert.Equal(ValidUri1, result.RequestMessage.RequestUri);
        }
        public async Task ReturnsSuccessResponseForCollectionContainingValidUri()
        {
            var inspectingHandler = new RequestInspectingHandler();
            var client = CreateWrapperClient(inspectingHandler);

            var result = await client.GetAsync(new[] { InvalidUri1, InvalidUri2, ValidUri1, InvalidUri3 });

            Assert.True(result.IsSuccessStatusCode);
            Assert.Equal(ValidUri1, result.RequestMessage.RequestUri);
        }
        public async Task DoesNotAddCorrelationIdToRequestWhenNotAvailable()
        {
            // Arrange
            var correlatingHttpClientHandler = new CorrelatingHttpClientHandler();
            correlatingHttpClientHandler.InnerHandler = new HttpClientHandler();

            var inspectingHandler = new RequestInspectingHandler();
            inspectingHandler.InnerHandler = correlatingHttpClientHandler;

            // Act
            using (var client = new HttpClient(inspectingHandler))
            {
                await client.GetAsync("https://www.nuget.org");
            }

            // Assert
            var request = inspectingHandler.Requests.FirstOrDefault();
            Assert.NotNull(request);
            Assert.False(request.Headers.Contains(WebApiCorrelationHandler.CorrelationIdHttpHeaderName));
        }
Exemple #8
0
        public async Task LoadBalancesBetweenValidUrisForGetAsync()
        {
            var inspectingHandler = new RequestInspectingHandler();
            var client            = CreateWrapperClient(inspectingHandler);

            int hitUri1Count = 0;
            int hitUri2Count = 0;
            var result       = await client.GetAsync(new[] { ValidUri1, ValidUri2 });

            Assert.NotNull(result);
            if (inspectingHandler.Requests.Any(r => r.RequestUri == ValidUri1))
            {
                hitUri1Count++;
            }
            if (inspectingHandler.Requests.Any(r => r.RequestUri == ValidUri2))
            {
                hitUri2Count++;
            }
            // Because the algorithm for choosing uri1 or uri2 is random asserting that the one or the other uri is not reliable
            // However the sum of requests between two uri requests needs to be larger or equal with 1
            Assert.True(hitUri1Count + hitUri2Count >= 1, $"The uri1 had {hitUri1Count} hits and the uri2 had {hitUri2Count} hits. The sum will need to be at least 1.");
        }
        public async Task LoadBalancesBetweenValidUrisForGetStringAsync()
        {
            var inspectingHandler = new RequestInspectingHandler();
            var client = CreateWrapperClient(inspectingHandler);

            bool hasHitUri1 = false;
            bool hasHitUri2 = false;

            int numRequests = 0;
            while ((!hasHitUri1 || !hasHitUri2) && numRequests < 25)
            {
                numRequests++;
                var result = await client.GetStringAsync(new[] { ValidUri1, ValidUri2 });

                Assert.NotNull(result);
                if (!hasHitUri1) hasHitUri1 = inspectingHandler.Requests.Any(r => r.RequestUri == ValidUri1);
                if (!hasHitUri2) hasHitUri2 = inspectingHandler.Requests.Any(r => r.RequestUri == ValidUri2);
            }

            Assert.True(hasHitUri1, "The first valid Uri has not been hit within the limit of " + numRequests + " requests.");
            Assert.True(hasHitUri2, "The second valid Uri has not been hit within the limit of " + numRequests + " requests.");
        }
Exemple #10
0
        public async Task DoesNotAddCorrelationIdToRequestWhenNotAvailable()
        {
            // Arrange
            var correlatingHttpClientHandler = new CorrelatingHttpClientHandler();

            correlatingHttpClientHandler.InnerHandler = new HttpClientHandler();

            var inspectingHandler = new RequestInspectingHandler();

            inspectingHandler.InnerHandler = correlatingHttpClientHandler;

            // Act
            using (var client = new HttpClient(inspectingHandler))
            {
                await client.GetAsync("https://www.nuget.org");
            }

            // Assert
            var request = inspectingHandler.Requests.FirstOrDefault();

            Assert.NotNull(request);
            Assert.False(request.Headers.Contains(WebApiCorrelationHandler.CorrelationIdHttpHeaderName));
        }
        public async Task AddsCorrelationIdToRequestWhenAvailable()
        {
            // Arrange
            var correlatingHttpClientHandler = new CorrelatingHttpClientHandler();
            correlatingHttpClientHandler.InnerHandler = new HttpClientHandler();

            var inspectingHandler = new RequestInspectingHandler();
            inspectingHandler.InnerHandler = correlatingHttpClientHandler;

            var correlationId = Guid.NewGuid();
            CallContext.LogicalSetData(WebApiCorrelationHandler.CallContextKey, correlationId);

            // Act
            using (var client = new HttpClient(inspectingHandler))
            {
                await client.GetAsync("https://www.nuget.org");
            }

            // Assert
            var request = inspectingHandler.Requests.FirstOrDefault();
            Assert.NotNull(request);
            Assert.True(request.Headers.Contains(WebApiCorrelationHandler.CorrelationIdHttpHeaderName));
            Assert.Equal(correlationId.ToString(), request.Headers.GetValues(WebApiCorrelationHandler.CorrelationIdHttpHeaderName).FirstOrDefault());
        }