예제 #1
0
        public async Task InvokeMiddleware_RequestContainsImages_DoesNotStoreImageIfCacheFull()
        {
            var context = new DefaultHttpContext();

            context.Response.Body = new MemoryStream();
            var pathToFile       = Path.Combine(AppContext.BaseDirectory, "images/testname.bmp");
            var pathToSecondFile = Path.Combine(AppContext.BaseDirectory, "images/testname2.bmp");

            CreateTestDirectory();
            await File.WriteAllTextAsync(Path.Combine(_pathToImageFolder, "firstimage.bmp"), "Text1");

            await File.WriteAllTextAsync(Path.Combine(_pathToImageFolder, "secondimage.bmp"), "Text2");

            context.Request.Path = "/images/testname";
            var middleware = new ImageCachingMiddleware(next: (innerHttpContext) => Task.CompletedTask, new ImageCachingOptions("images", 3, new TimeSpan(1, 0, 0), 7));

            await middleware.Invoke(context);

            Assert.True(File.Exists(pathToFile));
            Assert.NotEqual(0, context.Response.ContentLength);

            context = new DefaultHttpContext();
            context.Response.Body = new MemoryStream();
            await middleware.Invoke(context);

            Assert.False(File.Exists(pathToSecondFile));
            Assert.Null(context.Response.ContentLength);
        }
예제 #2
0
        public async Task InvokeMiddleware_RequestContainsImages_DirectoryToStoreExists()
        {
            var context = new DefaultHttpContext();

            context.Response.Body = new MemoryStream();
            context.Request.Path  = "/images/test";
            var middleware = new ImageCachingMiddleware(next: (innerHttpContext) => Task.CompletedTask);

            await middleware.Invoke(context);

            Assert.True(Directory.Exists(_pathToImageFolder));
        }
예제 #3
0
        public async Task InvokeMiddleware_RequestContainsImages_ReturnsContentTypeImgBmp()
        {
            var context = new DefaultHttpContext();

            context.Response.Body = new MemoryStream();
            context.Request.Path  = "/images/3";
            var middleware = new ImageCachingMiddleware(next: (innerHttpContext) => Task.CompletedTask);

            await middleware.Invoke(context);

            Assert.Equal("image/bmp", context.Response.ContentType);
        }
예제 #4
0
        public async Task InvokeMiddleware_RequestContainsImages_FileTestnameExistsInCache()
        {
            var context = new DefaultHttpContext();

            context.Response.Body = new MemoryStream();
            var pathToFile = Path.Combine(_pathToImageFolder, "testname.bmp");

            context.Request.Path = "/images/testname";
            var middleware = new ImageCachingMiddleware(next: (innerHttpContext) => Task.CompletedTask);

            await middleware.Invoke(context);

            Assert.True(File.Exists(pathToFile));
        }
예제 #5
0
        public async Task InvokeMiddleware_RequestContainsImages_ReturnsFromLocalCacheFile()
        {
            var context = new DefaultHttpContext();

            context.Response.Body = new MemoryStream();
            var pathToFile = Path.Combine(AppContext.BaseDirectory, "images/testname.bmp");

            CreateTestDirectory();
            await File.WriteAllTextAsync(pathToFile, "Text");

            context.Request.Path = "/images/testname";
            var middleware = new ImageCachingMiddleware(next: (innerHttpContext) => Task.CompletedTask);

            await middleware.Invoke(context);

            Assert.True(File.Exists(pathToFile));
            Assert.NotEqual(0, context.Response.Body.Length);
        }
예제 #6
0
        public async Task InvokeMiddleware_RequestContainsImages_FileIsWrittenAftercleanUp()
        {
            var context = new DefaultHttpContext();

            context.Response.Body = new MemoryStream();
            var pathToFileStored  = Path.Combine(AppContext.BaseDirectory, "images/testname.bmp");
            var pathToFileToStore = Path.Combine(AppContext.BaseDirectory, "images/testnamesecond.bmp");

            CreateTestDirectory();
            await File.WriteAllTextAsync(pathToFileStored, "Text");

            context.Request.Path = "/images/testnamesecond";
            var middleware = new ImageCachingMiddleware(next: (innerHttpContext) => Task.CompletedTask,
                                                        new ImageCachingOptions("images", 3, new TimeSpan(1, 0, 0), 7));

            Directory.SetCreationTime(_pathToImageFolder, DateTime.Now.AddDays(-8));

            await middleware.Invoke(context);

            Assert.True(File.Exists(pathToFileToStore));
            Assert.False(File.Exists(pathToFileStored));
        }