Exemple #1
0
        public async Task TestPollyManualIncorrectUri()
        {
            var tries = 0;

            var policy = HttpPolicyExtensions
                         .HandleTransientHttpError()
                         .OrResult(response => response.StatusCode == HttpStatusCode.NotFound)
                         .RetryAsync(3);

            var client = new Client(
                new ProtobufSerializationAdapter(),
                null,
                new Uri(UnitTests.LocalBaseUriString),
                logger: null,
                createHttpClient: UnitTests.GetTestClientFactory().CreateClient,
                sendHttpRequestFunc: (httpClient, httpRequestMessageFunc, logger, cancellationToken) =>
            {
                return(policy.ExecuteAsync(() =>
                {
                    var httpRequestMessage = httpRequestMessageFunc.Invoke();

                    //On the third try change the Url to a the correct one
                    if (tries == 2)
                    {
                        httpRequestMessage.RequestUri = new Uri("Person", UriKind.Relative);
                    }
                    tries++;
                    return httpClient.SendAsync(httpRequestMessage, cancellationToken);
                }));
            });

            var person = new Person {
                FirstName = "Bob", Surname = "Smith"
            };

            //Note the Uri here is deliberately incorrect. It will cause a 404 Not found response. This is to make sure that polly is working
            person = await client.PostAsync <Person, Person>(person, new Uri("person2", UriKind.Relative));

            Assert.AreEqual("Bob", person.FirstName);
            Assert.AreEqual(3, tries);
        }