public async Task CanGetAndSetRequestHeaders(string headerName, string headerValue, bool contentHeader)
        {
            IEnumerable <string> httpHeaderValues = null;

            var mockHandler = new MockHttpClientHandler(
                httpRequestMessage => {
                Assert.True(
                    httpRequestMessage.Headers.TryGetValues(headerName, out httpHeaderValues) ||
                    httpRequestMessage.Content.Headers.TryGetValues(headerName, out httpHeaderValues));
            });

            var transport = new HttpClientTransport(new HttpClient(mockHandler));
            var request   = transport.CreateRequest(null);

            request.SetRequestLine(HttpPipelineMethod.Get, new Uri("http://example.com:340"));
            request.Content = HttpPipelineRequestContent.Create(Array.Empty <byte>());

            request.AddHeader(headerName, headerValue);

            Assert.True(request.TryGetHeader(headerName, out var value));
            Assert.AreEqual(headerValue, value);

            Assert.True(request.TryGetHeader(headerName.ToUpper(), out value));
            Assert.AreEqual(headerValue, value);

            CollectionAssert.AreEqual(new []
            {
                new HttpHeader(headerName, headerValue),
            }, request.Headers);

            await ExecuteRequest(request, transport);

            Assert.AreEqual(headerValue, string.Join(",", httpHeaderValues));
        }
Exemplo n.º 2
0
        public async Task OriginalContentStreamIsReturnedIfNotAsync()
        {
            var content = new AsyncContent()
            {
                MemoryStream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 })
            };
            var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = content
            };
            var mockHandler = new MockHttpClientHandler(httpRequestMessage => Task.FromResult(httpResponseMessage));

            var     transport = new HttpClientTransport(new HttpClient(mockHandler));
            Request request   = transport.CreateRequest();

            request.Method = RequestMethod.Get;
            request.Uri.Reset(new Uri("https://example.com:340"));

            Response response = await ExecuteRequest(request, transport);

            content.CreateContentReadStreamAsyncCompletionSource.SetResult(null);
            Stream stream = response.ContentStream;

            Assert.AreSame(content.MemoryStream, stream);
        }
        public async Task DoesntDisposeContentIfStreamGotReplaced()
        {
            DisposeTrackingHttpContent disposeTrackingContent = new DisposeTrackingHttpContent();

            var mockHandler = new MockHttpClientHandler(
                httpRequestMessage =>
            {
                return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = disposeTrackingContent
                }));
            });

            var     transport = new HttpClientTransport(new HttpClient(mockHandler));
            Request request   = transport.CreateRequest();

            request.Method = RequestMethod.Get;
            request.Uri.Reset(new Uri("https://example.com:340"));

            Response response = await ExecuteRequest(request, transport);

            response.ContentStream = new MemoryStream();
            response.Dispose();

            Assert.False(disposeTrackingContent.IsDisposed);
        }
        public async Task CanSetRequestHeaders(string headerName, string headerValue, bool contentHeader)
        {
            IEnumerable <string> httpHeaderValues = null;

            var mockHandler = new MockHttpClientHandler(
                httpRequestMessage =>
            {
                Assert.True(
                    httpRequestMessage.Headers.TryGetValues(headerName, out httpHeaderValues) ||
                    httpRequestMessage.Content.Headers.TryGetValues(headerName, out httpHeaderValues));
            });

            var     transport = new HttpClientTransport(new HttpClient(mockHandler));
            Request request   = CreateRequest(transport);

            request.Headers.Add(headerName, "Random value");
            request.Headers.SetValue(headerName, headerValue);

            Assert.True(request.Headers.TryGetValue(headerName, out var value));
            Assert.AreEqual(headerValue, value);

            Assert.True(request.Headers.TryGetValue(headerName.ToUpper(), out value));
            Assert.AreEqual(headerValue, value);

            CollectionAssert.AreEqual(new[]
            {
                new HttpHeader(headerName, headerValue),
            }, request.Headers);

            await ExecuteRequest(request, transport);

            Assert.AreEqual(headerValue, string.Join(",", httpHeaderValues));
        }
Exemplo n.º 5
0
        public async Task CanGetAndSetResponseHeaders(string headerName, string headerValue, bool contentHeader)
        {
            var mockHandler = new MockHttpClientHandler(
                httpRequestMessage => {
                var responseMessage = new HttpResponseMessage((HttpStatusCode)200);

                if (contentHeader)
                {
                    responseMessage.Content = new StreamContent(new MemoryStream());
                    Assert.True(responseMessage.Content.Headers.TryAddWithoutValidation(headerName, headerValue));
                }
                else
                {
                    Assert.True(responseMessage.Headers.TryAddWithoutValidation(headerName, headerValue));
                }

                return(Task.FromResult(responseMessage));
            });

            var transport = new HttpClientTransport(new HttpClient(mockHandler));
            var request   = transport.CreateRequest(null);

            request.SetRequestLine(HttpPipelineMethod.Get, new Uri("http://example.com:340"));

            var response = await ExecuteRequest(request, transport);

            Assert.True(response.Headers.TryGetValue(headerName, out var value));
            Assert.AreEqual(headerValue, value);

            Assert.True(response.Headers.TryGetValue(headerName.ToUpper(), out value));
            Assert.AreEqual(headerValue, value);

            CollectionAssert.Contains(response.Headers, new HttpHeader(headerName, headerValue));
        }
        public async Task RequestContentIsNotDisposedOnSend()
        {
            var requests            = new List <HttpRequestMessage>();
            var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);

            var mockHandler = new MockHttpClientHandler(httpRequestMessage =>
            {
                requests.Add(httpRequestMessage);
                return(Task.FromResult(httpResponseMessage));
            });

            DisposeTrackingContent disposeTrackingContent = new DisposeTrackingContent();
            var transport = new HttpClientTransport(new HttpClient(mockHandler));

            using (Request request = transport.CreateRequest())
            {
                request.Content = disposeTrackingContent;
                request.Method  = RequestMethod.Get;
                request.Uri.Reset(new Uri("https://example.com:340"));

                await ExecuteRequest(request, transport);

                Assert.False(disposeTrackingContent.IsDisposed);
            }

            Assert.True(disposeTrackingContent.IsDisposed);
        }
Exemplo n.º 7
0
        public async Task RequestIdCanBeOverriden()
        {
            var mockHandler = new MockHttpClientHandler(httpRequestMessage => { });

            var transport = new HttpClientTransport(new HttpClient(mockHandler));
            var request   = transport.CreateRequest(null);

            request.RequestId = "123";
            request.SetRequestLine(HttpPipelineMethod.Get, new Uri("http://example.com:340"));

            var response = await ExecuteRequest(request, transport);

            Assert.AreEqual(request.RequestId, response.RequestId);
        }
Exemplo n.º 8
0
        public async Task RequestAndResponseHasRequestId()
        {
            var mockHandler = new MockHttpClientHandler(httpRequestMessage => { });

            var transport = new HttpClientTransport(new HttpClient(mockHandler));
            var request   = transport.CreateRequest(null);

            Assert.IsNotEmpty(request.RequestId);
            Assert.True(Guid.TryParse(request.RequestId, out _));
            request.SetRequestLine(HttpPipelineMethod.Get, new Uri("http://example.com:340"));

            var response = await ExecuteRequest(request, transport);

            Assert.AreEqual(request.RequestId, response.RequestId);
        }
Exemplo n.º 9
0
        public async Task SettingContentHeaderDoesNotSetContent(string headerName, string headerValue, bool contentHeader)
        {
            HttpContent httpMessageContent = null;
            var         mockHandler        = new MockHttpClientHandler(httpRequestMessage => { httpMessageContent = httpRequestMessage.Content; });

            var transport = new HttpClientTransport(new HttpClient(mockHandler));
            var request   = transport.CreateRequest(null);

            request.SetRequestLine(HttpPipelineMethod.Get, new Uri("http://example.com:340"));
            request.Headers.Add(headerName, headerValue);

            await ExecuteRequest(request, transport);

            Assert.Null(httpMessageContent);
        }
        public async Task RequestIdCanBeOverriden()
        {
            var mockHandler = new MockHttpClientHandler(httpRequestMessage => { });

            var     transport = new HttpClientTransport(new HttpClient(mockHandler));
            Request request   = transport.CreateRequest();

            request.ClientRequestId = "123";
            request.Method          = RequestMethod.Get;
            request.Uri.Reset(new Uri("https://example.com:340"));

            Response response = await ExecuteRequest(request, transport);

            Assert.AreEqual(request.ClientRequestId, response.ClientRequestId);
        }
        public async Task RequestAndResponseHasRequestId()
        {
            var mockHandler = new MockHttpClientHandler(httpRequestMessage => { });

            var     transport = new HttpClientTransport(new HttpClient(mockHandler));
            Request request   = transport.CreateRequest();

            Assert.IsNotEmpty(request.ClientRequestId);
            Assert.True(Guid.TryParse(request.ClientRequestId, out _));
            request.Method         = RequestMethod.Get;
            request.UriBuilder.Uri = new Uri("https://example.com:340");

            Response response = await ExecuteRequest(request, transport);

            Assert.AreEqual(request.ClientRequestId, response.ClientRequestId);
        }
Exemplo n.º 12
0
        public async Task SettingHeaderOverridesDefaultContentLength()
        {
            long contentLength = 0;
            var  mockHandler   = new MockHttpClientHandler(
                httpRequestMessage => contentLength = httpRequestMessage.Content.Headers.ContentLength.Value);

            var transport = new HttpClientTransport(new HttpClient(mockHandler));
            var request   = transport.CreateRequest(null);

            request.SetRequestLine(HttpPipelineMethod.Get, new Uri("http://example.com"));
            request.Content = HttpPipelineRequestContent.Create(new byte[10]);
            request.Headers.Add("Content-Length", "50");

            await ExecuteRequest(request, transport);

            Assert.AreEqual(50, contentLength);
        }
Exemplo n.º 13
0
        public async Task ReasonPhraseIsExposed()
        {
            var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
            {
                ReasonPhrase = "Custom ReasonPhrase"
            };
            var mockHandler = new MockHttpClientHandler(httpRequestMessage => Task.FromResult(httpResponseMessage));

            var     transport = new HttpClientTransport(new HttpClient(mockHandler));
            Request request   = transport.CreateRequest();

            request.SetRequestLine(HttpPipelineMethod.Get, new Uri("http://example.com:340"));

            Response response = await ExecuteRequest(request, transport);

            Assert.AreEqual("Custom ReasonPhrase", response.ReasonPhrase);
        }
Exemplo n.º 14
0
        public async Task ContentLengthIsSetForArrayContent(HttpPipelineRequestContent content, int expectedLength)
        {
            long contentLength = 0;
            var  mockHandler   = new MockHttpClientHandler(
                httpRequestMessage => contentLength = httpRequestMessage.Content.Headers.ContentLength.Value
                );

            var transport = new HttpClientTransport(new HttpClient(mockHandler));
            var request   = transport.CreateRequest(null);

            request.SetRequestLine(HttpPipelineMethod.Get, new Uri("http://example.com"));
            request.Content = content;

            await ExecuteRequest(request, transport);

            Assert.AreEqual(expectedLength, contentLength);
        }
Exemplo n.º 15
0
        public async Task SettingHeaderOverridesDefaultHost()
        {
            string host        = null;
            var    mockHandler = new MockHttpClientHandler(
                httpRequestMessage => {
                host = httpRequestMessage.Headers.Host;
            });

            var transport = new HttpClientTransport(new HttpClient(mockHandler));
            var request   = transport.CreateRequest(null);

            request.SetRequestLine(HttpPipelineMethod.Get, new Uri("http://example.com:340"));
            request.Headers.Add("Host", "example.org");

            await ExecuteRequest(request, transport);

            Assert.AreEqual("example.org", host);
        }
Exemplo n.º 16
0
        public async Task CanGetAndSetMethod(HttpPipelineMethod method, string expectedMethod)
        {
            HttpMethod httpMethod  = null;
            var        mockHandler = new MockHttpClientHandler(
                httpRequestMessage => {
                httpMethod = httpRequestMessage.Method;
            });

            var transport = new HttpClientTransport(new HttpClient(mockHandler));
            var request   = transport.CreateRequest(null);

            request.SetRequestLine(method, new Uri("http://example.com:340"));

            Assert.AreEqual(method, request.Method);

            await ExecuteRequest(request, transport);

            Assert.AreEqual(expectedMethod, httpMethod.Method);
        }
        public async Task CanGetAndSetMultiValueResponseHeaders(string headerName, string headerValue, bool contentHeader)
        {
            var anotherHeaderValue = headerValue + "1";
            var joinedHeaderValues = headerValue + "," + anotherHeaderValue;

            var mockHandler = new MockHttpClientHandler(
                httpRequestMessage =>
            {
                var responseMessage = new HttpResponseMessage((HttpStatusCode)200);

                if (contentHeader)
                {
                    responseMessage.Content = new StreamContent(new MemoryStream());
                    Assert.True(responseMessage.Content.Headers.TryAddWithoutValidation(headerName, headerValue));
                    Assert.True(responseMessage.Content.Headers.TryAddWithoutValidation(headerName, anotherHeaderValue));
                }
                else
                {
                    Assert.True(responseMessage.Headers.TryAddWithoutValidation(headerName, headerValue));
                    Assert.True(responseMessage.Headers.TryAddWithoutValidation(headerName, anotherHeaderValue));
                }

                return(Task.FromResult(responseMessage));
            });

            var     transport = new HttpClientTransport(new HttpClient(mockHandler));
            Request request   = transport.CreateRequest();

            request.Method = RequestMethod.Get;
            request.Uri.Reset(new Uri("https://example.com:340"));

            Response response = await ExecuteRequest(request, transport);

            Assert.True(response.Headers.Contains(headerName));

            Assert.True(response.Headers.TryGetValue(headerName, out var value));
            Assert.AreEqual(joinedHeaderValues, value);

            Assert.True(response.Headers.TryGetValues(headerName, out IEnumerable <string> values));
            CollectionAssert.AreEqual(new[] { headerValue, anotherHeaderValue }, values);

            CollectionAssert.Contains(response.Headers, new HttpHeader(headerName, joinedHeaderValues));
        }
Exemplo n.º 18
0
        public async Task CanGetAndSetUri()
        {
            Uri requestUri  = null;
            var mockHandler = new MockHttpClientHandler(
                httpRequestMessage => {
                requestUri = httpRequestMessage.RequestUri;
            });

            var expectedUri = new Uri("http://example.com:340");
            var transport   = new HttpClientTransport(new HttpClient(mockHandler));
            var request     = transport.CreateRequest(null);

            request.SetRequestLine(HttpPipelineMethod.Get, expectedUri);

            Assert.AreEqual(expectedUri.ToString(), request.UriBuilder.ToString());

            await ExecuteRequest(request, transport);

            Assert.AreEqual(expectedUri, requestUri);
        }
Exemplo n.º 19
0
        public async Task ContentStreamIsReturnedSynchronously()
        {
            var content = new AsyncContent()
            {
                MemoryStream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 })
            };
            var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = content
            };
            var mockHandler = new MockHttpClientHandler(httpRequestMessage => Task.FromResult(httpResponseMessage));

            var     transport = new HttpClientTransport(new HttpClient(mockHandler));
            Request request   = transport.CreateRequest();

            request.Method = RequestMethod.Get;
            request.Uri.Reset(new Uri("https://example.com:340"));

            Response response = await ExecuteRequest(request, transport);

            byte[]     data      = new byte[5];
            Stream     stream    = response.ContentStream;
            Task <int> firstRead = stream.ReadAsync(data, 0, 5);

            Assert.False(firstRead.IsCompleted);

            content.CreateContentReadStreamAsyncCompletionSource.SetResult(null);
            Assert.AreEqual(5, await firstRead);

            // Exercise stream features
            stream.Position = 3;
            stream.Seek(-1, SeekOrigin.Current);
            var secondReadLength = await stream.ReadAsync(data, 0, 5);

            Assert.AreEqual(3, secondReadLength);
            Assert.False(stream.CanWrite);
            Assert.True(stream.CanSeek);
            Assert.True(stream.CanRead);
            Assert.Throws <NotSupportedException>(() => stream.Write(null, 0, 0));
            Assert.Throws <NotSupportedException>(() => stream.SetLength(5));
        }
Exemplo n.º 20
0
        public async Task HostHeaderSetFromUri()
        {
            string host        = null;
            Uri    uri         = null;
            var    mockHandler = new MockHttpClientHandler(
                httpRequestMessage => {
                uri  = httpRequestMessage.RequestUri;
                host = httpRequestMessage.Headers.Host;
            });

            var transport = new HttpClientTransport(new HttpClient(mockHandler));
            var request   = transport.CreateRequest(null);

            request.SetRequestLine(HttpPipelineMethod.Get, new Uri("http://example.com:340"));

            await ExecuteRequest(request, transport);

            // HttpClientHandler would correctly set Host header from UriBuilder when it's not set explicitly
            Assert.AreEqual("http://example.com:340/", uri.ToString());
            Assert.Null(host);
        }
Exemplo n.º 21
0
        public async Task CanRemoveHeaders(string headerName, string headerValue, bool contentHeader)
        {
            var mockHandler = new MockHttpClientHandler(
                httpRequestMessage => {
                Assert.False(
                    httpRequestMessage.Headers.TryGetValues(headerName, out _) &&
                    httpRequestMessage.Content.Headers.TryGetValues(headerName, out _));
            });

            var     transport = new HttpClientTransport(new HttpClient(mockHandler));
            Request request   = CreateRequest(transport);

            request.Headers.Add(headerName, headerValue);
            Assert.True(request.Headers.Remove(headerName));
            Assert.False(request.Headers.Remove(headerName));

            Assert.False(request.Headers.TryGetValue(headerName, out _));
            Assert.False(request.Headers.TryGetValue(headerName.ToUpper(), out _));
            Assert.False(request.Headers.Contains(headerName.ToUpper()));

            await ExecuteRequest(request, transport);
        }
Exemplo n.º 22
0
        public async Task CanGetAndSetContent()
        {
            byte[] requestBytes = null;
            var    mockHandler  = new MockHttpClientHandler(
                async httpRequestMessage => {
                requestBytes = await httpRequestMessage.Content.ReadAsByteArrayAsync();
            });

            var bytes     = Encoding.ASCII.GetBytes("Hello world");
            var content   = HttpPipelineRequestContent.Create(bytes);
            var transport = new HttpClientTransport(new HttpClient(mockHandler));
            var request   = transport.CreateRequest(null);

            request.SetRequestLine(HttpPipelineMethod.Get, new Uri("http://example.com:340"));
            request.Content = content;

            Assert.AreEqual(content, request.Content);

            await ExecuteRequest(request, transport);

            CollectionAssert.AreEqual(bytes, requestBytes);
        }
        public async Task StreamRequestContentCanBeSentMultipleTimes()
        {
            var requests            = new List <HttpRequestMessage>();
            var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);

            var mockHandler = new MockHttpClientHandler(httpRequestMessage =>
            {
                requests.Add(httpRequestMessage);
                return(Task.FromResult(httpResponseMessage));
            });

            var     transport = new HttpClientTransport(new HttpClient(mockHandler));
            Request request   = transport.CreateRequest();

            request.Content = HttpPipelineRequestContent.Create(new MemoryStream(new byte[] { 1, 2, 3 }));
            request.SetRequestLine(RequestMethod.Get, new Uri("http://example.com:340"));

            await ExecuteRequest(request, transport);
            await ExecuteRequest(request, transport);

            Assert.AreEqual(2, requests.Count);
        }
        public async Task CanAddMultipleValuesToRequestHeader(string headerName, string headerValue, bool contentHeader)
        {
            var anotherHeaderValue = headerValue + "1";
            var joinedHeaderValues = headerValue + "," + anotherHeaderValue;

            IEnumerable <string> httpHeaderValues = null;

            var mockHandler = new MockHttpClientHandler(
                httpRequestMessage =>
            {
                Assert.True(
                    httpRequestMessage.Headers.TryGetValues(headerName, out httpHeaderValues) ||
                    httpRequestMessage.Content.Headers.TryGetValues(headerName, out httpHeaderValues));
            });

            var     transport = new HttpClientTransport(new HttpClient(mockHandler));
            Request request   = CreateRequest(transport);

            request.Headers.Add(headerName, headerValue);
            request.Headers.Add(headerName, anotherHeaderValue);

            Assert.True(request.Headers.Contains(headerName));

            Assert.True(request.Headers.TryGetValue(headerName, out var value));
            Assert.AreEqual(joinedHeaderValues, value);

            Assert.True(request.Headers.TryGetValues(headerName, out IEnumerable <string> values));
            CollectionAssert.AreEqual(new[] { headerValue, anotherHeaderValue }, values);

            CollectionAssert.AreEqual(new[]
            {
                new HttpHeader(headerName, joinedHeaderValues),
            }, request.Headers);

            await ExecuteRequest(request, transport);

            CollectionAssert.AreEqual(new[] { headerValue, anotherHeaderValue }, httpHeaderValues);
        }