コード例 #1
0
        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);
        }
コード例 #2
0
        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 BeforeExecution()
        {
            NoOpRequestBeforeInterceptor interceptor1 = new NoOpRequestBeforeInterceptor();
            NoOpRequestBeforeInterceptor interceptor2 = new NoOpRequestBeforeInterceptor();
            requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
                new IClientHttpRequestInterceptor[] { interceptor1, interceptor2 });

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

            Assert.IsFalse(interceptor1.invoked);
            Assert.IsFalse(interceptor2.invoked);
            Assert.IsTrue(this.requestFactoryMock.created);
            Assert.IsFalse(this.requestMock.executed);

            IClientHttpResponse response = request.Execute();

            Assert.IsTrue(interceptor1.invoked);
            Assert.IsTrue(interceptor2.invoked);
            Assert.IsTrue(this.requestMock.executed);
            Assert.AreSame(this.responseMock, response);
        }
        public void BeforeExecution()
        {
            NoOpRequestBeforeInterceptor interceptor1 = new NoOpRequestBeforeInterceptor();
            NoOpRequestBeforeInterceptor interceptor2 = new NoOpRequestBeforeInterceptor();

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

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

            Assert.IsFalse(interceptor1.invoked);
            Assert.IsFalse(interceptor2.invoked);
            Assert.IsTrue(this.requestFactoryMock.created);
            Assert.IsFalse(this.requestMock.executed);

            IClientHttpResponse response = request.Execute();

            Assert.IsTrue(interceptor1.invoked);
            Assert.IsTrue(interceptor2.invoked);
            Assert.IsTrue(this.requestMock.executed);
            Assert.AreSame(this.responseMock, response);
        }
コード例 #5
0
        public void UsingInterceptors()
        {
            NoOpRequestSyncInterceptor.counter   = 0;
            NoOpRequestBeforeInterceptor.counter = 0;
            NoOpRequestSyncInterceptor   interceptor1 = new NoOpRequestSyncInterceptor();
            NoOpRequestBeforeInterceptor interceptor2 = new NoOpRequestBeforeInterceptor();
            NoOpRequestSyncInterceptor   interceptor3 = new NoOpRequestSyncInterceptor();

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

            string result = template.PostForObject <string>("user", "Lisa Baia");

            Assert.AreEqual("3", result, "Invalid content");

            Assert.AreEqual(1, interceptor1.HandleRequestCounter);
            Assert.AreEqual(2, interceptor3.HandleRequestCounter);
            Assert.AreEqual(3, interceptor3.HandleResponseCounter);
            Assert.AreEqual(4, interceptor1.HandleResponseCounter);
            Assert.AreEqual(1, interceptor2.HandleCounter);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        public void UsingInterceptors()
        {
            NoOpRequestSyncInterceptor.counter = 0;
            NoOpRequestBeforeInterceptor.counter = 0;
            NoOpRequestSyncInterceptor interceptor1 = new NoOpRequestSyncInterceptor();
            NoOpRequestBeforeInterceptor interceptor2 = new NoOpRequestBeforeInterceptor();
            NoOpRequestSyncInterceptor interceptor3 = new NoOpRequestSyncInterceptor();
            template.RequestInterceptors.Add(interceptor1);
            template.RequestInterceptors.Add(interceptor2);
            template.RequestInterceptors.Add(interceptor3);

            string result = template.PostForObject<string>("user", "Lisa Baia");
            Assert.AreEqual("3", result, "Invalid content");

            Assert.AreEqual(1, interceptor1.HandleRequestCounter);
            Assert.AreEqual(2, interceptor3.HandleRequestCounter);
            Assert.AreEqual(3, interceptor3.HandleResponseCounter);
            Assert.AreEqual(4, interceptor1.HandleResponseCounter);
            Assert.AreEqual(1, interceptor2.HandleCounter);
        }