public void AddSettingsTests() { Settings setting = new Settings(MakeNode(rman.GetString("DeflateAndHigh"))); Assertion.AssertEquals(CompressionLevels.High, setting.CompressionLevel); Assertion.AssertEquals(Algorithms.Deflate, setting.PreferredAlgorithm); // test adding a null node setting.AddSettings(null); Assertion.AssertEquals(CompressionLevels.High, setting.CompressionLevel); Assertion.AssertEquals(Algorithms.Deflate, setting.PreferredAlgorithm); // test overriding algorithm setting.AddSettings(MakeNode(rman.GetString("GZip"))); Assertion.AssertEquals(CompressionLevels.High, setting.CompressionLevel); Assertion.AssertEquals(Algorithms.GZip, setting.PreferredAlgorithm); // test overriding compression level setting.AddSettings(MakeNode(rman.GetString("LevelLow"))); Assertion.AssertEquals(CompressionLevels.Low, setting.CompressionLevel); Assertion.AssertEquals(Algorithms.GZip, setting.PreferredAlgorithm); }
/// <summary> /// Get ahold of a <see cref="CompressingFilter"/> for the given encoding scheme. /// If no encoding scheme can be found, it returns null. /// </summary> /// <remarks> /// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3 for details /// on how clients are supposed to construct the Accept-Encoding header. This /// implementation follows those rules, though we allow the server to override /// the preference given to different supported algorithms. I'm doing this as /// I would rather give the server control over the algorithm decision than /// the client. If the clients send up * as an accepted encoding with highest /// quality, we use the preferred algorithm as specified in the config file. /// </remarks> public static CompressingFilter GetFilterForScheme(string[] schemes, Stream output, Settings prefs) { bool foundDeflate = false; bool foundGZip = false; bool foundStar = false; float deflateQuality = 0f; float gZipQuality = 0f; float starQuality = 0f; bool isAcceptableDeflate; bool isAcceptableGZip; bool isAcceptableStar; for (int i = 0; i<schemes.Length;i++) { string acceptEncodingValue = schemes[i].Trim().ToLower(); if (acceptEncodingValue.StartsWith("deflate")) { foundDeflate = true; float newDeflateQuality = GetQuality(acceptEncodingValue); if (deflateQuality < newDeflateQuality) deflateQuality = newDeflateQuality; } else if (acceptEncodingValue.StartsWith("gzip") || acceptEncodingValue.StartsWith("x-gzip")) { foundGZip = true; float newGZipQuality = GetQuality(acceptEncodingValue); if (gZipQuality < newGZipQuality) gZipQuality = newGZipQuality; } else if (acceptEncodingValue.StartsWith("*")) { foundStar = true; float newStarQuality = GetQuality(acceptEncodingValue); if (starQuality < newStarQuality) starQuality = newStarQuality; } } isAcceptableStar = foundStar && (starQuality > 0); isAcceptableDeflate = (foundDeflate && (deflateQuality > 0)) || (!foundDeflate && isAcceptableStar); isAcceptableGZip = (foundGZip && (gZipQuality > 0)) || (!foundGZip && isAcceptableStar); if (isAcceptableDeflate && !foundDeflate) deflateQuality = starQuality; if (isAcceptableGZip && !foundGZip) gZipQuality = starQuality; // do they support any of our compression methods? if(!(isAcceptableDeflate || isAcceptableGZip || isAcceptableStar)) { return null; } // if deflate is better according to client if (isAcceptableDeflate && (!isAcceptableGZip || (deflateQuality > gZipQuality))) return new DeflateFilter(output, prefs.CompressionLevel); // if gzip is better according to client if (isAcceptableGZip && (!isAcceptableDeflate || (deflateQuality < gZipQuality))) return new GZipFilter(output); // if we're here, the client either didn't have a preference or they don't support compression if(isAcceptableDeflate && (prefs.PreferredAlgorithm == Algorithms.Deflate || prefs.PreferredAlgorithm == Algorithms.Default)) return new DeflateFilter(output, prefs.CompressionLevel); if(isAcceptableGZip && prefs.PreferredAlgorithm == Algorithms.GZip) return new GZipFilter(output); if(isAcceptableDeflate || isAcceptableStar) return new DeflateFilter(output, prefs.CompressionLevel); if(isAcceptableGZip) return new GZipFilter(output); // return null. we couldn't find a filter. return null; }
public void ExcludeMultiplePaths() { Settings setting = new Settings(MakeNode(rman.GetString("ExcludeMultiplePaths"))); Assertion.Assert(setting.IsExcludedPath("foo.aspx")); Assertion.Assert(setting.IsExcludedPath("foo/bar.aspx")); }
public void EmptyNodeTest() { Settings setting = new Settings(MakeNode(rman.GetString("EmptyNode"))); Assertion.AssertEquals(CompressionLevels.Default, setting.CompressionLevel); Assertion.AssertEquals(Algorithms.Default, setting.PreferredAlgorithm); }
public void BadLevelTest() { Settings setting = new Settings(MakeNode(rman.GetString("BadLevel"))); Assertion.AssertEquals(CompressionLevels.Default, setting.CompressionLevel); }
public void BadAlgorithmTest() { Settings setting = new Settings(MakeNode(rman.GetString("BadAlgorithm"))); Assertion.AssertEquals(Algorithms.Default, setting.PreferredAlgorithm); }
public void UserRemovedExcludedType() { Settings setting = new Settings(MakeNode(rman.GetString("ExcludeMultipleTypes"))); Assertion.Assert(setting.IsExcludedMimeType("ben/foo")); Assertion.Assert(setting.IsExcludedMimeType("image/jpeg")); setting.AddSettings(MakeNode(rman.GetString("RemoveImageJpegExclusion"))); Assertion.Assert(setting.IsExcludedMimeType("ben/foo")); Assertion.Assert(!setting.IsExcludedMimeType("image/jpeg")); }
public void UserAddedMultipleExcludedTypes() { Settings setting = new Settings(MakeNode(rman.GetString("ExcludeMultipleTypes"))); Assertion.Assert(setting.IsExcludedMimeType("ben/foo")); Assertion.Assert(setting.IsExcludedMimeType("image/jpeg")); }
public void UserAddedExcludedType() { Settings setting = new Settings(MakeNode(rman.GetString("ExcludeBenFoo"))); Assertion.Assert(setting.IsExcludedMimeType("ben/foo")); }
public void UserAddedAndRemovedType() { Settings setting = new Settings(MakeNode(rman.GetString("AddAndRemoveSameType"))); Assertion.Assert(!setting.IsExcludedMimeType("user/silly")); }
public void RemoveExistingPathExclude() { Settings setting = new Settings(MakeNode(rman.GetString("ExcludeMultiplePaths"))); setting.AddSettings(MakeNode(rman.GetString("RemoveFooAspxExclude"))); Assertion.Assert(!setting.IsExcludedPath("foo.aspx")); Assertion.Assert(setting.IsExcludedPath("foo/bar.aspx")); }