コード例 #1
0
        public void ResponsesIncludeContentHeaders()
        {
            using (var client = new HttpAbstractionClientFactory().Create(new TimeSpan(0, 5, 0), CancellationToken.None))
            {
                client.Uri = new Uri("http://httpbin.org/get");
                client.Method = HttpMethod.Get;

                var responseTask = client.SendAsync();
                responseTask.Wait();
                var response = responseTask.Result;

                Assert.IsNotNull(response);
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

                Assert.IsTrue(response.Headers.Contains("Content-Length"));
                Assert.IsTrue(response.Headers.Contains("Content-Type"));

                Assert.IsTrue(int.Parse(response.Headers["Content-Length"].First()) > 100);
                Assert.AreEqual("application/json", response.Headers["Content-Type"].First());

                Assert.IsNotNull(response.Content);
                var content = TestHelper.GetStringFromStream(response.Content);
                Assert.AreNotEqual(0, content.Length);
            }
        }
コード例 #2
0
        public void CanMakeGetRequest()
        {
            using (var client = new HttpAbstractionClientFactory().Create())
            {
                client.Uri = new Uri("http://httpbin.org/get");
                client.Method = HttpMethod.Get;

                var responseTask = client.SendAsync();
                responseTask.Wait();
                var response = responseTask.Result;

                Assert.IsNotNull(response);
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

                Assert.IsNotNull(response.Content);
                var content = TestHelper.GetStringFromStream(response.Content);
                Assert.AreNotEqual(0, content.Length);
            }
        }
コード例 #3
0
        public void CanMakePutRequest()
        {
            using (var client = new HttpAbstractionClientFactory().Create())
            {
                using (var content = TestHelper.CreateStream("Test Text"))
                {
                    client.Uri = new Uri("http://httpbin.org/put");
                    client.Method = HttpMethod.Put;
                    client.Content = content;

                    var responseTask = client.SendAsync();
                    responseTask.Wait();
                    var response = responseTask.Result;

                    Assert.IsNotNull(response);
                    Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
                    
                    Assert.IsNotNull(response.Content);

                    var stringContent = TestHelper.GetStringFromStream(response.Content);
                    Assert.AreNotEqual(0, stringContent.Length);
                    Assert.IsTrue(stringContent.Contains("\"data\": \"Test Text\""));
                }
            }
        }
コード例 #4
0
        public void RequestsCanSendHeaders()
        {
            using (var client = new HttpAbstractionClientFactory().Create())
            {
                client.Uri = new Uri("http://httpbin.org/get");
                client.Headers.Add("X-Test-Header","TEST");

                //Added in order to force httpbin not to cache the responses from any prior get requests/unit tests. 
                client.Headers.Add("Cache-Control", "max-age=0");

                var responseTask = client.SendAsync();
                
                responseTask.Wait();
                var response = responseTask.Result;
                Assert.IsNotNull(response);
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

                Assert.IsNotNull(response.Content);
                var content = TestHelper.GetStringFromStream(response.Content);
                Assert.AreNotEqual(0, content.Length);

                Assert.IsTrue(content.Contains("\"X-Test-Header\": \"TEST\""));
            }
        }
コード例 #5
0
        public void RequestsHandlesHttpErrors()
        {
            using (var client = new HttpAbstractionClientFactory().Create())
            {
                client.Uri = new Uri("http://httpbin.org/status/404");

                var responseTask = client.SendAsync();
                responseTask.Wait();
                var response = responseTask.Result;

                Assert.IsNotNull(response);
                Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);

                var stringContent = TestHelper.GetStringFromStream(response.Content);

                Assert.AreEqual(string.Empty, stringContent);
            }
        }
コード例 #6
0
        public void RequestsHonorsCancelationToken()
        {
            var token = new CancellationToken(true);
            var startTime = DateTime.Now;

            try
            {
                using (var client = new HttpAbstractionClientFactory().Create(token))
                {
                    client.Uri = new Uri("http://httpbin.org/delay/30000");
                    client.Timeout = TimeSpan.FromSeconds(31);

                    var responseTask = client.SendAsync();
                    responseTask.Wait();
                }
            }
            catch (AggregateException ex)
            {
                var inner = ex.InnerException;
               Assert.IsTrue(DateTime.Now - startTime < TimeSpan.FromSeconds(30));
               Assert.IsTrue(inner.Message.Contains("was canceled"));
            }
        }
コード例 #7
0
        public void RequestsHonorsClientSideTimeout()
        {
             try
             {
                 using (var client = new HttpAbstractionClientFactory().Create(TimeSpan.FromMilliseconds(100)))
                 {
                     client.Uri = new Uri("http://httpbin.org/delay/30000");

                     var responseTask = client.SendAsync();
                     responseTask.Wait();
                 }
             }
             catch (AggregateException ex)
             {
                 var inner = ex.InnerException;
                 Assert.IsInstanceOfType(inner,typeof(TimeoutException));
                 Assert.IsTrue(inner.Message.Contains("failed to complete in the given timeout period"));
             }
        }
コード例 #8
0
        public void CanMakeMultipleGetRequestsWithSameClient()
        {
            using (var client = new HttpAbstractionClientFactory().Create())
            {

                client.Uri = new Uri("http://httpbin.org/get");
                client.Method = HttpMethod.Get;

                var responseTask = client.SendAsync();
                responseTask.Wait();

                responseTask = client.SendAsync();
                responseTask.Wait();
            }
        }