Inheritance: ICommunicationContext
Beispiel #1
0
        public IResponse ProcessRequest(IRequest request)
        {
            CheckNotDisposed();
            var ambientContext = new AmbientContext();
            var context        = new InMemoryCommunicationContext
            {
                ApplicationBaseUri = new Uri("http://localhost"),
                Request            = request,
                Response           = new InMemoryResponse()
            };

            try
            {
                using (new ContextScope(ambientContext))
                {
                    RaiseIncomingRequestReceived(context);
                }
            }
            finally
            {
                using (new ContextScope(ambientContext))
                {
                    RaiseIncomingRequestProcessed(context);
                }
            }
            return(context.Response);
        }
Beispiel #2
0
 public IResponse ProcessRequest(IRequest request)
 {
     CheckNotDisposed();
     var ambientContext = new AmbientContext();
     var context = new InMemoryCommunicationContext
     {
         ApplicationBaseUri = new Uri("http://localhost"), 
         Request = request, 
         Response = new InMemoryResponse()
     };
     try
     {
         using (new ContextScope(ambientContext))
         {
             RaiseIncomingRequestReceived(context);
         }
     }
     finally
     {
         using (new ContextScope(ambientContext))
         {
             RaiseIncomingRequestProcessed(context);
         }
     }
     return context.Response;
 }
        public void the_filter_doesnt_authorize_the_execution()
        {
            var context = new InMemoryCommunicationContext();
            var principal = new PrincipalAuthorizationAttribute { InRoles = new[] { "Administrators"}};

            principal.ExecuteBefore(context)
                .ShouldBe(PipelineContinuation.RenderNow);
        }
        public void the_filter_doesnt_authorize_the_execution()
        {
            var context = new InMemoryCommunicationContext();
            var principal = new PrincipalAuthorizationInterceptor(context) { InRoles = new[] { "Administrators"}};

            principal.BeforeExecute(new Mock<IOperation>().Object)
                .ShouldBe(true);
        }
        public void execution_is_denied()
        {
            var context = new InMemoryCommunicationContext();

            var authenticationInterceptor = new RequiresAuthenticationInterceptor(context);
            authenticationInterceptor.BeforeExecute(new Mock<IOperation>().Object)
                .ShouldBeFalse();
            context.OperationResult.ShouldBeOfType<OperationResult.Unauthorized>();
        }
        public void execution_is_allowed()
        {
            var context = new InMemoryCommunicationContext();
            context.User = new GenericPrincipal(new GenericIdentity("name"), null);

            var authenticationInterceptor = new RequiresAuthenticationInterceptor(context);

            authenticationInterceptor.BeforeExecute(new Mock<IOperation>().Object)
                .ShouldBeTrue();
        }
 public void execution_is_denied()
 {
     var context = new InMemoryCommunicationContext();
     const string REALM = "Test Realm";
     var authenticationInterceptor = new RequiresBasicAuthenticationInterceptor(context, REALM);
     authenticationInterceptor.BeforeExecute(new Mock<IOperation>().Object).ShouldBeFalse();
     context.OperationResult.ShouldBeOfType<OperationResult.Unauthorized>();
     var expectedHeader = String.Format("Basic realm=\"{0}\"", REALM);
     context.Response.Headers["WWW-Authenticate"].ShouldBe(expectedHeader);
 }
        public void the_username_is_matched_and_execution_continues()
        {
            Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("johndoe"), new[] { "Administrator" });

            var rastaContext = new InMemoryCommunicationContext();
            var authorizer = new PrincipalAuthorizationAttribute() { Users = new[] { "johndoe" } };
            authorizer.ExecuteBefore(rastaContext)
                .ShouldBe(PipelineContinuation.Continue);

            rastaContext.OperationResult.ShouldBeNull();
        }
        public void the_username_is_matched_and_execution_continues()
        {
            Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("johndoe"), new[] { "Administrator" });

            var rastaContext = new InMemoryCommunicationContext();
            var authorizer = new PrincipalAuthorizationInterceptor(rastaContext) { Users = new[] { "johndoe" } };
            authorizer.BeforeExecute(new Mock<IOperation>().Object)
              .ShouldBe(true);

            rastaContext.OperationResult.ShouldBeNull();
        }
        public void execution_is_not_allowed()
        {
            var context = new InMemoryCommunicationContext();
            context.User = new GenericPrincipal(new GenericIdentity("name"), new []{"Administrator"});

            var authenticationInterceptor = new RequiresRoleInterceptor(context) { Role = "SuperUser" };

            authenticationInterceptor.BeforeExecute(new Mock<IOperation>().Object)
                .ShouldBeFalse();
            context.OperationResult.ShouldBeOfType<OperationResult.Unauthorized>();

        }
        // TODO: Need to be moved to the core framework.
        public static void RenderResource(this IXhtmlAnchor anchor, Uri resource)
        {
            var context = new InMemoryCommunicationContext
                {
                    Request = new InMemoryRequest
                        {
                            HttpMethod = "GET",
                            Uri = resource,
                            Entity = new HttpEntity
                                {
                                    ContentLength = 0
                                }
                        }
                };
            context.Request.Headers["Accept"] = MediaType.XhtmlFragment.ToString();
            var textWriterProvider = anchor.AmbientWriter as ISupportsTextWriter;

            StringBuilder inMemoryRendering = null;

            if (textWriterProvider != null && textWriterProvider.TextWriter != null)
                context.Response = new InMemoryResponse
                    {
                        Entity = new TextWriterEnabledEntity(textWriterProvider.TextWriter)
                    };
            else
            {
                inMemoryRendering = new StringBuilder();
                var writer = new StringWriter(inMemoryRendering);
                context.Response = new InMemoryResponse { Entity = new TextWriterEnabledEntity(writer) };
            }

            anchor.Resolver.Resolve<IPipeline>().Run(context);

            if (context.Response.Entity.Stream.Length > 0)
            {
                context.Response.Entity.Stream.Position = 0;
                var
                    destinationEncoding =
                        Encoding.GetEncoding(context.Response.Entity.ContentType.CharSet ?? "UTF8");

                var reader = new StreamReader(context.Response.Entity.Stream, destinationEncoding);
                anchor.AmbientWriter.WriteUnencodedString(reader.ReadToEnd());
            }
            else if (inMemoryRendering != null && inMemoryRendering.Length > 0)
            {
                anchor.AmbientWriter.WriteUnencodedString(inMemoryRendering.ToString());
            }
        }
        public void Should_not_throw_exception_if_multiple_threads_inject_conext_into_singleton_dependency_resolver()
        {
            var container = new Container();
                container.Configure(c => c.Scan(s => {
                    s.AssemblyContainingType(GetType());
                    s.LookForRegistries();
                    s.WithDefaultConventions();
                }));

                _resolver = new StructureMapDependencyResolver(container);

                _resolver.AddDependency(typeof(IDependencyResolver),
                                        typeof(StructureMapDependencyResolver),
                                        DependencyLifetime.Singleton);
                _resolver.AddDependency(typeof(IPipeline), typeof(PipelineRunner),
                                        DependencyLifetime.Singleton);
                _resolver.AddDependency(typeof(ILogger), typeof(NullLogger),
                                        DependencyLifetime.Singleton);

                for (int i = 0; i < 200; i++) {
                    int errorCount = 0;
                    new Thread(() => {

                        try {
                            var context = new InMemoryCommunicationContext();
                            _resolver.AddDependencyInstance<ICommunicationContext>(context,DependencyLifetime.PerRequest);
                            _resolver.AddDependencyInstance<IRequest>(context.Request,DependencyLifetime.PerRequest);
                            _resolver.AddDependencyInstance<IResponse>(context.Response,DependencyLifetime.PerRequest);
                            _resolver.AddDependencyInstance<IHost>(new InMemoryHost(new ConfigurationSource()), DependencyLifetime.PerRequest);

                            _resolver.Resolve<ICommunicationContext>();

                        } catch (Exception) {
                            errorCount = errorCount +1 ;
                            throw;
                        }

                    }).Start();
                    Assert.That(errorCount, Is.EqualTo(0), "Some of the requests threw errors meaning that the dependencyresolver is not threadsafe ");
                }
        }