protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
 {
     HttpRequestContext context = request.GetRequestContext();
     context.Principal = Principal;
     request.SetOwinContext(this.OwinContext);
     return base.SendAsync(request, cancellationToken);
 }
        public void GetOwinContext_ReturnsSetContextInstance()
        {
            // Arrange
            var request = new HttpRequestMessage();
            var context = new OwinContext();
            request.SetOwinContext(context);

            // Act & Asert
            Assert.Same(context, request.GetOwinContext());
        }
        public void SetOwinContext_RemovesOwinEnvironmentProperty_WhenPresent()
        {
            // Arrange
            using (HttpRequestMessage request = new HttpRequestMessage())
            {
                var environment = new Dictionary<string, object>();
                request.Properties.Add("MS_OwinEnvironment", environment);
                var context = new OwinContext();

                // Act
                request.SetOwinContext(context);

                // Assert
                Assert.False(request.Properties.ContainsKey("MS_OwinEnvironment"));
            }
        }
        private static void MapRequestProperties(HttpRequestMessage request, IOwinContext context)
        {
            // Set the OWIN context on the request
            request.SetOwinContext(context);

            // Set the a request context on the request that lazily populates each property.
            HttpRequestContext requestContext = new OwinHttpRequestContext(context, request);
            request.SetRequestContext(requestContext);
        }
        private HttpRequestMessage CreateRequestMessage(ClaimsPrincipal user, bool isCompressed, bool isJson, string charset = "utf-8") {
            var request = new HttpRequestMessage();

            var context = new OwinContext();
            context.Request.User = Thread.CurrentPrincipal = user;
            request.SetOwinContext(context);
            request.SetConfiguration(new HttpConfiguration());
            request.Content = new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, "/api/v2/events"));
            if (isCompressed)
                request.Content.Headers.ContentEncoding.Add("gzip");
            request.Content.Headers.ContentType.MediaType = isJson ? "application/json" : "text/plain";
            request.Content.Headers.ContentType.CharSet = charset;

            return request;
        }
 private static HttpRequestMessage CreateRequestWithOwinContextAndRequestContext(IOwinContext context)
 {
     HttpRequestMessage request = new HttpRequestMessage();
     request.SetOwinContext(context);
     request.SetRequestContext(new HttpRequestContext());
     return request;
 }
        public void SendAsync_SetsRequestContextPrincipalToAnonymous_BeforeCallingInnerHandler()
        {
            // Arrange
            IPrincipal requestContextPrincipal = null;
            Mock<HttpRequestContext> requestContextMock = new Mock<HttpRequestContext>(MockBehavior.Strict);
            requestContextMock
                .SetupSet(c => c.Principal = It.IsAny<IPrincipal>())
                .Callback<IPrincipal>((value) => requestContextPrincipal = value);
            IPrincipal principalBeforeInnerHandler = null;
            HttpMessageHandler inner = new LambdaHttpMessageHandler((ignore1, ignore2) =>
            {
                principalBeforeInnerHandler = requestContextPrincipal;
                return Task.FromResult<HttpResponseMessage>(null);
            });
            HttpMessageHandler handler = CreateProductUnderTest(inner);
            IOwinContext context = CreateOwinContext();

            using (HttpRequestMessage request = new HttpRequestMessage())
            {
                request.SetOwinContext(context);
                request.SetRequestContext(requestContextMock.Object);

                // Act
                HttpResponseMessage ignore = handler.SendAsync(request, CancellationToken.None).Result;
            }

            // Assert
            Assert.NotNull(principalBeforeInnerHandler);
            IIdentity identity = principalBeforeInnerHandler.Identity;
            Assert.NotNull(identity);
            Assert.False(identity.IsAuthenticated);
            Assert.Null(identity.Name);
            Assert.Null(identity.AuthenticationType);
        }
        private static void MapRequestProperties(HttpRequestMessage request, IOwinContext context)
        {
            // Set the OWIN context on the request
            request.SetOwinContext(context);

            // Set the virtual path root for link resolution and link generation to work
            // OWIN spec requires request path base to be either the empty string or start with "/"
            string requestPathBase = context.Request.PathBase;
            request.SetVirtualPathRoot(String.IsNullOrEmpty(requestPathBase) ? "/" : requestPathBase);

            // Set a delegate to get the client certificate
            request.Properties[HttpPropertyKeys.RetrieveClientCertificateDelegateKey] = new Func<HttpRequestMessage, X509Certificate2>(
                req => context.Get<X509Certificate2>(OwinConstants.ClientCertifiateKey));

            // Set a lazily-evaluated way of determining whether the request is local or not
            Lazy<bool> isLocal = new Lazy<bool>(() => context.Get<bool>(OwinConstants.IsLocalKey), isThreadSafe: false);
            request.Properties[HttpPropertyKeys.IsLocalKey] = isLocal;
        }
 private static HttpRequestMessage CreateRequest(IOwinContext owinContext)
 {
     HttpRequestMessage request = new HttpRequestMessage();
     request.SetOwinContext(owinContext);
     return request;
 }
 protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
 {
     request.SetOwinContext(new OwinContext());
     return base.SendAsync(request, cancellationToken);
 }
        public void GetOwinEnvironment_ReturnsSetContextEnvironmentInstance()
        {
            // Arrange
            var request = new HttpRequestMessage();
            var environment = new Dictionary<string, object>();
            var context = new OwinContext(environment);
            request.SetOwinContext(context);

            // Act & Assert
            Assert.Same(environment, request.GetOwinEnvironment());
        }