public async Task AssetMiddleware_NoCache()
        {
            string cssContent = "*{color:red}";

            var pipeline = new AssetPipeline();
            var options  = new WebOptimizerOptions()
            {
                EnableCaching = false
            };
            var asset = new Mock <IAsset>().SetupAllProperties();

            asset.SetupGet(a => a.ContentType).Returns("text/css");
            asset.SetupGet(a => a.Route).Returns("/file.css");
            asset.Setup(a => a.ExecuteAsync(It.IsAny <HttpContext>(), options))
            .Returns(Task.FromResult(cssContent.AsByteArray()));

            StringValues values;
            var          response = new Mock <HttpResponse>().SetupAllProperties();
            var          context  = new Mock <HttpContext>().SetupAllProperties();

            context.Setup(s => s.Request.Headers.TryGetValue("Accept-Encoding", out values))
            .Returns(false);
            context.Setup(c => c.Response)
            .Returns(response.Object);

            context.Setup(c => c.Request.Path).Returns("/file.css");

            response.SetupGet(c => c.Headers)
            .Returns(new HeaderDictionary());

            var next  = new Mock <RequestDelegate>();
            var env   = new HostingEnvironment();
            var cache = new Mock <IMemoryCache>();

            var member = pipeline.GetType().GetField("_assets", BindingFlags.NonPublic | BindingFlags.Instance);

            member.SetValue(pipeline, new List <IAsset> {
                asset.Object
            });

            var amo = new Mock <IOptionsSnapshot <WebOptimizerOptions> >();

            amo.SetupGet(a => a.Value).Returns(options);

            var mcr     = new AssetResponse(cssContent.AsByteArray(), null);
            var logger  = new Mock <ILogger <AssetMiddleware> >();
            var builder = new Mock <IAssetBuilder>();

            builder.Setup(b => b.BuildAsync(It.IsAny <IAsset>(), context.Object, options)).Returns(Task.FromResult <IAssetResponse>(mcr));
            var middleware = new AssetMiddleware(next.Object, pipeline, logger.Object, builder.Object);
            var stream     = new MemoryStream();

            response.Setup(r => r.Body).Returns(stream);
            await middleware.InvokeAsync(context.Object, amo.Object);

            Assert.Equal("text/css", context.Object.Response.ContentType);
            Assert.Equal(cssContent.AsByteArray(), await stream.AsBytesAsync());
            Assert.Equal(0, response.Object.StatusCode);
        }
        public async Task AssetBuilder_NoMemoryCache()
        {
            byte[] cssContent = "*{color:red}".AsByteArray();

            var pipeline = new AssetPipeline();
            var options  = new WebOptimizerOptions()
            {
                EnableMemoryCache = false
            };
            var asset = new Mock <IAsset>().SetupAllProperties();

            asset.SetupGet(a => a.ContentType).Returns("text/css");
            asset.SetupGet(a => a.Route).Returns("/file.css");
            asset.Setup(a => a.GenerateCacheKey(It.IsAny <HttpContext>())).Returns("cachekey");
            asset.Setup(a => a.ExecuteAsync(It.IsAny <HttpContext>(), options))
            .Returns(Task.FromResult(cssContent));

            StringValues values;
            var          response = new Mock <HttpResponse>().SetupAllProperties();

            response.Setup(r => r.Headers.Keys).Returns(new string[] { });
            var context = new Mock <HttpContext>().SetupAllProperties();

            context.Setup(s => s.Request.Headers.TryGetValue("Accept-Encoding", out values)).Returns(false);
            context.Setup(c => c.Response).Returns(response.Object);
            context.Setup(c => c.Request.Path).Returns("/file.css");

            var next = new Mock <RequestDelegate>();
            var env  = new Mock <IHostingEnvironment>().SetupAllProperties();

            env.Setup(e => e.ContentRootPath).Returns(@"D:\Project\");

            object bytes;
            var    cache = new Mock <IMemoryCache>();

            cache.Setup(c => c.TryGetValue(It.IsAny <string>(), out bytes)).Throws <InvalidOperationException>();

            var store = new Mock <IAssetResponseStore>();

            var member = pipeline.GetType().GetField("_assets", BindingFlags.NonPublic | BindingFlags.Instance);

            member.SetValue(pipeline, new List <IAsset> {
                asset.Object
            });

            var amo = new Mock <IOptionsSnapshot <WebOptimizerOptions> >();

            amo.SetupGet(a => a.Value).Returns(options);

            var logger  = new Mock <ILogger <AssetBuilder> >();
            var builder = new AssetBuilder(cache.Object, store.Object, logger.Object, env.Object);

            var result = await builder.BuildAsync(asset.Object, context.Object, options).ConfigureAwait(false);

            Assert.Equal(cssContent, result.Body);
        }
        public async Task AssetMiddleware_NoCache_EmptyResponse()
        {
            var pipeline = new AssetPipeline();
            var options  = new WebOptimizerOptions()
            {
                EnableCaching = false
            };
            var asset = new Mock <IAsset>().SetupAllProperties();

            asset.SetupGet(a => a.ContentType).Returns("text/css");
            asset.SetupGet(a => a.Route).Returns("/file.css");
            asset.Setup(a => a.ExecuteAsync(It.IsAny <HttpContext>(), options))
            .Returns(Task.FromResult(new byte[0]));

            StringValues values;
            var          context = new Mock <HttpContext>().SetupAllProperties();

            context.Setup(s => s.Request.Headers.TryGetValue("Accept-Encoding", out values))
            .Returns(false);

            context.Setup(c => c.Request.Path).Returns("/file.css");

            context.SetupGet(c => c.Response.Headers)
            .Returns(new HeaderDictionary());

            var next  = new Mock <RequestDelegate>();
            var env   = new HostingEnvironment();
            var cache = new Mock <IMemoryCache>();

            var member = pipeline.GetType().GetField("_assets", BindingFlags.NonPublic | BindingFlags.Instance);

            member.SetValue(pipeline, new List <IAsset> {
                asset.Object
            });

            var amo = new Mock <IOptionsSnapshot <WebOptimizerOptions> >();

            amo.SetupGet(a => a.Value).Returns(options);

            var logger     = new Mock <ILogger <AssetMiddleware> >();
            var builder    = new Mock <IAssetBuilder>();
            var middleware = new AssetMiddleware(next.Object, pipeline, logger.Object, builder.Object);

            var stream = new MemoryStream();

            await middleware.InvokeAsync(context.Object, amo.Object);

            next.Verify(n => n(context.Object), Times.Once);
        }