public async Task IHalFormatterNotSelected_DoesSerializeTest()
        {
            var outputFormatter   = Mock.Of <IOutputFormatter>();
            var formatterSelector = FormatterSelector;

            formatterSelector.Setup(f => f.SelectFormatter(
                                        It.IsAny <OutputFormatterCanWriteContext>(),
                                        It.IsAny <IList <IOutputFormatter> >(),
                                        It.IsAny <MediaTypeCollection>()))
            .Returns(outputFormatter);

            var inner = InnerExecutor;

            inner.Setup(i => i.ExecuteAsync(It.IsAny <ActionContext>(), It.IsAny <ObjectResult>()))
            .Returns(Task.CompletedTask)
            .Verifiable();

            var executor = new HalObjectResultExecutor(
                inner.Object,
                WriterFactory.Object,
                formatterSelector.Object,
                Logger);

            var httpContext = HttpContext;

            httpContext.Features.Set(new HalFeature());
            var actionContext = new ActionContext
            {
                HttpContext = httpContext
            };

            await executor.ExecuteAsync(actionContext, ObjectResult);

            inner.Verify();
        }
        public async Task IHalFormatterSelected_DoesNotSerializeTest()
        {
            var halFormatter      = new Mock <IHalFormatter>();
            var outputFormatter   = halFormatter.As <IOutputFormatter>();
            var formatterSelector = FormatterSelector;

            formatterSelector.Setup(f => f.SelectFormatter(
                                        It.IsAny <OutputFormatterCanWriteContext>(),
                                        It.IsAny <IList <IOutputFormatter> >(),
                                        It.IsAny <MediaTypeCollection>()))
            .Returns(outputFormatter.Object);

            var inner    = InnerExecutor.Object;
            var executor = new HalObjectResultExecutor(
                inner,
                WriterFactory.Object,
                formatterSelector.Object,
                Logger);

            var halFeature  = new HalFeature();
            var httpContext = HttpContext;

            httpContext.Features.Set(halFeature);
            var actionContext = new ActionContext
            {
                HttpContext = httpContext
            };

            var result = ObjectResult;
            await executor.ExecuteAsync(actionContext, result);

            Assert.AreSame(actionContext, halFeature.FormattingContext.Context);
            Assert.AreSame(result, halFeature.FormattingContext.Result);
            Assert.AreSame(inner, halFeature.FormattingContext.Executor);
        }
        public async Task SelectsFormatterBasedOnObjectResultTest()
        {
            var inner = InnerExecutor;

            inner.Setup(i => i.ExecuteAsync(It.IsAny <ActionContext>(), It.IsAny <ObjectResult>()))
            .Returns(Task.CompletedTask);

            var formatters = new FormatterCollection <IOutputFormatter>();
            var mediaTypes = new MediaTypeCollection();
            var result     = ObjectResult;

            result.ContentTypes = mediaTypes;
            result.Formatters   = formatters;

            var httpContext = HttpContext;

            httpContext.Features.Set <HalFeature>(new HalFeature());

            var formatterSelector = FormatterSelector;

            formatterSelector.Setup(f => f.SelectFormatter(
                                        It.IsAny <OutputFormatterCanWriteContext>(),
                                        It.IsAny <IList <IOutputFormatter> >(),
                                        It.IsAny <MediaTypeCollection>()))
            .Callback <
                OutputFormatterCanWriteContext,
                IList <IOutputFormatter>,
                MediaTypeCollection>((c, f, m) =>
            {
                Assert.AreSame(formatters, f);
                Assert.AreSame(m, mediaTypes);
            })
            .Returns(default(IOutputFormatter))
            .Verifiable();

            var executor = new HalObjectResultExecutor(
                inner.Object,
                WriterFactory.Object,
                formatterSelector.Object,
                Logger);

            var actionContext = new ActionContext
            {
                HttpContext = httpContext
            };

            await executor.ExecuteAsync(actionContext, result);

            formatterSelector.Verify();
        }
        public async Task HalHttpContext_SetsResourceTest()
        {
            var executor = new HalObjectResultExecutor(
                InnerExecutor.Object,
                WriterFactory.Object,
                FormatterSelector.Object,
                Logger);

            var httpContext   = HalContext;
            var actionContext = new ActionContext
            {
                HttpContext = httpContext
            };
            var resultObject = new object();
            var result       = new ObjectResult(resultObject);

            await executor.ExecuteAsync(actionContext, result);

            var response = httpContext.Response as HalHttpResponse;

            Assert.AreSame(resultObject, response.Resource);
        }
        public async Task HalMiddlewareNotRegistered_ExecutesInnerTest()
        {
            var inner = InnerExecutor;

            inner.Setup(i => i.ExecuteAsync(It.IsAny <ActionContext>(), It.IsAny <ObjectResult>()))
            .Returns(Task.CompletedTask)
            .Verifiable();

            var executor = new HalObjectResultExecutor(
                inner.Object,
                WriterFactory.Object,
                FormatterSelector.Object,
                Logger);

            var actionContext = new ActionContext
            {
                HttpContext = HttpContext
            };

            await executor.ExecuteAsync(actionContext, ObjectResult);

            inner.Verify();
        }