private void Test_ProcessRequest(IImageData imageData)
        {
            using (TemporaryDirectory directory = new TemporaryDirectory())
            {
                string tempDir = directory.FullName;

                string config = $@"<magick.net.web cacheDirectory=""{tempDir}"" tempDirectory=""{tempDir}"" useOpenCL=""true""/>";

                MagickWebSettings settings = TestSectionLoader.Load(config);

                HttpRequest request = new HttpRequest("foo", "https://bar", string.Empty);

                string outputFile = Path.Combine(tempDir, "output.jpg");

                using (StreamWriter writer = new StreamWriter(outputFile, false, Encoding))
                {
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

                    ImageOptimizerHandler handler = new ImageOptimizerHandler(settings, imageData);
                    handler.ProcessRequest(context);
                }

                byte[] imageBytes = imageData.GetBytes();
                Assert.IsTrue(new FileInfo(outputFile).Length < imageBytes.Length);
                Assert.AreEqual(2, tempDir.GetFiles().Count());

                File.Delete(outputFile);

                FileInfo cacheFile = tempDir.GetFiles().First();
                File.WriteAllText(cacheFile.FullName, string.Empty);

                using (StreamWriter writer = new StreamWriter(outputFile))
                {
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

                    ImageOptimizerHandler handler = new ImageOptimizerHandler(settings, imageData);
                    handler.ProcessRequest(context);
                }

                Assert.AreEqual(0, File.ReadAllBytes(outputFile).Count());
                Assert.AreEqual(2, tempDir.GetFiles().Count());

                cacheFile.LastWriteTimeUtc = new DateTime(1979, 11, 19);

                using (StreamWriter writer = new StreamWriter(outputFile))
                {
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

                    ImageOptimizerHandler handler = new ImageOptimizerHandler(settings, imageData);
                    handler.ProcessRequest(context);
                }

                Assert.AreNotEqual(0, File.ReadAllBytes(outputFile).Count());
                Assert.AreEqual(2, tempDir.GetFiles().Count());
            }
        }
Exemplo n.º 2
0
        private void Test_ProcessRequest(IImageData imageData)
        {
            string tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            try
            {
                string config = $@"<magick.net.web cacheDirectory=""{tempDir}"" tempDirectory=""{tempDir}""/>";

                MagickWebSettings settings = TestSectionLoader.Load(config);

                HttpRequest request = new HttpRequest("foo", "https://bar", "");

                string outputFile = Path.Combine(tempDir, "output");

                using (StreamWriter writer = new StreamWriter(outputFile))
                {
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

                    GzipHandler handler = new GzipHandler(settings, imageData);
                    handler.ProcessRequest(context);
                }

                Assert.AreEqual(0, new FileInfo(outputFile).Length);
                Assert.AreEqual(1, tempDir.GetFiles().Count());

                using (StreamWriter writer = new StreamWriter(outputFile))
                {
                    request.SetHeaders("Accept-Encoding", "invalid");
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

                    GzipHandler handler = new GzipHandler(settings, imageData);
                    handler.ProcessRequest(context);
                }

                Assert.AreEqual(0, new FileInfo(outputFile).Length);
                Assert.AreEqual(1, tempDir.GetFiles().Count());

                using (StreamWriter writer = new StreamWriter(outputFile))
                {
                    request.SetHeaders("Accept-Encoding", "gzip");
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

                    GzipHandler handler = new GzipHandler(settings, imageData);
                    handler.ProcessRequest(context);
                }

                byte[] imageBytes = imageData.GetBytes();
                Assert.IsTrue(new FileInfo(outputFile).Length < imageBytes.Length);
                Assert.AreEqual(2, tempDir.GetFiles().Count());

                File.Delete(outputFile);

                FileInfo cacheFile = tempDir.GetFiles().First();
                File.WriteAllText(cacheFile.FullName, "");

                using (StreamWriter writer = new StreamWriter(outputFile))
                {
                    request.SetHeaders("Accept-Encoding", "gzip");
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

                    GzipHandler handler = new GzipHandler(settings, imageData);
                    handler.ProcessRequest(context);
                }

                Assert.AreEqual(0, File.ReadAllBytes(outputFile).Count());
                Assert.AreEqual(2, tempDir.GetFiles().Count());

                cacheFile.LastWriteTimeUtc = new DateTime(1979, 11, 19);

                using (StreamWriter writer = new StreamWriter(outputFile))
                {
                    request.SetHeaders("Accept-Encoding", "gzip");
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

                    GzipHandler handler = new GzipHandler(settings, imageData);
                    handler.ProcessRequest(context);
                }

                Assert.AreNotEqual(0, File.ReadAllBytes(cacheFile.FullName).Count());
                Assert.AreEqual(2, tempDir.GetFiles().Count());

                using (StreamWriter writer = new StreamWriter(outputFile))
                {
                    request.SetHeaders("Accept-Encoding", "deflate");
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

                    GzipHandler handler = new GzipHandler(settings, imageData);
                    handler.ProcessRequest(context);
                }

                Assert.IsTrue(new FileInfo(outputFile).Length < imageBytes.Length);
                Assert.AreEqual(3, tempDir.GetFiles().Count());
            }
            finally
            {
                if (Directory.Exists(tempDir))
                {
                    Directory.Delete(tempDir, true);
                }
            }
        }