public void ChangeMethod()
        {
            ChangeMethodRequestFactoryInterceptor interceptor = new ChangeMethodRequestFactoryInterceptor();

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

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

            Assert.AreEqual(HttpMethod.POST, requestMock.Method);
        }
        public void ChangeResponse()
        {
            ChangeResponseInterceptor interceptor = new ChangeResponseInterceptor();

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

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

            Assert.AreNotSame(this.responseMock, response);
        }
        private IClientHttpRequest CreateRequest(BasicSigningRequestInterceptor requestInterceptor, string path, HttpMethod method)
        {
            IClientHttpRequestFactory requestFactory = new InterceptingClientHttpRequestFactory(
                new WebClientHttpRequestFactory(),
                new IClientHttpRequestInterceptor[] { requestInterceptor });

            Uri uri = new Uri(BASE_URL + path);
            IClientHttpRequest request = requestFactory.CreateRequest(uri, method);
            Assert.AreEqual(method, request.Method, "Invalid HTTP method");
            Assert.AreEqual(uri, request.Uri, "Invalid HTTP URI");

            return request;
        }
        public void NoSyncExecution()
        {
            NoOpExecutionInterceptor   interceptor1 = new NoOpExecutionInterceptor();
            NoOpRequestSyncInterceptor interceptor2 = new NoOpRequestSyncInterceptor();

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

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

            Assert.IsFalse(interceptor2.invoked);
            Assert.IsFalse(requestMock.executed);
        }
        private IClientHttpRequest CreateRequest(BasicSigningRequestInterceptor requestInterceptor, string path, HttpMethod method)
        {
            IClientHttpRequestFactory requestFactory = new InterceptingClientHttpRequestFactory(
                new WebClientHttpRequestFactory(),
                new IClientHttpRequestInterceptor[] { requestInterceptor });

            Uri uri = new Uri(BASE_URL + path);
            IClientHttpRequest request = requestFactory.CreateRequest(uri, method);

            Assert.AreEqual(method, request.Method, "Invalid HTTP method");
            Assert.AreEqual(uri, request.Uri, "Invalid HTTP URI");

            return(request);
        }
        public void Creation()
        {
            NoOpRequestFactoryInterceptor interceptor1 = new NoOpRequestFactoryInterceptor();
            NoOpRequestFactoryInterceptor interceptor2 = new NoOpRequestFactoryInterceptor();
            requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
                new IClientHttpRequestInterceptor[] { interceptor1, interceptor2 });

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

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

            IClientHttpResponse response = request.Execute();
            Assert.IsTrue(this.requestMock.executed);
            Assert.AreSame(this.responseMock, response);
        }
        public void ChangeHeaders()
        {
            ChangeHeaderInterceptor interceptor = new ChangeHeaderInterceptor();

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

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

            Assert.IsNotNull(requestMock.Headers.Get("AfterCreation"));
            Assert.IsNull(requestMock.Headers.Get("BeforeSyncExecution"));
            Assert.IsNull(responseMock.Headers.Get("AfterSyncExecution"));

            request.Execute();

            Assert.IsNotNull(requestMock.Headers.Get("BeforeSyncExecution"));
            Assert.IsNotNull(responseMock.Headers.Get("AfterSyncExecution"));
        }
        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 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 ChangeResponseAsync()
        {
            ManualResetEvent manualEvent = new ManualResetEvent(false);
            Exception        exception   = null;
            Object           state       = new Object();

            ChangeResponseInterceptor interceptor = new ChangeResponseInterceptor();

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

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

            request.ExecuteAsync(state, delegate(ClientHttpRequestCompletedEventArgs args)
            {
                try
                {
                    Assert.IsNull(args.Error, "Invalid response");
                    Assert.IsFalse(args.Cancelled, "Invalid response");
                    Assert.AreNotSame(this.responseMock, args.Response, "Invalid response");
                    Assert.AreEqual(state, args.UserState);
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
                finally
                {
                    manualEvent.Set();
                }
            });

            manualEvent.WaitOne();
            if (exception != null)
            {
                throw exception;
            }
        }
        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 ChangeBody()
        {
            ChangeBodyInterceptor interceptor = new ChangeBodyInterceptor();

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

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

            request.Execute();

            MemoryStream stream = new MemoryStream();

            requestMock.Body(stream);
            stream.Position = 0;
            string result = null;

            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            Assert.AreEqual("New body", result);
        }
        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 ChangeHeaders()
        {
            ChangeHeaderInterceptor interceptor = new ChangeHeaderInterceptor();
            requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
                new IClientHttpRequestInterceptor[] { interceptor });

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

            Assert.IsNotNull(requestMock.Headers.Get("AfterCreation"));
            Assert.IsNull(requestMock.Headers.Get("BeforeSyncExecution"));
            Assert.IsNull(responseMock.Headers.Get("AfterSyncExecution"));

            request.Execute();

            Assert.IsNotNull(requestMock.Headers.Get("BeforeSyncExecution"));
            Assert.IsNotNull(responseMock.Headers.Get("AfterSyncExecution"));
        }
        public void ChangeMethod()
        {
            ChangeMethodRequestFactoryInterceptor interceptor = new ChangeMethodRequestFactoryInterceptor();
            requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
                new IClientHttpRequestInterceptor[] { interceptor });

            IClientHttpRequest request = requestFactory.CreateRequest(new Uri("http://example.com"), HttpMethod.GET);
            Assert.AreEqual(HttpMethod.POST, requestMock.Method);
        }
        public void ChangeResponseAsync()
        {
            ManualResetEvent manualEvent = new ManualResetEvent(false);
            Exception exception = null;
            Object state = new Object();

            ChangeResponseInterceptor interceptor = new ChangeResponseInterceptor();
            requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
                new IClientHttpRequestInterceptor[] { interceptor });

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

            manualEvent.WaitOne();
            if (exception != null)
            {
                throw exception;
            }
        }
        public void ChangeResponse()
        {
            ChangeResponseInterceptor interceptor = new ChangeResponseInterceptor();
            requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
                new IClientHttpRequestInterceptor[] { interceptor });

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

            Assert.AreNotSame(this.responseMock, response);
        }
        public void ChangeBody()
        {
            ChangeBodyInterceptor interceptor = new ChangeBodyInterceptor();
            requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock,
                new IClientHttpRequestInterceptor[] { interceptor });

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

            MemoryStream stream = new MemoryStream();
            requestMock.Body(stream);
            stream.Position = 0;
            string result = null;
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            Assert.AreEqual("New body", result);
        }