public async Task ResponseHasStarted_DoesNothing()
        {
            var next = Next;

            next.Setup(r => r(It.IsAny <HttpContext>()))
            .Returns(Task.CompletedTask);

            var middleware = new HalMiddleware(
                next.Object,
                Logger,
                HalOptions.Object,
                PipelineFactory.Object);

            // If someone tries to write to the stream, this will throw.
            var responseBody = new Mock <Stream>(MockBehavior.Strict);

            responseBody.SetupGet(s => s.Length).Returns(2);
            var httpContext = HttpContext;

            httpContext.Response.Body = responseBody.Object;

            await middleware.Invoke(httpContext);

            next.Verify();
        }
        public async Task ResourcePipeline_ResultPassedToExecutor()
        {
            var actionContext = Mock.Of <ActionContext>();
            var objectResult  = new ObjectResult(new object());
            var executorMock  = new Mock <IActionResultExecutor <ObjectResult> >();

            executorMock.Setup(e => e.ExecuteAsync(actionContext, objectResult))
            .Returns(Task.CompletedTask)
            .Verifiable();

            var formattingContext = new HalFormattingContext(
                actionContext,
                new ObjectResult(new object()),
                executorMock.Object);

            var next = Next;

            next.Setup(r => r(It.IsAny <HttpContext>()))
            .Returns(Task.CompletedTask)
            .Callback <HttpContext>((context) =>
            {
                var halFeature = context.Features.Get <HalFeature>();
                halFeature.FormattingContext = formattingContext;
            });

            var pipeline = Pipeline;

            pipeline.Setup(p => p.InvokeAsync(It.IsAny <HalFormattingContext>()))
            .Returns(() => Task.FromResult(objectResult))
            .Verifiable();
            var pipelineFactory = PipelineFactory;

            pipelineFactory.Setup(p => p.Create(It.IsAny <MvcPipeline>()))
            .Returns(pipeline.Object)
            .Verifiable();

            var middleware = new HalMiddleware(
                next.Object,
                Logger,
                HalOptions.Object,
                pipelineFactory.Object);

            await middleware.Invoke(HttpContext);

            executorMock.Verify();
        }
        public async Task NoHalFormattingContext_DoesNothing()
        {
            var next = Next;

            next.Setup(r => r(It.IsAny <HttpContext>()))
            .Returns(Task.CompletedTask);

            var middleware = new HalMiddleware(
                next.Object,
                Logger,
                HalOptions.Object,
                PipelineFactory.Object);

            var httpContext = HttpContext;
            var response    = httpContext.Response;

            await middleware.Invoke(httpContext);

            Assert.IsFalse(response.HasStarted);
        }
        public async Task SetsHalFeature()
        {
            var next = Next;

            next.Setup(r => r(It.IsAny <HttpContext>()))
            .Returns(Task.CompletedTask)
            .Callback <HttpContext>(context =>
            {
                var halFeature = context.Features.Get <HalFeature>();
                Assert.NotNull(halFeature);
            })
            .Verifiable();

            var middleware = new HalMiddleware(
                next.Object,
                Logger,
                HalOptions.Object,
                PipelineFactory.Object);

            await middleware.Invoke(HttpContext);

            next.Verify();
        }