private void Test_ProcessRequest(IImageData imageData) { 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"); 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, string.Empty); 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()); } }
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); } } }