public void MultipleExecute()
        {
            IClientHttpRequest request = this.CreateRequest("/status/ok", HttpMethod.GET);

            request.Execute();
            request.Execute();
        }
        public void Echo()
        {
            IClientHttpRequest request = this.CreateRequest("/echo", HttpMethod.PUT);

            request.Headers.ContentType = new MediaType("text", "plain", "utf-8");
            String headerName   = "MyHeader";
            String headerValue1 = "value1";

            request.Headers.Add(headerName, headerValue1);
            String headerValue2 = "value2";

            request.Headers.Add(headerName, headerValue2);

            byte[] body = Encoding.UTF8.GetBytes("Hello World");
            request.Body = delegate(Stream stream)
            {
                stream.Write(body, 0, body.Length);
            };

            using (IClientHttpResponse response = request.Execute())
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Invalid status code");
                Assert.AreEqual("value1,value2", response.Headers[headerName], "Header values not found");
                using (BinaryReader reader = new BinaryReader(response.Body))
                {
                    byte[] result = reader.ReadBytes((int)response.Headers.ContentLength);
                    Assert.AreEqual(body, result, "Invalid body");
                }
            }
        }
Пример #3
0
        public void NtlmAuthorizationKO()
        {
            IClientHttpRequest request = this.CreateRequest("/ntlm/echo", HttpMethod.PUT);

            request.Headers.ContentLength = 0;
            using (IClientHttpResponse response = request.Execute())
            {
                Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode, "Invalid status code");
            }
        }
        public void Status()
        {
            IClientHttpRequest request = this.CreateRequest("/status/notfound", HttpMethod.GET);

            using (IClientHttpResponse response = request.Execute())
            {
                Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode, "Invalid status code");
                Assert.AreEqual("Status NotFound", response.StatusDescription, "Invalid status description");
            }
        }
        public void BasicAuthorizationOK()
        {
            IClientHttpRequest request = this.CreateRequest(
                new BasicSigningRequestInterceptor("login", "password"), "/basic/echo", HttpMethod.PUT);

            request.Headers.ContentLength = 0;
            using (IClientHttpResponse response = request.Execute())
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Invalid status code");
            }
        }
        private void AssertHttpMethod(String path, HttpMethod method)
        {
            IClientHttpRequest request = this.CreateRequest("/methods/" + path, method);

            request.Headers.ContentLength = 0;

            using (IClientHttpResponse response = request.Execute())
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Invalid status code");
                Assert.AreEqual(path, response.StatusDescription, "Invalid status description");
            }
        }
Пример #7
0
        public void BasicAuthorizationKO()
        {
            this.webRequestFactory.Credentials = new NetworkCredential("bruno", "password");

            IClientHttpRequest request = this.CreateRequest("/basic/echo", HttpMethod.PUT);

            request.Headers.ContentLength = 0;
            using (IClientHttpResponse response = request.Execute())
            {
                Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode, "Invalid status code");
            }
        }
Пример #8
0
 private void ExpectGetResponse()
 {
     Expect.Call <IClientHttpResponse>(request.Execute()).Return(response);
     #region Instrumentation
     if (LOG.IsDebugEnabled)
     {
         Expect.Call <HttpStatusCode>(response.StatusCode).Return(HttpStatusCode.OK);
         Expect.Call <string>(response.StatusDescription).Return("OK");
     }
     #endregion
     Expect.Call(response.Dispose);
 }
Пример #9
0
        public void NtlmAuthorizationOK()
        {
            this.webRequestFactory.UseDefaultCredentials = true;

            IClientHttpRequest request = this.CreateRequest("/ntlm/echo", HttpMethod.PUT);

            request.Headers.ContentLength = 0;
            using (IClientHttpResponse response = request.Execute())
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Invalid status code");
            }
        }
        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);
        }
Пример #11
0
        public void RangeHeader()
        {
            IClientHttpRequest request = this.CreateRequest("/echo", HttpMethod.PUT);

            request.Headers.ContentLength = 0;
            request.Headers["Range"]      = "bytes=128-1024";

            using (IClientHttpResponse response = request.Execute())
            {
                Assert.AreEqual("bytes=128-1024", response.Headers["Range"]);
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "OK");
            }
        }
        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);
        }
Пример #13
0
        public void Timeout()
        {
            this.webRequestFactory.Timeout = 1000;

            IClientHttpRequest request = this.CreateRequest("/sleep/2", HttpMethod.GET);

            try
            {
                request.Execute();
                Assert.Fail("Execute should failed !");
            }
            catch (Exception ex)
            {
                WebException webEx = ex as WebException;
                Assert.IsNotNull(webEx, "Invalid response exception");
                Assert.AreEqual(WebExceptionStatus.Timeout, webEx.Status, "Invalid response exception status");
            }
        }
        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"));
        }
Пример #15
0
        public void SpecialHeaders() // related to HttpWebRequest implementation
        {
            IClientHttpRequest request = this.CreateRequest("/echo", HttpMethod.PUT);

            request.Headers.Accept        = new MediaType[] { MediaType.ALL };
            request.Headers.ContentType   = MediaType.TEXT_PLAIN;
            request.Headers["Connection"] = "close";
            request.Headers.ContentLength = 0;
#if NET_4_0
            request.Headers.Date = DateTime.Now;
#endif
            request.Headers["Expect"]       = "bla";
            request.Headers.IfModifiedSince = DateTime.Now;
            request.Headers["Referer"]      = "http://www.springframework.net/";
            //request.Headers["Transfer-Encoding"] = "Identity";
            request.Headers["User-Agent"] = "Unit tests";

            using (IClientHttpResponse response = request.Execute())
            {
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Invalid status code");
            }
        }
        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 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);
        }