Exemple #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);
        }
Exemple #2
0
        public void ThrowsWithNullBaseAddressAndNullPath()
        {
            var requester = new PublicRequester(new HttpClient()
            {
                BaseAddress = null
            });
            var requestInfo = new RequestInfo(HttpMethod.Get, null);

            Assert.Throws <FormatException>(() => requester.ConstructUri(null, null, requestInfo));
        }
Exemple #3
0
        public void AllowsNullBaseAddressAndNonNullPath()
        {
            var requester = new PublicRequester(new HttpClient()
            {
                BaseAddress = null
            });
            var requestInfo = new RequestInfo(HttpMethod.Get, null);
            var uri         = requester.ConstructUri(null, "foo", requestInfo);

            Assert.Equal("http://foo/", uri.ToString());
        }
Exemple #4
0
        public void CombinesUriParts(string baseAddress, string basePath, string path, string expected)
        {
            var requester = new PublicRequester(new HttpClient()
            {
                BaseAddress = baseAddress == null ? null : new Uri(baseAddress),
            });
            var requestInfo = new RequestInfo(HttpMethod.Get, path)
            {
                BasePath = basePath
            };
            var uri = requester.ConstructUri(basePath, path, requestInfo);

            Assert.Equal(expected, uri.ToString());
        }
Exemple #5
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());
        }
Exemple #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());
        }
        public void RequestInfoHttpMessagePropertiesAddedToRequestMessage()
        {
            var messageHandler = new MockHttpMessageHandler {
                ResponseMessage = Task.FromResult(new HttpResponseMessage())
            };
            var httpClient = new HttpClient(messageHandler);
            var requester  = new PublicRequester(httpClient);

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

            requestInfo.AddHttpRequestMessagePropertyProperty("key1", "value1");
            requestInfo.AddHttpRequestMessagePropertyParameter("key2", "value2");
            requester.RequestWithResponseMessageAsync(requestInfo).Wait();

            Assert.Equal(2, messageHandler.Request.Properties.Count);
            Assert.Equal("value1", messageHandler.Request.Properties["key1"]);
            Assert.Equal("value2", messageHandler.Request.Properties["key2"]);
        }
Exemple #8
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);
        }
Exemple #9
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);
        }
        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());
        }
        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);
        }
        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);
        }
        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);
        }