Ejemplo n.º 1
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.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);
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 3
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, true).Result;

            Assert.Equal("http://api.com/foo", messageHandler.Request.RequestUri.ToString());
            Assert.Equal(responseMessage, response);
        }
Ejemplo n.º 4
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")
            {
                AllowAnyStatusCode = true
            };
            var responseMessage = new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.NotFound
            };

            messageHandler.ResponseMessage = Task.FromResult(responseMessage);

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

            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }