public void Test_CacheControlMode() { string configCache = @"<magick.net.web canCreateDirectories=""false"" cacheDirectory=""c:\cache""/>"; string configNoCache = @" <magick.net.web canCreateDirectories=""false"" cacheDirectory=""c:\cache""> <clientCache cacheControlMode=""NoControl""/> </magick.net.web>"; HttpRequest request = new HttpRequest("foo", "https://bar", string.Empty); using (MemoryStream memStream = new MemoryStream()) { using (StreamWriter writer = new StreamWriter(memStream)) { HttpResponse response = new HttpResponse(writer); HttpContext context = new HttpContext(request, response); MagickWebSettings settings = TestSectionLoader.Load(configNoCache); TestMagickHandler handler = new TestMagickHandler(settings, ImageData); handler.ProcessRequest(context); Assert.AreNotEqual(HttpCacheability.Public, response.Cache.GetCacheability()); settings = TestSectionLoader.Load(configCache); handler = new TestMagickHandler(settings, ImageData); handler.ProcessRequest(context); Assert.AreEqual(HttpCacheability.Public, response.Cache.GetCacheability()); } } }
public void Test_Defaults() { using (TemporaryDirectory directory = new TemporaryDirectory()) { string tempDir = directory.FullName + "\\"; string config = $@"<magick.net.web cacheDirectory=""{tempDir}""/>"; MagickWebSettings settings = TestSectionLoader.Load(config); Assert.AreEqual(tempDir, settings.CacheDirectory); Assert.IsTrue(settings.CanCreateDirectories); Assert.AreEqual(new TimeSpan(1, 0, 0, 0), settings.ClientCache.CacheControlMaxAge); Assert.AreEqual(CacheControlMode.UseMaxAge, settings.ClientCache.CacheControlMode); Assert.IsTrue(settings.EnableGzip); Assert.IsTrue(settings.Optimization.IsEnabled); Assert.IsTrue(settings.Optimization.Lossless); Assert.IsFalse(settings.Optimization.OptimalCompression); Assert.IsNull(settings.ResourceLimits.Height); Assert.IsNull(settings.ResourceLimits.Width); Assert.IsFalse(settings.ShowVersion); Assert.AreEqual(Path.GetTempPath(), settings.TempDirectory); Assert.IsFalse(settings.UseOpenCL); var urlResolverSettings = settings.UrlResolvers.Cast <UrlResolverSettings>(); Assert.AreEqual(0, urlResolverSettings.Count()); Assert.IsTrue(Directory.Exists(tempDir)); settings = TestSectionLoader.Load(config); } }
public void Test_WriteFile() { string config = @"<magick.net.web canCreateDirectories=""false"" cacheDirectory=""c:\cache""/>"; MagickWebSettings settings = TestSectionLoader.Load(config); TestMagickHandler handler = new TestMagickHandler(settings, ImageData); HttpRequest request = new HttpRequest("foo", "https://bar", string.Empty); using (MemoryStream memStream = new MemoryStream()) { using (StreamWriter writer = new StreamWriter(memStream)) { HttpResponse response = new HttpResponse(writer); HttpContext context = new HttpContext(request, response); Assert.IsFalse(handler.IsReusable); handler.ProcessRequest(null); handler.FileName = null; handler.ProcessRequest(context); handler.FileName = string.Empty; handler.ProcessRequest(context); handler.FileName = "missing"; ExceptionAssert.ThrowsArgumentNullException("path", () => { handler.ProcessRequest(context); }); } } }
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.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()); } }
public void Test_CanCompress() { string config = @"<magick.net.web canCreateDirectories=""false"" cacheDirectory=""c:\cache"" enableGzip=""false""/>"; MagickWebSettings settings = TestSectionLoader.Load(config); bool canCompress = HandlerHelper.CanCompress(settings, SvgFormatInfo); Assert.IsFalse(canCompress); }
public void Test_CanOptimize() { string config = @" <magick.net.web canCreateDirectories=""false"" cacheDirectory=""c:\cache""> <optimization enabled=""false""/> </magick.net.web>"; MagickWebSettings settings = TestSectionLoader.Load(config); bool canCompress = HandlerHelper.CanOptimize(settings, JpgFormatInfo); Assert.IsFalse(canCompress); }
private void Test_Optimize(IImageData imageData, IScriptData scriptData) { using (TemporaryDirectory directory = new TemporaryDirectory()) { string tempDir = directory.FullName; 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", string.Empty); 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); } }
public void Test_Exceptions() { ExceptionAssert.Throws <ConfigurationErrorsException>(() => { MagickWebSettings settings = MagickWebSettings.Instance; }); string config = @" <magick.net.web canCreateDirectories=""false"" cacheDirectory=""c:\cache""> <urlResolvers> <urlResolver type=""Magick.NET.Tests.MagickWebSettingsTests, Magick.NET.Tests""/> </urlResolvers> </magick.net.web>"; ExceptionAssert.Throws <ConfigurationErrorsException>(() => { TestSectionLoader.Load(config); }); }
public void Test_Version() { string config = @"<magick.net.web canCreateDirectories=""false"" cacheDirectory=""c:\cache"" showVersion=""true""/>"; MagickWebSettings settings = TestSectionLoader.Load(config); TestMagickHandler handler = new TestMagickHandler(settings, ImageData); HttpRequest request = new HttpRequest("foo", "https://bar", string.Empty); using (MemoryStream memStream = new MemoryStream()) { using (StreamWriter writer = new StreamWriter(memStream)) { HttpResponse response = new HttpResponse(writer); HttpContext context = new HttpContext(request, response); handler.ProcessRequest(context); Assert.AreEqual("image/png", response.ContentType); Assert.IsNotNull(response.GetHeader("X-Magick")); } } }
public void Test_Properties() { string config = @" <magick.net.web canCreateDirectories=""false"" cacheDirectory=""~\cache"" tempDirectory=""c:\temp\"" enableGzip=""false"" showVersion=""true"" useOpenCL=""true""> <clientCache cacheControlMaxAge=""4:2:0"" cacheControlMode=""NoControl""/> <optimization enabled=""false"" lossless=""false"" optimalCompression=""true""/> <resourceLimits width=""1"" height=""2""/> <urlResolvers> <urlResolver type=""Magick.NET.Tests.Web.TestFileUrlResolver, Magick.NET.Tests""/> </urlResolvers> </magick.net.web>"; MagickWebSettings settings = TestSectionLoader.Load(config); Assert.IsTrue(settings.CacheDirectory.EndsWith(@"\cache\")); Assert.IsFalse(settings.CanCreateDirectories); Assert.AreEqual(new TimeSpan(4, 2, 0), settings.ClientCache.CacheControlMaxAge); Assert.AreEqual(CacheControlMode.NoControl, settings.ClientCache.CacheControlMode); Assert.IsFalse(settings.EnableGzip); Assert.IsFalse(settings.Optimization.IsEnabled); Assert.IsFalse(settings.Optimization.Lossless); Assert.IsTrue(settings.Optimization.OptimalCompression); Assert.AreEqual(1, settings.ResourceLimits.Width); Assert.AreEqual(2, settings.ResourceLimits.Height); Assert.IsTrue(settings.ShowVersion); Assert.AreEqual(@"c:\temp\", settings.TempDirectory); Assert.IsTrue(settings.UseOpenCL); var urlResolverSettings = settings.UrlResolvers.Cast <UrlResolverSettings>(); Assert.AreEqual(1, urlResolverSettings.Count()); IUrlResolver urlResolver = urlResolverSettings.First().CreateInstance(); Assert.AreEqual(urlResolver.GetType(), typeof(TestFileUrlResolver)); }
public void Test_Write304() { string config = @"<magick.net.web canCreateDirectories=""false"" cacheDirectory=""c:\cache""/>"; MagickWebSettings settings = TestSectionLoader.Load(config); TestMagickHandler handler = new TestMagickHandler(settings, ImageData); HttpRequest request = new HttpRequest("foo", "https://bar", string.Empty); using (MemoryStream memStream = new MemoryStream()) { using (StreamWriter writer = new StreamWriter(memStream)) { HttpResponse response = new HttpResponse(writer); HttpContext context = new HttpContext(request, response); FileInfo file = new FileInfo(handler.FileName); string modifiedSince = file.LastWriteTimeUtc.AddMinutes(1).ToString("r", CultureInfo.InvariantCulture); request.SetHeaders("If-Modified-Since", modifiedSince); handler.ProcessRequest(context); Assert.AreEqual(200, response.StatusCode); Assert.AreEqual(file.LastWriteTimeUtc.ToString(), response.Cache.GetLastModified().ToString()); request.SetHeaders("If-Modified-Since", "foobar"); handler.ProcessRequest(context); Assert.AreEqual(200, response.StatusCode); modifiedSince = file.LastWriteTimeUtc.AddMinutes(1).ToString("r", CultureInfo.InvariantCulture) + "; foo"; request.SetHeaders("If-Modified-Since", modifiedSince); handler.ProcessRequest(context); Assert.AreEqual(200, response.StatusCode); modifiedSince = file.LastWriteTimeUtc.ToString("r", CultureInfo.InvariantCulture); request.SetHeaders("If-Modified-Since", modifiedSince); handler.ProcessRequest(context); Assert.AreEqual(304, response.StatusCode); string tempFile = Path.GetTempFileName(); try { File.Copy(handler.FileName, tempFile, true); File.SetLastWriteTimeUtc(tempFile, DateTime.Now.AddYears(2)); handler.FileName = tempFile; request.SetHeaders("If-Modified-Since", modifiedSince); handler.ProcessRequest(context); Assert.AreEqual(304, response.StatusCode); Assert.AreEqual(DateTime.Now.Year, response.Cache.GetLastModified().Year); } finally { Cleanup.DeleteFile(tempFile); } } } }
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); } } }