/// <summary>
        /// A subtest for testing execute when an exception is thrown during sending the request, with or without
        /// back-off. If back-off handler is attached to the service's message handler, there are going to be several
        /// retries (up to 2 minutes).
        /// </summary>
        /// <param name="backOff">Indicates if back-off handler is attached to the service.</param>
        private void SubtestExecute_ThrowException(bool backOff)
        {
            var handler     = new MockMessageHandler(true);
            var initializer = new BaseClientService.Initializer()
            {
                HttpClientFactory = new MockHttpClientFactory(handler)
            };

            // by default back-off is used, so disable it in case backOff is false
            if (!backOff)
            {
                initializer.DefaultExponentialBackOffPolicy = BaseClientService.ExponentialBackOffPolicy.None;
            }

            using (var service = new MockClientService(initializer))
            {
                var request = new TestClientServiceRequest(service, "GET", null);
                Assert.Throws <InvalidOperationMockException>(() => request.Execute());

                // if back-off is enabled, we use 2 minutes maximum wait time for a request, so we should make lg(120)
                // + 1 calls
                int calls = backOff ? (int)Math.Ceiling(Math.Log(120, 2) + 1) : 1;
                Assert.That(handler.Calls, Is.EqualTo(calls));
            }
        }
        public void Execute_UnicodeCharacters()
        {
            var handler = new TestBodyMessageHnalder()
            {
                GZipEnabled    = false,
                ResponseObject = new MockResponse {
                    Id = 100, Name = @"مرحبا العالم"
                },
                ExpectedRequestObject = new MockRequest {
                    Name = @"مرحبا العالم! 您好,世界!"
                }
            };
            var initializer = new BaseClientService.Initializer()
            {
                GZipEnabled       = false,
                HttpClientFactory = new MockHttpClientFactory(handler)
            };

            using (var service = new MockClientService(initializer))
            {
                handler.Serializer = service.Serializer;

                var request  = new TestClientServiceRequest(service, "GET", handler.ExpectedRequestObject);
                var response = request.Execute();
                Assert.That(handler.Calls, Is.EqualTo(1));
                // the returned response should contain ETag, check that the service add the right ETag property on
                // the response
                handler.ResponseObject.ETag = handler.ResponseETag;
                Assert.That(response, Is.EqualTo(handler.ResponseObject));
            }
        }
        /// <summary> A subtest for testing GZip and sync-async calls. </summary>
        private void SubtestExecute_GZip(bool gzip, bool async)
        {
            var handler = new TestBodyMessageHnalder()
            {
                GZipEnabled    = gzip,
                ResponseObject = new MockResponse {
                    Id = 100, Name = "sample name"
                },
                ExpectedRequestObject = new MockRequest {
                    Name = "long long name"
                }
            };
            var initializer = new BaseClientService.Initializer()
            {
                GZipEnabled       = gzip,
                HttpClientFactory = new MockHttpClientFactory(handler)
            };

            using (var service = new MockClientService(initializer))
            {
                handler.Serializer = service.Serializer;

                var          request  = new TestClientServiceRequest(service, "POST", handler.ExpectedRequestObject);
                MockResponse response = null;
                if (async)
                {
                    var task = request.ExecuteAsync();
                    response = task.Result;
                    Assert.AreNotEqual(Thread.CurrentThread.ManagedThreadId, handler.ThreadId);
                }
                else
                {
                    response = request.Execute();
                    Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, handler.ThreadId);
                }

                // NOTICE: even if GZipEnabled is true, we don't need to extract the real string from the GZip stream,
                // because in a real request we use HttpClientHandler which its AutomaticDecompression is set to
                // System.Net.DecompressionMethods.GZip.

                Assert.That(handler.Calls, Is.EqualTo(1));
                // the returned response should contain ETag, check that the service add the right ETag property on
                // the response
                handler.ResponseObject.ETag = handler.ResponseETag;
                Assert.That(response, Is.EqualTo(handler.ResponseObject));
            }
        }
        public void Execute_DisposeService()
        {
            var handler     = new MockMessageHandler();
            var initializer = new BaseClientService.Initializer()
            {
                HttpClientFactory = new MockHttpClientFactory(handler)
            };

            TestClientServiceRequest request;

            using (var service = new MockClientService(initializer))
            {
                request = new TestClientServiceRequest(service, "POST", new MockRequest());
            }

            // the service was disposed before the request was made (and the message handler as well). As a result an
            // exception should be thrown before we try to send the request
            Assert.Throws <ObjectDisposedException>(() => request.Execute());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// A subtest for testing Execute when an exception is thrown while sending the request. This is tested with
        /// and without back-off. If back-off handler is attached to the service's message handler, there should be 3
        /// tries (the default value of <seealso cref="ConfigurableMessageHandler.NumTries"/>) before the operation
        /// fails.
        /// </summary>
        /// <param name="backOff">Indicates if back-off handler is attached to the service.</param>
        private void SubtestExecute_ThrowException(bool backOff)
        {
            var handler     = new MockMessageHandler(true);
            var initializer = new BaseClientService.Initializer()
            {
                HttpClientFactory = new MockHttpClientFactory(handler)
            };

            // Set the default exponential back-off policy by the input.
            initializer.DefaultExponentialBackOffPolicy = backOff ?
                                                          ExponentialBackOffPolicy.Exception : ExponentialBackOffPolicy.None;

            using (var service = new MockClientService(initializer))
            {
                var request = new TestClientServiceRequest(service, "GET", null);
                Assert.Throws <InvalidOperationMockException>(() => request.Execute());

                int calls = backOff ? service.HttpClient.MessageHandler.NumTries : 1;
                Assert.That(handler.Calls, Is.EqualTo(calls));
            }
        }
        public void Execute_Error()
        {
            var handler     = new ErrorMessageHanlder();
            var initializer = new BaseClientService.Initializer()
            {
                HttpClientFactory = new MockHttpClientFactory(handler)
            };

            using (var service = new MockClientService(initializer))
            {
                var request = new TestClientServiceRequest(service, "GET", null);
                try
                {
                    request.Execute();
                    Assert.Fail();
                }
                catch (Exception ex)
                {
                    Assert.True(ex.Message.Contains(handler.ExpectedError), "Error message is invalid");
                }
                Assert.That(handler.Calls, Is.EqualTo(1));
            }
        }