예제 #1
0
        private void Test_Optimize(IImageData imageData, IScriptData scriptData)
        {
            string tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

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

                MagickWebSettings settings = TestSectionLoader.Load(config);

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

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

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

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

                outputFile.Refresh();
                long lengthWithoutOptimization = outputFile.Length;

                foreach (FileInfo file in tempDir.GetFiles())
                {
                    file.Delete();
                }

                config   = $@"<magick.net.web cacheDirectory=""{tempDir}"" tempDirectory=""{tempDir}""/>";
                settings = TestSectionLoader.Load(config);

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

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

                outputFile.Refresh();
                Assert.IsTrue(outputFile.Length < lengthWithoutOptimization);
            }
            finally
            {
                if (Directory.Exists(tempDir))
                {
                    Directory.Delete(tempDir, true);
                }
            }
        }
예제 #2
0
        private void Test_ProcessRequest(IImageData imageData, TestScriptData scriptData)
        {
            using (TemporaryDirectory directory = new TemporaryDirectory())
            {
                string tempDir = directory.FullName;

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

                MagickWebSettings settings = TestSectionLoader.Load(config);

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

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

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

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

                using (IMagickImage image = new MagickImage(outputFile))
                {
                    Assert.AreEqual(MagickFormat.Png, image.Format);
                    Assert.AreEqual(62, image.Width);
                    Assert.AreEqual(59, image.Height);
                }

                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, false, Encoding))
                {
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

                    MagickScriptHandler handler = new MagickScriptHandler(settings, imageData, scriptData);
                    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, false, Encoding))
                {
                    HttpResponse response = new HttpResponse(writer);
                    HttpContext  context  = new HttpContext(request, response);

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

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

                using (IMagickImage image = new MagickImage(outputFile))
                {
                    Assert.AreEqual(MagickFormat.Png, image.Format);
                    Assert.AreEqual(62, image.Width);
                    Assert.AreEqual(59, image.Height);
                }

                scriptData.OutputFormat = MagickFormat.Tiff;

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

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

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

                Assert.AreEqual(4, tempDir.GetFiles().Count());

                using (IMagickImage image = new MagickImage(outputFile))
                {
                    Assert.AreEqual(MagickFormat.Tiff, image.Format);
                    Assert.AreEqual(62, image.Width);
                    Assert.AreEqual(59, image.Height);
                }
            }
        }