public void UsingInterceptorsTaskAsync()
        {
            NoOpRequestAsyncInterceptor.counter  = 0;
            NoOpRequestSyncInterceptor.counter   = 0;
            NoOpRequestBeforeInterceptor.counter = 0;
            NoOpRequestAsyncInterceptor  interceptor1 = new NoOpRequestAsyncInterceptor();
            NoOpRequestSyncInterceptor   interceptor2 = new NoOpRequestSyncInterceptor();
            NoOpRequestBeforeInterceptor interceptor3 = new NoOpRequestBeforeInterceptor();
            NoOpRequestAsyncInterceptor  interceptor4 = new NoOpRequestAsyncInterceptor();

            template.RequestInterceptors.Add(interceptor1);
            template.RequestInterceptors.Add(interceptor2);
            template.RequestInterceptors.Add(interceptor3);
            template.RequestInterceptors.Add(interceptor4);

            template.ExchangeAsync <string>("user", HttpMethod.POST, new HttpEntity("Lisa Baia"), CancellationToken.None)
            .ContinueWith(task =>
            {
                Assert.IsFalse(task.IsCanceled, "Invalid response");
                Assert.IsFalse(task.IsFaulted, "Invalid response");
                Assert.AreEqual("3", task.Result.Body, "Invalid content");
            })
            .Wait();

            Assert.AreEqual(1, interceptor1.HandleRequestCounter);
            Assert.AreEqual(2, interceptor4.HandleRequestCounter);
            Assert.AreEqual(3, interceptor4.HandleResponseCounter);
            Assert.AreEqual(4, interceptor1.HandleResponseCounter);
            Assert.AreEqual(1, interceptor3.HandleCounter);
            Assert.AreEqual(0, interceptor2.HandleRequestCounter);
            Assert.AreEqual(0, interceptor2.HandleResponseCounter);
        }
        public void UsingInterceptorsAsync()
        {
            ManualResetEvent manualEvent = new ManualResetEvent(false);
            Exception        exception   = null;

            NoOpRequestAsyncInterceptor.counter  = 0;
            NoOpRequestSyncInterceptor.counter   = 0;
            NoOpRequestBeforeInterceptor.counter = 0;
            NoOpRequestAsyncInterceptor  interceptor1 = new NoOpRequestAsyncInterceptor();
            NoOpRequestSyncInterceptor   interceptor2 = new NoOpRequestSyncInterceptor();
            NoOpRequestBeforeInterceptor interceptor3 = new NoOpRequestBeforeInterceptor();
            NoOpRequestAsyncInterceptor  interceptor4 = new NoOpRequestAsyncInterceptor();

            template.RequestInterceptors.Add(interceptor1);
            template.RequestInterceptors.Add(interceptor2);
            template.RequestInterceptors.Add(interceptor3);
            template.RequestInterceptors.Add(interceptor4);

            template.PostForObjectAsync <string>("user", "Lisa Baia",
                                                 delegate(RestOperationCompletedEventArgs <string> args)
            {
                try
                {
                    Assert.IsNull(args.Error, "Invalid response");
                    Assert.IsFalse(args.Cancelled, "Invalid response");
                    Assert.AreEqual("3", args.Response, "Invalid content");
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
                finally
                {
                    manualEvent.Set();
                }
            });

            manualEvent.WaitOne();
            if (exception != null)
            {
                throw exception;
            }

            Assert.AreEqual(1, interceptor1.HandleRequestCounter);
            Assert.AreEqual(2, interceptor4.HandleRequestCounter);
            Assert.AreEqual(3, interceptor4.HandleResponseCounter);
            Assert.AreEqual(4, interceptor1.HandleResponseCounter);
            Assert.AreEqual(1, interceptor3.HandleCounter);
            Assert.AreEqual(0, interceptor2.HandleRequestCounter);
            Assert.AreEqual(0, interceptor2.HandleResponseCounter);
        }
        public void AsyncExecution()
        {
            ManualResetEvent manualEvent = new ManualResetEvent(false);
            Exception        exception   = null;

            NoOpRequestAsyncInterceptor interceptor1 = new NoOpRequestAsyncInterceptor();
            ChangeHeaderInterceptor     interceptor2 = new ChangeHeaderInterceptor();

            requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
                                                                      new IClientHttpRequestInterceptor[] { interceptor1, interceptor2 });

            IClientHttpRequest request = requestFactory.CreateRequest(new Uri("http://example.com"), HttpMethod.GET);

            request.ExecuteAsync(null, delegate(ClientHttpRequestCompletedEventArgs args)
            {
                try
                {
                    Assert.IsNull(args.Error, "Invalid response");
                    Assert.IsFalse(args.Cancelled, "Invalid response");
                    Assert.AreSame(this.responseMock, args.Response, "Invalid response");
                    Assert.IsNotNull(args.Response.Headers.Get("AfterAsyncExecution"));
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
                finally
                {
                    manualEvent.Set();
                }
            });

            manualEvent.WaitOne();
            if (exception != null)
            {
                throw exception;
            }

            Assert.IsTrue(interceptor1.invoked);
            Assert.IsTrue(this.requestMock.executed);
            Assert.IsNotNull(responseMock.Headers.Get("AfterAsyncExecution"));
        }
        public void AsyncExecution()
        {
            ManualResetEvent manualEvent = new ManualResetEvent(false);
            Exception exception = null;

            NoOpRequestAsyncInterceptor interceptor1 = new NoOpRequestAsyncInterceptor();
            ChangeHeaderInterceptor interceptor2 = new ChangeHeaderInterceptor();
            requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
                new IClientHttpRequestInterceptor[] { interceptor1, interceptor2 });

            IClientHttpRequest request = requestFactory.CreateRequest(new Uri("http://example.com"), HttpMethod.GET);
            request.ExecuteAsync(null, delegate(ClientHttpRequestCompletedEventArgs args)
            {
                try
                {
                    Assert.IsNull(args.Error, "Invalid response");
                    Assert.IsFalse(args.Cancelled, "Invalid response");
                    Assert.AreSame(this.responseMock, args.Response, "Invalid response");
                    Assert.IsNotNull(args.Response.Headers.Get("AfterAsyncExecution"));
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
                finally
                {
                    manualEvent.Set();
                }
            });

            manualEvent.WaitOne();
            if (exception != null)
            {
                throw exception;
            }

            Assert.IsTrue(interceptor1.invoked);
            Assert.IsTrue(this.requestMock.executed);
            Assert.IsNotNull(responseMock.Headers.Get("AfterAsyncExecution"));
        }
        public void NoAsyncExecution()
        {
            ManualResetEvent manualEvent = new ManualResetEvent(false);
            Exception        exception   = null;

            NoOpExecutionInterceptor    interceptor1 = new NoOpExecutionInterceptor();
            NoOpRequestAsyncInterceptor interceptor2 = new NoOpRequestAsyncInterceptor();

            requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
                                                                      new IClientHttpRequestInterceptor[] { interceptor1, interceptor2 });

            IClientHttpRequest request = requestFactory.CreateRequest(new Uri("http://example.com"), HttpMethod.GET);

            request.ExecuteAsync(null, delegate(ClientHttpRequestCompletedEventArgs args)
            {
                try
                {
                    Assert.Fail("No Execution");
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
                finally
                {
                    manualEvent.Set();
                }
            });

            //manualEvent.WaitOne();
            if (exception != null)
            {
                throw exception;
            }

            Assert.IsFalse(interceptor2.invoked);
            Assert.IsFalse(requestMock.executed);
        }
        public void UsingInterceptorsTaskAsync()
        {
            NoOpRequestAsyncInterceptor.counter = 0;
            NoOpRequestSyncInterceptor.counter = 0;
            NoOpRequestBeforeInterceptor.counter = 0;
            NoOpRequestAsyncInterceptor interceptor1 = new NoOpRequestAsyncInterceptor();
            NoOpRequestSyncInterceptor interceptor2 = new NoOpRequestSyncInterceptor();
            NoOpRequestBeforeInterceptor interceptor3 = new NoOpRequestBeforeInterceptor();
            NoOpRequestAsyncInterceptor interceptor4 = new NoOpRequestAsyncInterceptor();
            template.RequestInterceptors.Add(interceptor1);
            template.RequestInterceptors.Add(interceptor2);
            template.RequestInterceptors.Add(interceptor3);
            template.RequestInterceptors.Add(interceptor4);

            template.ExchangeAsync<string>("user", HttpMethod.POST, new HttpEntity("Lisa Baia"), CancellationToken.None)
                .ContinueWith(task =>
                {
                    Assert.IsFalse(task.IsCanceled, "Invalid response");
                    Assert.IsFalse(task.IsFaulted, "Invalid response");
                    Assert.AreEqual("3", task.Result.Body, "Invalid content");
                })
                .Wait();

            Assert.AreEqual(1, interceptor1.HandleRequestCounter);
            Assert.AreEqual(2, interceptor4.HandleRequestCounter);
            Assert.AreEqual(3, interceptor4.HandleResponseCounter);
            Assert.AreEqual(4, interceptor1.HandleResponseCounter);
            Assert.AreEqual(1, interceptor3.HandleCounter);
            Assert.AreEqual(0, interceptor2.HandleRequestCounter);
            Assert.AreEqual(0, interceptor2.HandleResponseCounter);
        }
        public void UsingInterceptorsAsync()
        {
            ManualResetEvent manualEvent = new ManualResetEvent(false);
            Exception exception = null;

            NoOpRequestAsyncInterceptor.counter = 0;
            NoOpRequestSyncInterceptor.counter = 0;
            NoOpRequestBeforeInterceptor.counter = 0;
            NoOpRequestAsyncInterceptor interceptor1 = new NoOpRequestAsyncInterceptor();
            NoOpRequestSyncInterceptor interceptor2 = new NoOpRequestSyncInterceptor();
            NoOpRequestBeforeInterceptor interceptor3 = new NoOpRequestBeforeInterceptor();
            NoOpRequestAsyncInterceptor interceptor4 = new NoOpRequestAsyncInterceptor();
            template.RequestInterceptors.Add(interceptor1);
            template.RequestInterceptors.Add(interceptor2);
            template.RequestInterceptors.Add(interceptor3);
            template.RequestInterceptors.Add(interceptor4);

            template.PostForObjectAsync<string>("user", "Lisa Baia",
                delegate(RestOperationCompletedEventArgs<string> args)
                {
                    try
                    {
                        Assert.IsNull(args.Error, "Invalid response");
                        Assert.IsFalse(args.Cancelled, "Invalid response");
                        Assert.AreEqual("3", args.Response, "Invalid content");
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                    finally
                    {
                        manualEvent.Set();
                    }
                });

            manualEvent.WaitOne();
            if (exception != null)
            {
                throw exception;
            }

            Assert.AreEqual(1, interceptor1.HandleRequestCounter);
            Assert.AreEqual(2, interceptor4.HandleRequestCounter);
            Assert.AreEqual(3, interceptor4.HandleResponseCounter);
            Assert.AreEqual(4, interceptor1.HandleResponseCounter);
            Assert.AreEqual(1, interceptor3.HandleCounter);
            Assert.AreEqual(0, interceptor2.HandleRequestCounter);
            Assert.AreEqual(0, interceptor2.HandleResponseCounter);
        }
        public void NoAsyncExecution()
        {
            ManualResetEvent manualEvent = new ManualResetEvent(false);
            Exception exception = null;

            NoOpExecutionInterceptor interceptor1 = new NoOpExecutionInterceptor();
            NoOpRequestAsyncInterceptor interceptor2 = new NoOpRequestAsyncInterceptor();
            requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
                new IClientHttpRequestInterceptor[] { interceptor1, interceptor2 });

            IClientHttpRequest request = requestFactory.CreateRequest(new Uri("http://example.com"), HttpMethod.GET);
            request.ExecuteAsync(null, delegate(ClientHttpRequestCompletedEventArgs args)
            {
                try
                {
                    Assert.Fail("No Execution");
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
                finally
                {
                    manualEvent.Set();
                }
            });

            //manualEvent.WaitOne();
            if (exception != null)
            {
                throw exception;
            }

            Assert.IsFalse(interceptor2.invoked);
            Assert.IsFalse(requestMock.executed);
        }