Beispiel #1
0
        public async Task SendAsync_HttpTracingEnabled_Succeeds()
        {
            using (var listener = new TestEventListener("Microsoft-System-Net-Http", EventLevel.Verbose))
            {
                var events = new ConcurrentQueue <EventWrittenEventArgs>();
                await listener.RunWithCallbackAsync(events.Enqueue, async() =>
                {
                    // Exercise various code paths to get coverage of tracing
                    using (var client = new HttpClient())
                    {
                        // Do a get to a loopback server
                        await LoopbackServer.CreateServerAsync(async(server, url) =>
                        {
                            await TestHelper.WhenAllCompletedOrAnyFailed(
                                LoopbackServer.ReadRequestAndSendResponseAsync(server),
                                client.GetAsync(url));
                        });

                        // Do a post to a remote server
                        byte[] expectedData        = Enumerable.Range(0, 20000).Select(i => (byte)i).ToArray();
                        HttpContent content        = new ByteArrayContent(expectedData);
                        content.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(expectedData);
                        using (HttpResponseMessage response = await client.PostAsync(HttpTestServers.RemoteEchoServer, content))
                        {
                            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                        }
                    }
                });

                // We don't validate receiving specific events, but rather that we do at least
                // receive some events, and that enabling tracing doesn't cause other failures
                // in processing.
                Assert.InRange(events.Count, 1, int.MaxValue);
            }
        }
Beispiel #2
0
        private async Task PostAsync_Redirect_LargePayload_Helper(Configuration.Http.RemoteServer remoteServer, int statusCode, bool expectRedirectToPost)
        {
            using (var fs = new FileStream(
                       Path.Combine(Path.GetTempPath(), Path.GetTempFileName()),
                       FileMode.Create,
                       FileAccess.ReadWrite,
                       FileShare.None,
                       0x1000,
                       FileOptions.DeleteOnClose))
            {
                string contentString = string.Join("", Enumerable.Repeat("Content", 100000));
                byte[] contentBytes  = Encoding.UTF32.GetBytes(contentString);
                fs.Write(contentBytes, 0, contentBytes.Length);
                fs.Flush(flushToDisk: true);
                fs.Position = 0;

                Uri redirectUri = remoteServer.RedirectUriForDestinationUri(
                    statusCode: statusCode,
                    destinationUri: remoteServer.VerifyUploadUri,
                    hops: 1);
                var content = new StreamContent(fs);

                // Compute MD5 of request body data. This will be verified by the server when it receives the request.
                if (PlatformDetection.IsBrowser)
                {
                    // [ActiveIssue("https://github.com/dotnet/runtime/issues/37669", TestPlatforms.Browser)]
                    content.Headers.Add("Content-MD5-Skip", "browser");
                }
                else
                {
                    content.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(contentBytes);
                }

                using (HttpClient client = CreateHttpClientForRemoteServer(remoteServer))
                    using (HttpResponseMessage response = await client.PostAsync(redirectUri, content))
                    {
                        try
                        {
                            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                        }
                        catch
                        {
                            _output.WriteLine($"{(int)response.StatusCode} {response.ReasonPhrase}");
                            throw;
                        }

                        if (expectRedirectToPost)
                        {
                            IEnumerable <string> headerValue = response.Headers.GetValues("X-HttpRequest-Method");
                            Assert.Equal("POST", headerValue.First());
                        }
                    }
            }
        }
Beispiel #3
0
        public async Task PostAsync_CallMethod_StreamContent(Configuration.Http.RemoteServer remoteServer, HttpContent content, byte[] expectedData)
        {
            using (HttpClient client = CreateHttpClientForRemoteServer(remoteServer))
            {
                content.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(expectedData);

                using (HttpResponseMessage response = await client.PostAsync(remoteServer.VerifyUploadUri, content))
                {
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                }
            }
        }
Beispiel #4
0
        public async Task PostAsync_CallMethod_StreamContent(Uri remoteServer, Stream requestContentStream, byte[] expectedData)
        {
            using (var client = new HttpClient())
            {
                HttpContent content = new StreamContent(requestContentStream);
                content.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(expectedData);

                using (HttpResponseMessage response = await client.PostAsync(remoteServer, content))
                {
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                }
            }
        }
Beispiel #5
0
        public async Task PostAsync_CallMethod_UnicodeStringContent(Configuration.Http.RemoteServer remoteServer)
        {
            using (HttpClient client = CreateHttpClientForRemoteServer(remoteServer))
            {
                string data    = "\ub4f1\uffc7\u4e82\u67ab4\uc6d4\ud1a0\uc694\uc77c\uffda3\u3155\uc218\uffdb";
                var    content = new StringContent(data, Encoding.UTF8);
                content.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(data);

                using (HttpResponseMessage response = await client.PostAsync(remoteServer.VerifyUploadUri, content))
                {
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                }
            }
        }
Beispiel #6
0
        private async Task PostHelper(
            Configuration.Http.RemoteServer remoteServer,
            string requestBody,
            HttpContent requestContent,
            bool useContentLengthUpload,
            bool useChunkedEncodingUpload)
        {
            using (HttpClient client = CreateHttpClientForRemoteServer(remoteServer))
            {
                if (requestContent != null)
                {
                    if (useContentLengthUpload)
                    {
                        // Ensure that Content-Length is populated (see https://github.com/dotnet/runtime/issues/25086)
                        requestContent.Headers.ContentLength = requestContent.Headers.ContentLength;
                    }
                    else
                    {
                        requestContent.Headers.ContentLength = null;
                    }

                    // Compute MD5 of request body data. This will be verified by the server when it
                    // receives the request.
                    if (PlatformDetection.IsBrowser)
                    {
                        // [ActiveIssue("https://github.com/dotnet/runtime/issues/37669", TestPlatforms.Browser)]
                        requestContent.Headers.Add("Content-MD5-Skip", "browser");
                    }
                    else
                    {
                        requestContent.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(requestBody);
                    }
                }

                if (useChunkedEncodingUpload)
                {
                    client.DefaultRequestHeaders.TransferEncodingChunked = true;
                }

                using (HttpResponseMessage response = await client.PostAsync(remoteServer.VerifyUploadUri, requestContent))
                {
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                }
            }
        }
Beispiel #7
0
        public void SendAsync_HttpTracingEnabled_Succeeds(bool useSsl)
        {
            RemoteInvoke(async(useSocketsHttpHandlerString, useSslString) =>
            {
                using (var listener = new TestEventListener("Microsoft-System-Net-Http", EventLevel.Verbose))
                {
                    var events = new ConcurrentQueue <EventWrittenEventArgs>();
                    await listener.RunWithCallbackAsync(events.Enqueue, async() =>
                    {
                        // Exercise various code paths to get coverage of tracing
                        using (HttpClient client = CreateHttpClient(useSocketsHttpHandlerString))
                        {
                            // Do a get to a loopback server
                            await LoopbackServer.CreateServerAsync(async(server, url) =>
                            {
                                await TestHelper.WhenAllCompletedOrAnyFailed(
                                    server.AcceptConnectionSendResponseAndCloseAsync(),
                                    client.GetAsync(url));
                            });

                            // Do a post to a remote server
                            byte[] expectedData        = Enumerable.Range(0, 20000).Select(i => unchecked ((byte)i)).ToArray();
                            Uri remoteServer           = bool.Parse(useSslString) ? Configuration.Http.SecureRemoteEchoServer : Configuration.Http.RemoteEchoServer;
                            var content                = new ByteArrayContent(expectedData);
                            content.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(expectedData);
                            using (HttpResponseMessage response = await client.PostAsync(remoteServer, content))
                            {
                                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                            }
                        }
                    });

                    // We don't validate receiving specific events, but rather that we do at least
                    // receive some events, and that enabling tracing doesn't cause other failures
                    // in processing.
                    Assert.DoesNotContain(events, ev => ev.EventId == 0); // make sure there are no event source error messages
                    Assert.InRange(events.Count, 1, int.MaxValue);
                }

                return(SuccessExitCode);
            }, UseSocketsHttpHandler.ToString(), useSsl.ToString()).Dispose();
        }
Beispiel #8
0
        public async Task PostAsync_CallMethod_StreamContent(Configuration.Http.RemoteServer remoteServer, HttpContent content, byte[] expectedData)
        {
            using (HttpClient client = CreateHttpClientForRemoteServer(remoteServer))
            {
                if (PlatformDetection.IsBrowser)
                {
                    // [ActiveIssue("https://github.com/dotnet/runtime/issues/37669", TestPlatforms.Browser)]
                    content.Headers.Add("Content-MD5-Skip", "browser");
                }
                else
                {
                    content.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(expectedData);
                }

                using (HttpResponseMessage response = await client.PostAsync(remoteServer.VerifyUploadUri, content))
                {
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                }
            }
        }
Beispiel #9
0
        public async Task PostAsync_CallMethodTwice_StringContent(Configuration.Http.RemoteServer remoteServer)
        {
            using (HttpClient client = CreateHttpClientForRemoteServer(remoteServer))
            {
                string data    = "Test String";
                var    content = new StringContent(data, Encoding.UTF8);

                if (PlatformDetection.IsBrowser)
                {
                    // [ActiveIssue("https://github.com/dotnet/runtime/issues/37669", TestPlatforms.Browser)]
                    content.Headers.Add("Content-MD5-Skip", "browser");
                }
                else
                {
                    content.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(data);
                }

                HttpResponseMessage response;
                using (response = await client.PostAsync(remoteServer.VerifyUploadUri, content))
                {
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                }

                // Repeat call.
                content = new StringContent(data, Encoding.UTF8);
                if (PlatformDetection.IsBrowser)
                {
                    // [ActiveIssue("https://github.com/dotnet/runtime/issues/37669", TestPlatforms.Browser)]
                    content.Headers.Add("Content-MD5-Skip", "browser");
                }
                else
                {
                    content.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(data);
                }

                using (response = await client.PostAsync(remoteServer.VerifyUploadUri, content))
                {
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                }
            }
        }
Beispiel #10
0
        public async Task PostAsync_CallMethodTwice_StringContent(Configuration.Http.RemoteServer remoteServer)
        {
            using (HttpClient client = CreateHttpClientForRemoteServer(remoteServer))
            {
                string data    = "Test String";
                var    content = new StringContent(data, Encoding.UTF8);
                content.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(data);
                HttpResponseMessage response;
                using (response = await client.PostAsync(remoteServer.VerifyUploadUri, content))
                {
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                }

                // Repeat call.
                content = new StringContent(data, Encoding.UTF8);
                content.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(data);
                using (response = await client.PostAsync(remoteServer.VerifyUploadUri, content))
                {
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                }
            }
        }
Beispiel #11
0
        public async Task PostAsync_CallMethodTwice_StringContent(Uri remoteServer)
        {
            using (var client = new HttpClient())
            {
                string data    = "Test String";
                var    content = new StringContent(data, Encoding.UTF8);
                content.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(data);
                HttpResponseMessage response;
                using (response = await client.PostAsync(remoteServer, content))
                {
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                }

                // Repeat call.
                content = new StringContent(data, Encoding.UTF8);
                content.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(data);
                using (response = await client.PostAsync(remoteServer, content))
                {
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                }
            }
        }
Beispiel #12
0
        public async Task PostAsync_CallMethod_UnicodeStringContent(Configuration.Http.RemoteServer remoteServer)
        {
            using (HttpClient client = CreateHttpClientForRemoteServer(remoteServer))
            {
                string data    = "\ub4f1\uffc7\u4e82\u67ab4\uc6d4\ud1a0\uc694\uc77c\uffda3\u3155\uc218\uffdb";
                var    content = new StringContent(data, Encoding.UTF8);
                if (PlatformDetection.IsBrowser)
                {
                    // [ActiveIssue("https://github.com/dotnet/runtime/issues/37669", TestPlatforms.Browser)]
                    content.Headers.Add("Content-MD5-Skip", "browser");
                }
                else
                {
                    content.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(data);
                }

                using (HttpResponseMessage response = await client.PostAsync(remoteServer.VerifyUploadUri, content))
                {
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                }
            }
        }
Beispiel #13
0
        private async Task PostHelper(
            Configuration.Http.RemoteServer remoteServer,
            string requestBody,
            HttpContent requestContent,
            bool useContentLengthUpload,
            bool useChunkedEncodingUpload)
        {
            using (HttpClient client = CreateHttpClientForRemoteServer(remoteServer))
            {
                if (requestContent != null)
                {
                    if (useContentLengthUpload)
                    {
                        // Ensure that Content-Length is populated (see issue #27245)
                        requestContent.Headers.ContentLength = requestContent.Headers.ContentLength;
                    }
                    else
                    {
                        requestContent.Headers.ContentLength = null;
                    }

                    // Compute MD5 of request body data. This will be verified by the server when it
                    // receives the request.
                    requestContent.Headers.ContentMD5 = TestHelper.ComputeMD5Hash(requestBody);
                }

                if (useChunkedEncodingUpload)
                {
                    client.DefaultRequestHeaders.TransferEncodingChunked = true;
                }

                using (HttpResponseMessage response = await client.PostAsync(remoteServer.VerifyUploadUri, requestContent))
                {
                    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                }
            }
        }