public void Should_decorate_actions([Values(true, false)] bool clearOtherWriters)
        {
            var requestGraph     = RequestGraph.CreateFor <Handler>(x => x.HasResponse());
            var actionDescriptor = requestGraph.GetActionDescriptor();
            var context          = new ActionDecoratorContext(actionDescriptor);

            _viewConfiguration.ClearOtherWriters = clearOtherWriters;

            _decorator.Decorate(context);

            var views = actionDescriptor.Registry.Where(x => x.Instance is ViewContent).ToList();

            views.Count.ShouldEqual(1);

            var viewContent = views.First().Instance as ViewContent;

            viewContent.AcceptTypes.ShouldOnlyContain(MimeTypes.TextHtml);
            viewContent.Encoding.ShouldEqual(Encoding.UTF8);
            viewContent.Render(new Model {
                Value = "fark"
            }).ShouldEqual("fark");

            actionDescriptor.Registry.Any(x => x.ConcreteType == typeof(ViewWriter)).ShouldBeTrue();
            actionDescriptor.ResponseWriters.Any(x => x.Type == typeof(ViewWriter)).ShouldBeTrue();

            if (clearOtherWriters)
            {
                actionDescriptor.ResponseWriters.Count().ShouldEqual(1);
            }
            else
            {
                actionDescriptor.ResponseWriters.Count().ShouldBeGreaterThan(1);
            }
        }
        public void Should_apply_to_endpoints_with_a_reponse()
        {
            var requestGraph = RequestGraph.CreateFor <Handler>(x => x.HasResponse());
            var context      = new ActionDecoratorContext(requestGraph.GetActionDescriptor());

            _decorator.AppliesTo(context).ShouldBeTrue();
        }
        public void Should_not_decorate_actions_without_a_response()
        {
            var requestGraph     = RequestGraph.CreateFor <Handler>(x => x.NoResponse());
            var actionDescriptor = requestGraph.GetActionDescriptor();
            var context          = new ActionDecoratorContext(actionDescriptor);

            _viewConfiguration.ClearOtherWriters = true;

            _decorator.Decorate(context);

            Should_not_decorate(actionDescriptor);
        }
        public void Should_not_include_view_sources_that_dont_apply()
        {
            var requestGraph     = RequestGraph.CreateFor <Handler>(x => x.HasResponse());
            var actionDescriptor = requestGraph.GetActionDescriptor();
            var context          = new ActionDecoratorContext(actionDescriptor);

            _viewConfiguration.ViewSources.Configure(x => x
                                                     .Clear().Append <EmbeddedViewSource>(y => false));

            _decorator.Decorate(context);

            Should_not_decorate(actionDescriptor);
        }
Beispiel #5
0
        public virtual void Decorate(ActionDecoratorContext context)
        {
            var engines = _viewEngines
                          .ThatApplyTo(context.ActionDescriptor, _configuration,
                                       _viewConfiguration, _httpConfiguration)
                          .SelectMany(x => x
                                      .SupportedViewTypes.Select(t => new
            {
                Engine = x,
                Type   = t
            })).ToList();

            var supportedTypes = engines.SelectMany(x =>
                                                    x.Engine.SupportedViewTypes).ToArray();
            var viewSourceContext = new ViewSourceContext(
                context.ActionDescriptor, supportedTypes);

            var result = _viewSources
                         .ThatApplyTo(supportedTypes, context.ActionDescriptor,
                                      _configuration, _viewConfiguration, _httpConfiguration)
                         .SelectMany(x => x.GetViews(viewSourceContext))
                         .Join(engines, x => x.Type, x => x.Type,
                               (v, e) => new
            {
                View = v,
                e.Engine
            })
                         .FirstOrDefault();

            if (result == null)
            {
                return;
            }

            result.Engine.PreCompile(result.View);

            context
            .ConfigureRegistry(x => x
                               .Register(new ViewContent(result.View, result.Engine))
                               .Register <IResponseWriter, ViewWriter>())
            .ConfigureResponseWriters(x =>
            {
                if (_viewConfiguration.ClearOtherWriters)
                {
                    x.Clear();
                }
                x.Append <ViewWriter>();
            });
        }
Beispiel #6
0
 public bool AppliesTo(ActionDecoratorContext context)
 {
     return(context.ActionDescriptor.Route.Method == "GET");
 }
Beispiel #7
0
 public void Decorate(ActionDecoratorContext context)
 {
     context.ActionDescriptor.Registry.Register <IDependency, Dependency>();
 }
Beispiel #8
0
 public void Decorate(ActionDecoratorContext context)
 {
     DecorateCalled  = true;
     DecorateContext = context;
     DecorateFunc?.Invoke(context);
 }
Beispiel #9
0
 public bool AppliesTo(ActionDecoratorContext context)
 {
     AppliesToCalled  = true;
     AppliesToContext = context;
     return(AppliesToFunc?.Invoke(context) ?? true);
 }
Beispiel #10
0
 public virtual bool AppliesTo(ActionDecoratorContext context)
 {
     return(context.ActionDescriptor.Route.HasResponse);
 }