private HttpRequestMessage CreateHttpRequestMessage(RequestContext requestContext)
        {
            // Get the HTTP request from the WCF Message
            HttpRequestMessage request = requestContext.RequestMessage.ToHttpRequestMessage();

            if (request == null)
            {
                throw Error.InvalidOperation(SRResources.HttpMessageHandlerInvalidMessage, requestContext.RequestMessage.GetType());
            }

            // create principal information and add it the request for the windows auth case
            SetCurrentPrincipal(request);

            HttpRequestContext httpRequestContext = new SelfHostHttpRequestContext(requestContext, _configuration,
                                                                                   request);

            request.SetRequestContext(httpRequestContext);

            // The following two properties are set for backwards compatibility only. The request context controls the
            // behavior for all cases except when accessing the property directly by key.

            // Add the retrieve client certificate delegate to the property bag to enable lookup later on
            request.Properties.Add(HttpPropertyKeys.RetrieveClientCertificateDelegateKey, _retrieveClientCertificate);

            // Add information about whether the request is local or not
            request.Properties.Add(HttpPropertyKeys.IsLocalKey, new Lazy <bool>(() => IsLocal(requestContext.RequestMessage)));
            return(request);
        }
Exemplo n.º 2
0
        public async Task SendAsync_ServiceModel_AddsSelfHostHttpRequestContext()
        {
            // Arrange
            using (PortReserver port = new PortReserver())
            {
                string baseUri = port.BaseUri;

                HttpRequestContext context = null;
                Uri via = null;

                Func <HttpRequestMessage, CancellationToken, Task <HttpResponseMessage> > sendAsync = (r, c) =>
                {
                    if (r != null)
                    {
                        context = r.GetRequestContext();
                    }

                    SelfHostHttpRequestContext typedContext = context as SelfHostHttpRequestContext;

                    if (typedContext != null)
                    {
                        via = typedContext.RequestContext.RequestMessage.Properties.Via;
                    }

                    return(Task.FromResult(new HttpResponseMessage()));
                };

                using (HttpSelfHostConfiguration expectedConfiguration = new HttpSelfHostConfiguration(baseUri))
                {
                    expectedConfiguration.HostNameComparisonMode = HostNameComparisonMode.Exact;

                    using (HttpMessageHandler dispatcher = new LambdaHttpMessageHandler(sendAsync))
                        using (HttpSelfHostServer server = new HttpSelfHostServer(expectedConfiguration, dispatcher))
                            using (HttpClient client = new HttpClient())
                                using (HttpRequestMessage expectedRequest = new HttpRequestMessage(HttpMethod.Get, baseUri))
                                {
                                    await server.OpenAsync();

                                    // Act
                                    using (HttpResponseMessage ignore = await client.SendAsync(expectedRequest))
                                    {
                                        // Assert
                                        SelfHostHttpRequestContext typedContext = (SelfHostHttpRequestContext)context;
                                        Assert.Equal(expectedRequest.RequestUri, via);
                                        Assert.Same(expectedConfiguration, context.Configuration);
                                        Assert.Equal(expectedRequest.RequestUri, typedContext.Request.RequestUri);

                                        await server.CloseAsync();
                                    }
                                }
                }
            }
        }
Exemplo n.º 3
0
        public void SendAsync_ServiceModel_AddsSelfHostHttpRequestContext()
        {
            // Arrange
            // Use a different port from 50231 to prevent conflicts with concurrent tests.
            string baseUri = "http://localhost:50232/";

            HttpRequestContext context = null;
            Uri via = null;

            Func <HttpRequestMessage, CancellationToken, Task <HttpResponseMessage> > sendAsync = (r, c) =>
            {
                if (r != null)
                {
                    context = r.GetRequestContext();
                }

                SelfHostHttpRequestContext typedContext = context as SelfHostHttpRequestContext;

                if (typedContext != null)
                {
                    via = typedContext.RequestContext.RequestMessage.Properties.Via;
                }

                return(Task.FromResult(new HttpResponseMessage()));
            };

            using (HttpSelfHostConfiguration expectedConfiguration = new HttpSelfHostConfiguration(baseUri))
            {
                expectedConfiguration.HostNameComparisonMode = HostNameComparisonMode.Exact;

                using (HttpMessageHandler dispatcher = new LambdaHttpMessageHandler(sendAsync))
                    using (HttpSelfHostServer server = new HttpSelfHostServer(expectedConfiguration, dispatcher))
                        using (HttpClient client = new HttpClient())
                            using (HttpRequestMessage expectedRequest = new HttpRequestMessage(HttpMethod.Get, baseUri))
                            {
                                server.OpenAsync().Wait();

                                // Act
                                using (HttpResponseMessage ignore = client.SendAsync(expectedRequest).Result)
                                {
                                    // Assert
                                    SelfHostHttpRequestContext typedContext = (SelfHostHttpRequestContext)context;
                                    Assert.Equal(expectedRequest.RequestUri, via);
                                    Assert.Same(expectedConfiguration, context.Configuration);
                                    Assert.Equal(expectedRequest.RequestUri, typedContext.Request.RequestUri);

                                    server.CloseAsync().Wait();
                                }
                            }
            }
        }
Exemplo n.º 4
0
        public void RequestGet_ReturnsProvidedInstance()
        {
            // Arrange
            using (RequestContext serviceModelContext = CreateStubServiceModelContext())
                using (HttpConfiguration configuration = CreateConfiguration())
                    using (HttpRequestMessage expectedRequest = CreateRequest())
                    {
                        SelfHostHttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration,
                                                                                    expectedRequest);

                        // Act
                        HttpRequestMessage request = context.Request;

                        // Assert
                        Assert.Same(expectedRequest, request);
                    }
        }
        private HttpRequestMessage CreateHttpRequestMessage(RequestContext requestContext)
        {
            // Get the HTTP request from the WCF Message
            HttpRequestMessage request = requestContext.RequestMessage.ToHttpRequestMessage();
            if (request == null)
            {
                throw Error.InvalidOperation(SRResources.HttpMessageHandlerInvalidMessage, requestContext.RequestMessage.GetType());
            }

            // create principal information and add it the request for the windows auth case
            SetCurrentPrincipal(request);

            HttpRequestContext httpRequestContext = new SelfHostHttpRequestContext(requestContext, _configuration,
                request);
            request.SetRequestContext(httpRequestContext);

            // The following two properties are set for backwards compatibility only. The request context controls the
            // behavior for all cases except when accessing the property directly by key.

            // Add the retrieve client certificate delegate to the property bag to enable lookup later on
            request.Properties.Add(HttpPropertyKeys.RetrieveClientCertificateDelegateKey, _retrieveClientCertificate);

            // Add information about whether the request is local or not
            request.Properties.Add(HttpPropertyKeys.IsLocalKey, new Lazy<bool>(() => IsLocal(requestContext.RequestMessage)));
            return request;
        }