Пример #1
0
        public async Task Request_FieldsCanBeSet_Set()
        {
            string root;

            using (Utilities.CreateHttpServerReturnRoot("/basepath", out root, httpContext =>
            {
                try
                {
                    var requestInfo = httpContext.Features.Get <IHttpRequestFeature>();

                    // Request Keys
                    requestInfo.Method = "TEST";
                    Assert.Equal("TEST", requestInfo.Method);
                    requestInfo.Body = new MemoryStream();
                    Assert.IsType <MemoryStream>(requestInfo.Body);
                    var customHeaders = new HeaderCollection();
                    requestInfo.Headers = customHeaders;
                    Assert.Same(customHeaders, requestInfo.Headers);
                    requestInfo.Scheme = "abcd";
                    Assert.Equal("abcd", requestInfo.Scheme);
                    requestInfo.PathBase = "/customized/Base";
                    Assert.Equal("/customized/Base", requestInfo.PathBase);
                    requestInfo.Path = "/customized/Path";
                    Assert.Equal("/customized/Path", requestInfo.Path);
                    requestInfo.QueryString = "?customizedQuery";
                    Assert.Equal("?customizedQuery", requestInfo.QueryString);
                    requestInfo.RawTarget = "/customized/raw?Target";
                    Assert.Equal("/customized/raw?Target", requestInfo.RawTarget);
                    requestInfo.Protocol = "Custom/2.0";
                    Assert.Equal("Custom/2.0", requestInfo.Protocol);

                    var connectionInfo = httpContext.Features.Get <IHttpConnectionFeature>();
                    connectionInfo.RemoteIpAddress = IPAddress.Broadcast;
                    Assert.Equal(IPAddress.Broadcast, connectionInfo.RemoteIpAddress);
                    connectionInfo.RemotePort = 12345;
                    Assert.Equal(12345, connectionInfo.RemotePort);
                    connectionInfo.LocalIpAddress = IPAddress.Any;
                    Assert.Equal(IPAddress.Any, connectionInfo.LocalIpAddress);
                    connectionInfo.LocalPort = 54321;
                    Assert.Equal(54321, connectionInfo.LocalPort);
                    connectionInfo.ConnectionId = "CustomId";
                    Assert.Equal("CustomId", connectionInfo.ConnectionId);

                    // Trace identifier
                    var requestIdentifierFeature = httpContext.Features.Get <IHttpRequestIdentifierFeature>();
                    Assert.NotNull(requestIdentifierFeature);
                    requestIdentifierFeature.TraceIdentifier = "customTrace";
                    Assert.Equal("customTrace", requestIdentifierFeature.TraceIdentifier);

                    // Note: Response keys are validated in the ResponseTests
                }
                catch (Exception ex)
                {
                    byte[] body = Encoding.ASCII.GetBytes(ex.ToString());
                    httpContext.Response.Body.Write(body, 0, body.Length);
                }
                return(Task.FromResult(0));
            }))
            {
                string response = await SendRequestAsync(root + "/basepath/SomePath?SomeQuery");

                Assert.Equal(string.Empty, response);
            }
        }