コード例 #1
0
ファイル: SendRequestTests.cs プロジェクト: xgame92/RestEase
        public void SendRequestThrowsIfResponseIsBad()
        {
            var messageHandler = new MockHttpMessageHandler();
            var httpClient     = new HttpClient(messageHandler)
            {
                BaseAddress = new Uri("http://api.com")
            };
            var requester = new PublicRequester(httpClient);

            var requestInfo = new RequestInfo(HttpMethod.Post, "foo");

            var responseMessage = new HttpResponseMessage();

            responseMessage.Headers.Add("Foo", "bar");
            responseMessage.StatusCode = HttpStatusCode.NotFound;
            responseMessage.Content    = new StringContent("hello");

            messageHandler.ResponseMessage = Task.FromResult(responseMessage);

            var aggregateException = Assert.Throws <AggregateException>(() => requester.SendRequestAsync(requestInfo, true).Wait());
            var e = Assert.IsType <ApiException>(aggregateException.InnerException);

            Assert.Equal(HttpMethod.Post, e.RequestMethod);
            Assert.Equal("http://api.com/foo", e.RequestUri.ToString());
            Assert.Equal(HttpStatusCode.NotFound, e.StatusCode);
            Assert.True(e.Headers.Contains("Foo"));
            Assert.True(e.HasContent);
            Assert.Equal("hello", e.Content);
        }
コード例 #2
0
        public void AllowsNullPath()
        {
            var messageHandler = new MockHttpMessageHandler();
            var httpClient     = new HttpClient(messageHandler)
            {
                BaseAddress = new Uri("http://api.example.com/base/")
            };
            var requester = new PublicRequester(httpClient);

            var requestInfo = new RequestInfo(HttpMethod.Get, null);

            messageHandler.ResponseMessage = Task.FromResult(new HttpResponseMessage());
            requester.SendRequestAsync(requestInfo, true).Wait();
            Assert.Equal("http://api.example.com/base/", messageHandler.Request.RequestUri.ToString());
        }
コード例 #3
0
        public void AllowsPathWithLeadingSlash()
        {
            var messageHandler = new MockHttpMessageHandler();
            var httpClient     = new HttpClient(messageHandler)
            {
                BaseAddress = new Uri("http://api.example.com/base/")
            };
            var requester = new PublicRequester(httpClient);

            var requestInfo = new RequestInfo(HttpMethod.Get, "/foo");

            messageHandler.ResponseMessage = Task.FromResult(new HttpResponseMessage());

            var response = requester.SendRequestAsync(requestInfo).Result;

            Assert.Equal("http://api.example.com/base/foo", messageHandler.Request.RequestUri.ToString());
        }
コード例 #4
0
ファイル: SendRequestTests.cs プロジェクト: xgame92/RestEase
        public void SendRequestAsyncSendsRequest()
        {
            var messageHandler = new MockHttpMessageHandler();
            var httpClient     = new HttpClient(messageHandler)
            {
                BaseAddress = new Uri("http://api.com")
            };
            var requester = new PublicRequester(httpClient);

            var requestInfo = new RequestInfo(HttpMethod.Get, "foo");

            var responseMessage = new HttpResponseMessage();

            messageHandler.ResponseMessage = Task.FromResult(responseMessage);

            var response = requester.SendRequestAsync(requestInfo, true).Result;

            Assert.Equal("http://api.com/foo", messageHandler.Request.RequestUri.ToString());
            Assert.Equal(responseMessage, response);
        }
コード例 #5
0
        public void SendRequestDoesNotThrowIfResponseIsBadButAllowAnyStatusCodeSpecified()
        {
            var messageHandler = new MockHttpMessageHandler();
            var httpClient     = new HttpClient(messageHandler)
            {
                BaseAddress = new Uri("http://api.com")
            };
            var requester = new PublicRequester(httpClient);

            var requestInfo = new RequestInfo(HttpMethod.Get, "foo");

            requestInfo.AllowAnyStatusCode = true;

            var responseMessage = new HttpResponseMessage();

            responseMessage.StatusCode = HttpStatusCode.NotFound;

            messageHandler.ResponseMessage = Task.FromResult(responseMessage);

            var response = requester.SendRequestAsync(requestInfo).Result;

            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }
コード例 #6
0
        public void AllowsPathWithLeadingSlash()
        {
            var messageHandler = new MockHttpMessageHandler();
            var httpClient = new HttpClient(messageHandler) { BaseAddress = new Uri("http://api.example.com/base/") };
            var requester = new PublicRequester(httpClient);

            var requestInfo = new RequestInfo(HttpMethod.Get, "/foo");

            messageHandler.ResponseMessage = Task.FromResult(new HttpResponseMessage());

            var response = requester.SendRequestAsync(requestInfo).Result;
            Assert.Equal("http://api.example.com/base/foo", messageHandler.Request.RequestUri.ToString());
        }
コード例 #7
0
        public void SendRequestDoesNotThrowIfResponseIsBadButAllowAnyStatusCodeSpecified()
        {
            var messageHandler = new MockHttpMessageHandler();
            var httpClient = new HttpClient(messageHandler) { BaseAddress = new Uri("http://api.com") };
            var requester = new PublicRequester(httpClient);

            var requestInfo = new RequestInfo(HttpMethod.Get, "foo");
            requestInfo.AllowAnyStatusCode = true;

            var responseMessage = new HttpResponseMessage();
            responseMessage.StatusCode = HttpStatusCode.NotFound;

            messageHandler.ResponseMessage = Task.FromResult(responseMessage);

            var response = requester.SendRequestAsync(requestInfo).Result;
            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }
コード例 #8
0
        public void SendRequestThrowsIfResponseIsBad()
        {
            var messageHandler = new MockHttpMessageHandler();
            var httpClient = new HttpClient(messageHandler) { BaseAddress = new Uri("http://api.com") };
            var requester = new PublicRequester(httpClient);

            var requestInfo = new RequestInfo(HttpMethod.Get, "foo");

            var responseMessage = new HttpResponseMessage();
            responseMessage.Headers.Add("Foo", "bar");
            responseMessage.StatusCode = HttpStatusCode.NotFound;
            responseMessage.Content = new StringContent("hello");

            messageHandler.ResponseMessage = Task.FromResult(responseMessage);

            var aggregateException = Assert.Throws<AggregateException>(() => requester.SendRequestAsync(requestInfo).Wait());
            var e = Assert.IsType<ApiException>(aggregateException.InnerException);

            Assert.Equal(HttpStatusCode.NotFound, e.StatusCode);
            Assert.True(e.Headers.Contains("Foo"));
            Assert.True(e.HasContent);
            Assert.Equal("hello", e.Content);
        }
コード例 #9
0
        public void SendRequestAsyncSendsRequest()
        {
            var messageHandler = new MockHttpMessageHandler();
            var httpClient = new HttpClient(messageHandler) { BaseAddress = new Uri("http://api.com") };
            var requester = new PublicRequester(httpClient);

            var requestInfo = new RequestInfo(HttpMethod.Get, "foo");

            var responseMessage = new HttpResponseMessage();
            messageHandler.ResponseMessage = Task.FromResult(responseMessage);

            var response = requester.SendRequestAsync(requestInfo).Result;

            Assert.Equal("http://api.com/foo", messageHandler.Request.RequestUri.ToString());
            Assert.Equal(responseMessage, response);
        }